' ********************************************************************* ' ' Name : Verify Remote Service.vbs ' Author : 3Ds (UK) Limited ' Description : Example VBScript to check the status of a remote ' service. ' ' Arguments : 1 - Server Name. ' 2 - Service Name. ' ' WIZARD:PARAMS=1 = Server Name or . for Local Machine|2 = Service Name ' WIZARD:PARAMEXAMPLE=MyServer "Alerter" ' ' Returns : 0 - Service OK & running. ' 1 - Parameter error. ' 2 - WMI error. ' 3 - Service not installed. ' 4 - Service installed, but not running. ' ' WIZARD:RESULTS=Return code ...||0 = OK|1 = Parameter Error|2 = WMI error or script not supported on O/S|3 = Service is not installed|4 = Service is installed but is not 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 ' ------ 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 service. No server name specified" WScript.Quit (1) End If ' Service name strData = objArguments.Item(1) If Len (strData) = 0 Then WScript.Echo "Error. Cannot verify service. No service 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 service Set colData = objWMIService.ExecQuery ("Select * from Win32_Service Where Name = '" & strData & "'") ' Verify status blnFoundItem = False For Each objData in colData ' Got a value blnFoundItem = True If objData.State <> "Running" Then WScript.Echo "Error. The service is not currently running." & objData.DisplayName intResult = 4 Exit For End If Next ' Check it was installed If blnFoundItem = False Then ' Not installed WScript.Echo "Error. The service is not installed." intResult = 3 End If If intResult = 0 Then Wscript.Echo "OK. The service is running" End If ' ------ Cleanup & Return ------ Set colData = Nothing Set objData = Nothing Set objWMIService = Nothing WScript.Quit (intResult) ' ------ End of Script ------