Sub remoteService(sstrComputer,sstrService,sstrAction)
'This accepts a computer name, service name, and action to complete on the service.
'Input: sstrComputer = Machine to perform action on. Use "." for local
'Input: sstrService = Name of the service
'Input: sstrAction = See options below
' "Start" = Start the service
' "Stop" = Stop the service
' "Restart" = Restart the service
' "Pause" = Pause the service
' "Resume" = Resume the service
' "Automatic" = Set the startup type to Automatic
' "Manual" = Set the startup type to Manual
' "Disabled" = Set the startup type to Disabled
'To get the status codes:
'1. Declare strServiceStatus and blnServiceStatus globally (Main part of the script)
'2. Call this sub as usual.
'3. blnServiceStatus will be set to True if successfull.
'4. strServiceStatus will be set to the status of the action
On Error Resume Next
Set sobjWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & sstrComputer & "\root\cimv2")
Set scolServiceList = sobjWMIService.ExecQuery("Select * from Win32_Service where Name='" & sstrService &"'")
'Set colServiceList = objWMIService.ExecQuery("Select * from Win32_Service")
For Each sobjService in scolServiceList
'WScript.Echo sobjService.Name
sstrAction = UCase(sstrAction)
Select Case sstrAction
Case "START"
serrReturn = sobjService.StartService()
Case "STOP"
serrReturn = sobjService.StopService()
Case "RESTART"
serrReturn = sobjService.StopService()
serrReturn = sobjService.StartService()
Case "PAUSE"
serrReturn = sobjService.PauseService()
Case "RESUME"
serrReturn = sobjService.ResumeService()
Case "AUTOMATIC"
serrReturn = sobjService.ChangeStartMode("Automatic")
Case "MANUAL"
serrReturn = sobjService.ChangeStartMode("Manual")
Case "DISABLED"
serrReturn = sobjService.ChangeStartMode("Disabled")
End Select
'WScript.Echo serrReturn
Select Case serrReturn
Case 0 'Success
blnServiceStatus = True
strServiceStatus = "Success"
Case 1 'Not Supported
blnServiceStatus = False
strServiceStatus = "Not Supported"
Case 2 'Access Denied
blnServiceStatus = False
strServiceStatus = "Access Denied"
Case 3 'Dependent Services Running
blnServiceStatus = True
strServiceStatus = "Dependent Services Running"
Case 4 'Invalid Service Control
blnServiceStatus = False
strServiceStatus = "Invalid Service Control"
Case 5 'Service Cannot Accept Control
blnServiceStatus = False
strServiceStatus = "Service Cannot Accept Control"
Case 6 'Service Not Active
blnServiceStatus = False
strServiceStatus = "Service Not Active"
Case 7 'Service Request Timeout
blnServiceStatus = False
strServiceStatus = "Service Request Timeout"
Case 8 'Unknown Failure
blnServiceStatus = False
strServiceStatus = "Unknown Failure"
Case 9 'Path Not Found
blnServiceStatus = False
strServiceStatus = "Path Not Found"
Case 10 'Service Already Running
blnServiceStatus = True
strServiceStatus = "Service Already Running"
Case 11 'Service Database Locked
blnServiceStatus = False
strServiceStatus = "Service Database Locked"
Case 12 'Service Dependency Deleted
blnServiceStatus = True
strServiceStatus = "Service Dependency Deleted"
Case 13 'Service Dependency Failure
blnServiceStatus = False
strServiceStatus = "Service Dependency Failure"
Case 14 'Service Disabled
blnServiceStatus = True
strServiceStatus = "Service Disabled"
Case 15 'Service Logon Failure
blnServiceStatus = False
strServiceStatus = "Service Logon Failure"
Case 16 'Service Marked For Deletion
blnServiceStatus = True
strServiceStatus = "Service Marked For Deletion"
Case 17 'Service No Thread
blnServiceStatus = False
strServiceStatus = "Service No Thread"
Case 18 'Status Circular Dependency
blnServiceStatus = False
strServiceStatus = "Circular Dependency"
Case 19 'Status Duplicate Name
blnServiceStatus = False
strServiceStatus = "Duplicate Name"
Case 20 'Status Invalid Name
blnServiceStatus = False
strServiceStatus = "Invalid Name"
Case 21 'Status Invalid Parameter
blnServiceStatus = False
strServiceStatus = "Invalid Paramenter"
Case 22 'Status Invalid Service Account
blnServiceStatus = False
strServiceStatus = "Invalid Service Account"
Case 23 'Status Service Exists
blnServiceStatus = True
strServiceStatus = "Service Exists"
Case 24 'Service Already Paused
blnServiceStatus = True
strServiceStatus = "Already Paused"
End Select
Next
End Sub