Needed this for another script at work and couldnt find anything on the web that was complete and self-contained so I wrote this.
I forgot to mention that it checks for the service's state as well to ensure that it has indeed started/stopped
Usage:
service_control ".","IIS Admin","restart"
the arguments are commented in the sub
Sub service_control(strComputer,sname,purpose)
'strComputer is the computer you want to connect to
'sname is the service name
'purpose is whether or not you want to start, stop or restart a service
Dim delay, err_return
delay = 20000 '20 seconds
WScript.stdout.Write "*"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery _
("Select * from Win32_Service where DisplayName = '" & sname & "'")
For Each objService In colListOfServices
'Just to double check we have the right service
If objService.displayname = sname Then
WScript.stdout.Write "*"
If purpose = "stop" Or purpose = "restart" Then
err_return = objService.stopservice
'service has dependencies, so we need to stop those first
If err_return = 3 Then
'GOTCHA - you have to use the service.name NOT service.displayname for this query!!
'even if you change Win32_Service.Name to Win32_Service.DisplayName
Set colServiceList2 = objWMIService.ExecQuery("Associators of " _
& "{Win32_Service.Name='" & objService.name & "'} Where " _
& "AssocClass=Win32_DependentService " & "Role=Antecedent" )
For Each objService2 in colServiceList2
objService2.StopService()
If Not objService2.state = "Stopped" Then
'you have to pause because the service wont start unless it is completely stopped
WScript.Sleep delay
service_control strComputer,sname,"stop"
Else
'WScript.Echo objService2.displayname & " has been stopped."
End If
Next
'stop the original service that had dependencies
objService.stopservice
Else
If Not objService.state = "Stopped" Then
'you have to pause because the service wont start unless it is completely stopped
WScript.Sleep delay
service_control strComputer,sname,"stop"
Else
'WScript.Echo objService.displayname & " has been stopped."
End If
End If
'if restart was sent, start service after it has stopped
If purpose = "restart" Then service_control strComputer, sname, "start" End if
Elseif purpose = "start" Then
WScript.stdout.Write "*"
err_return = objService.startservice
If NOT err_return = 10 Then
'GOTCHA - you have to use the service.name NOT service.displayname for this query!!
'even if you change Win32_Service.Name to Win32_Service.DisplayName
Set colServiceList2 = objWMIService.ExecQuery("Associators of " _
& "{Win32_Service.Name='" & objService.name & "'} Where " _
& "AssocClass=Win32_DependentService " & "Role=Antecedent" )
For Each objService2 in colServiceList2
objService2.StartService()
If Not objService2.state = "Running" Then
'you have to pause because the service wont start unless it is completely stopped
WScript.Sleep delay
service_control strComputer,sname,"start"
Else
WScript.Echo objService.displayname & " has been started."
End If
Next
err_return = objService.startservice
'service has dependencies, so we need to start those first
If err_return = 3 Then
'start the original service that had dependencies
objService.startservice
'End If
If Not objService.state = "Running" then
'you have to pause because the service wont start unless it is completely stopped
WScript.Sleep delay
service_control strComputer,sname,"start"
Else
'WScript.Echo objService.displayname & " has been started."
End If
End If
End If
'WScript.Echo objService.displayname & " has been started."
End If
End If
Next
End Sub 'service_control
Code liberally borrowed from the scripting center
<message edited by kirrilian on Saturday, April 28, 2007 5:07 PM>
Have you searched [url="http://www.google.com"]here [/url]?
[url="http://tinyurl.com/as7xm"]VBScript Fundamentals[/url]
[url="http://kirrilian.dyndns.org/projects/code/"]My Site[/url]