Functions[-][--][++]

To return a value[-][--][++]

function test()
  dim a
  a = "Hi there"
  test = a          'This returns a
end function

Example Script[-][--][++]


Option Explicit
'Check a process, restart it and send email alert, or kill process depending on time window

'Original script created by NetRes
'Modified by mReschke 2008-04-28 (converted to functions, actions change by time, email enhancements, globals, generic process killer)
'If process is NOT running between start/endHour then restart it and send an email if sendEmail = true
'If process is running outside of start/endHour then kill it.

'Globals
Dim objProcess, objWMIService, colProcesses, Process, ProcessorTime, pc
Dim strBody, iMsg, iConf, Flds, DataList, strComputer

Dim processName        'Name of process to check/kill/restart    (NOTE: Case Sensetive !!!)
Dim processPath        'Full path to process .exe (do not include name.exe, ex: d:\dynamailgenerator\bin\)
Dim sendEmail        'Bool to send alert email if service is not running
Dim curTime, startHour, endHour, curMin

' ---------------------------------------------------------------------------------
'Set Configs
processName = "DynaMailGeneratorTEST.exe"
processPath = "D:\DynaMailGenerator\bin\"
'processName = "notepad.exe"
'processPath = "c:\windows\system32\"
sendEmail = true

'Determine what to do based on the current time
curTime = CInt(mid(FormatDateTime(Time(), 4), 1, 2))    '24 hour time, just the hour (so 02 or 08 or 12 or 15 or 18)
curMin = CInt(mid(FormatDateTime(Time(), 4), 4, 2))

startHour = 5
endHour = 22

If curTime >= startHour AND curTime < endHour Then
    'Process should be running between these times (restart it if not)
    If IsRunning = False Then
        'Process is NOT running, restart it and send an alert email
        
        if curTime = startHour AND curMin < 20 Then sendEmail = false    ' don't send alert email the first 20 minutes of the day (because it's just starting up, not really restarting)
        
        if sendEmail = true Then Call SendAlertEmail
        Call RestartProcess
    Else
        'Process is running fine, we are good!
    End If
Else
    'Kill the process if it is running
    Call KillProcess
End if

' ---------------------------------------------------------------------------------
Sub SendAlertEmail()
    Set pc = CreateObject("Wscript.Network")
    Set iMsg = CreateObject("CDO.Message")
    Set iConf = CreateObject("CDO.Configuration")
    Set Flds = iConf.Fields

    Const schema = "http://schemas.microsoft.com/cdo/configuration/"
    dim emailBody

    Flds.Item(schema & "sendusing") = 2
    Flds.Item(schema & "smtpserver") = "216.xxx.xxx.xxx" 
    Flds.Item(schema & "smtpserverport") = 25
    Flds.Item(schema & "smtpauthenticate") = 0
    'Flds.Item(schema & "sendusername") = "domain\someuser"
    'Flds.Item(schema & "sendpassword") = "somepass"
    Flds.Item(schema & "smtpusessl") = 0
    Flds.Update

    With iMsg
        .To = "email@domain.com, email2@domain.com"
        .From = "Process Alert "
        .Subject = processName & " has quit"

        emailBody = "

'" & processName & "' has unexpectedly quit

" emailBody = emailBody & "Server: " & pc.ComputerName & "
" emailBody = emailBody & "Process: " & processName & "
" emailBody = emailBody & "Location: " & processPath & processName & "
" emailBody = emailBody & "Time: " & Now & "

" emailBody = emailBody & "Attempting to restart '" & processName & "'

" emailBody = emailBody & "Please check that the process has restarted correctly.
" 'emailBody = emailBody & " .HTMLBody = emailBody .Sender = "Process Alert " .Organization = "My Company" .ReplyTo = "replyto@domain.com" Set .Configuration = iConf .Send End With ' Release Interfaces Set iMsg = nothing Set iConf = nothing Set Flds = nothing End Sub ' --------------------------------------------------------------------------------- Function IsRunning() Dim processRunning processRunning = false strComputer = "." Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer& "\root\cimv2") Set colProcesses = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name='" & processName & "'") For Each Process in colProcesses If Process.Name = processName Then processRunning = True End If Next IsRunning = processRunning 'return processRunning (true or false) End Function ' --------------------------------------------------------------------------------- Sub RestartProcess() 'Restart the Process Dim oShell Set oShell = WScript.CreateObject ("WSCript.shell") oShell.run "" & processPath & processName & "" Set oShell = Nothing End Sub ' --------------------------------------------------------------------------------- Sub KillProcess() strComputer = "." Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer& "\root\cimv2") Set colProcesses = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name='" & processName & "'") For Each Process in colProcesses If Process.Name = processName Then Process.Terminate() End If next End Sub