' ********************************************************************* ' ' Name : Check And Stop Remote Process.vbs ' Author : 3Ds (UK) Limited ' Description : Example VBScript to check for & terminate a named ' remote process. ' ' Arguments : 1 - Server Name. ' 2 - Process Name. ' ' WIZARD:PARAMS=1 = Server Name or . for Local Machine|2 = Name of Process to check for ' WIZARD:PARAMEXAMPLE=MyServer Setup.exe ' ' Returns : 0 - OK, process not running. ' 1 - Parameter error. ' 2 - WMI error. ' 3 - Process running, but unable to terminate. ' 4 - Process running, but now terminated. ' ' WIZARD:RESULTS=Return code ...||0 = OK|1 = Parameter Error|2 = WMI error or script not supported on O/S|3 = Process running but could not terminated|4 = Process was running & was terminated.||Script output also indicates "OK" for successor "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 ' ------ 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 ' Drive 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 ' 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 For Each objData in colData ' Found it so terminate Err.Clear() objData.Terminate() If Err.Number <> 0 Then intResult = 3 Wscript.Echo "Error. The process " & objData.Name & " is running, but could not be terminated. " & Err.Descirpiton Else If (intResult <> 3) Then intResult = 4 End If Wscript.Echo "Error. The process " & objData.Name & " was running and has been terminated." End If Next If intResult = 0 Then Wscript.Echo "OK. The process is not running" End If ' ------ Cleanup & Return ------ Set colData = Nothing Set objData = Nothing Set objWMIService = Nothing WScript.Quit (intResult) ' ------ End of Script ------