Wednesday, October 25, 2006

Query Services and Start, Stop, Pause and Continue Services with VBScript

This set of functions will start and stop services on the local machine or remote machines. These functions utilize the "SC" executable which should be in your local computer's path variable.

If you want to use these functions to query or manipulate the local computer's services just pass a period (.) as the computer name string.

The ServiceStatus function will return the service's current status as a string. Examples of possibilities include "started", "stopped" and "paused" along with a few others. The ServiceControl function will return "true" or "false" based on whether the command was successful or "error" if the control string that was passed was unsupported.

Function ServiceStatus(sComputer, sService)
  'This script is provided under the Creative Commons license located
  'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not
  'be used for commercial purposes with out the expressed written consent
  'of NateRice.com

  Const OpenAsDefault = -2
  Const FailIfNotExist = 0
  Const ForReading = 1

  Set oShell = CreateObject("WScript.Shell")
  Set oFSO = CreateObject("Scripting.FileSystemObject")
  sTemp = oShell.ExpandEnvironmentStrings("%TEMP%")
  sTempFile = sTemp & "\" & oFSO.GetTempName

  oShell.Run "%comspec% /c sc \\" & sComputer & " query " & _
  sService & ">" & sTempFile, 0, True

  Set fFile = oFSO.OpenTextFile(sTempFile, ForReading, FailIfNotExist, OpenAsDefault)
  sResults = fFile.ReadAll
  fFile.Close
  oFSO.DeleteFile (sTempFile)
              
  If InStr(lcase(sResults), "running") Then GetServiceStatus = "running"
  If InStr(lcase(sResults), "stopped") Then GetServiceStatus = "stopped"
  If InStr(lcase(sResults), "paused") Then GetServiceStatus = "paused"
  If InStr(lcase(sResults), "continue_pending") Then _
  GetServiceStatus = "continue_pending"
  If InStr(lcase(sResults), "pause_pending") Then GetServiceStatus = "pause_pending"
  If InStr(lcase(sResults), "start_pending") Then GetServiceStatus = "start_pending"
  If InStr(lcase(sResults), "stop_pending") Then GetServiceStatus = "stop_pending"
  If Not Len(GetServiceStatus) > 0 Then GetServiceStatus = "unknown"

  Set oShell = Nothing
  Set oFSO = Nothing
End Function

Function ServiceControl(sComputer, sService, sCommand)
  'This script is provided under the Creative Commons license located
  'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not
  'be used for commercial purposes with out the expressed written consent
  'of NateRice.com

  sCommand = LCase(sCommand)
  If sCommand <> "start" And sCommand <> "stop" _
  And sCommand <> "pause" And sCommand <> "continue" Then
    ServiceControl = "Error. Unknown Command."
    Exit Function
  End If
 
  Const OpenAsDefault = -2
  Const FailIfNotExist = 0
  Const ForReading = 1

  Set oShell = CreateObject("WScript.Shell")
  Set oFSO = CreateObject("Scripting.FileSystemObject")
  sTemp = oShell.ExpandEnvironmentStrings("%TEMP%")
  sTempFile = sTemp & "\" & oFSO.GetTempName

  oShell.Run "%comspec% /c sc \\" & sComputer & " " & sCommand & _
  " " & sService & ">" & sTempFile, 0, True

  Set fFile = oFSO.OpenTextFile(sTempFile, ForReading, FailIfNotExist, OpenAsDefault)
  sResults = fFile.ReadAll
  fFile.Close
  oFSO.DeleteFile (sTempFile)

  If InStr(lcase(sResults), "failed") Then
    ServiceControl = False
  Else
    ServiceControl = True
  End If
End Function
Send this to:                          

Comments

said...

Thanks for the Function!!

You might want to add:

sService = chr(34) & sService & chr(34)

to cover a service with more then one word.

Examples: Symantec AntiVirus

5/6/2009 7:45:01 AM

Name
URL
Email
Email address is not published
Remember Me
Comments

CAPTCHA
Write the characters in the image above