Do you like this article? Please, rate it and write review!
Rated:
by Aspin.com users
| |
| | Top messages |
| 4.5.2002 9:16:43 | |
| 22.3.2003 19:18:41 | |
| 3.1.2001 13:00:48 | |
Stop service using WMI/VBScript | Areas>WMI Areas>Languages>VBScript | |
This simple WMI script can stop Windows service on local or remote computer using WMI admin objects.
The script cannot stop service depending on another service, for example you cannot stop IISADMIN when W3SVC is running,
you have to stop both services, depending service second
'stop the service on which IISADMIN depends
StopService ".", "W3SVC", True
StopService ".", "HTTPFilter", True
'stop the depending service
StopService ".", "IISADMIN", True
|
StopService parameters
- Computer - name of the computer to stop service. Use "." or "localhost" for local computer.
- ServiceName - Name of the service (not a display name, but internal name)
- Wait - Bolean - wait for service to stop
StopService ".", "SQLSERVERAGENT", True
StopService ".", "MSSQLSERVER", True
Sub StopService(Computer, ServiceName, Wait)
Dim cimv2, oService, Result
'Get the WMI administration object
Set cimv2 = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
Computer & "\root\cimv2")
'Get the service object
Set oService = cimv2.Get("Win32_Service.Name='" & ServiceName & "'")
'Check base properties
If Not oService.Started Then
' the service is Not started
wscript.echo "The service " & ServiceName & " is Not started"
exit Sub
End If
If Not oService.AcceptStop Then
' the service does Not accept stop command
wscript.echo "The service " & ServiceName & " does Not accept stop command"
exit Sub
End If
'wscript.echo oService.getobjecttext_
'Stop the service
Result = oService.StopService
If 0 <> Result Then
wscript.echo "Stop " & ServiceName & " error: " & Result
exit Sub
End If
Do While oService.Started And Wait
'get the current service state
Set oService = cimv2.Get("Win32_Service.Name='" & ServiceName & "'")
wscript.echo now, "StopService", ServiceName, oService.Started, _
oService.State, oService.Status
Wscript.Sleep 200
Loop
End Sub
|
|
|
If you like this page, please include next link on your pages:
<A
Href="http://www.motobit.com/tips/detpg_vbs-wmi-stop-service/"
Title="Simple script to stop service on
local or remote computer."
>Stop service using WMI/VBScript</A>
|
|