' ********************************************************************* ' ' Name : Check Remote Process Running.vbs ' Author : 3Ds (UK) Limited ' Description : Example VBScript to check for X copies of a named ' remote process running. ' ' Arguments : 1 - Server Name. ' 2 - Process Name. ' 3 - No. copies to check for. ' ' WIZARD:PARAMS=1 = Server Name or . for Local Machine|2 = Process name to check for|3 = No. copies that should be runnning ' WIZARD:PARAMEXAMPLE=MyServer "AVirus.exe" 1 ' ' Returns : 0 - OK, process(es) are running. ' 1 - Parameter error. ' 2 - WMI error. ' 3 - Process is not running. ' ' WIZARD:RESULTS=Return code ...||0 = OK|1 = Parameter Error|2 = WMI error or script not supported on O/S|3 = Process is not running, or not enough copies are running.||Script output also indicates "OK" for success, or "Error" with details of the faults found. ' ' Notes : Requires Windows Scripting Host (for VBScript) & ' WMI installed. ' ' WIZARD:REMOTE ' ' ********************************************************************* ' Enable in-line error handling On Error Resume Next ' ------ Local declarations ------ Dim objArguments ' Command line arguments Dim objWMIService ' WMI object Dim objData ' Data object Dim strServer ' The server on which the shared drive is held Dim strData ' The data name Dim colData ' Data collection Dim blnFoundItem ' True if we found an entry Dim intResult ' Result Dim intNoCopies ' No. copies wanted Dim intNoCopiesRunning ' No. copies running ' ------ Main Logic ------ ' Default the answer intResult = 0 ' Retrieve Parameters, server name Set objArguments = WScript.Arguments strServer = objArguments.Item(0) If Len (strServer) = 0 Then WScript.Echo "Error. Cannot verify process. No server name specified" WScript.Quit (1) End If ' Process name strData = objArguments.Item(1) If Len (strData) = 0 Then WScript.Echo "Error. Cannot verify process. No process name specified" WScript.Quit (1) End If ' # copies (defaulting to 1) intNoCopies = CInt(objArguments.Item(2)) If Err.number <> 0 Then Err.Clear() WScript.Echo "Warning. No. of copies not specified, defaulting to 1" intNoCopies = 1 End If ' Connect to WMI Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strServer & "\root\cimv2") If Err.Number <> 0 Then WScript.Echo "Error. Cannot connect to WMI." WScript.Echo Err.Description WScript.Quit (2) End If ' Get the status of the process Set colData = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = '" & strData & "'") ' Check to see if its there intNoCopiesRunning = 0 For Each objData in colData ' Found a copy intNoCopiesRunning = intNoCopiesRunning + 1 Next ' Check the total If intNoCopiesRunning < intNoCopies Then intResult = 3 Wscript.Echo "Error. " & intNoCopiesRunning & " copy/copies running, but " & intNoCopies & " required" End If If intResult = 0 Then Wscript.Echo "OK. " & intNoCopiesRunning & " copy/copies of the process running" End If ' ------ Cleanup & Return ------ Set colData = Nothing Set objData = Nothing Set objWMIService = Nothing WScript.Quit (intResult) ' ------ End of Script ------