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 | |
Start service using WMI/VBScript | Areas>WMI Areas>Languages>VBScript | |
This simple WMI script can start Windows service on local or remote computer using WMI admin objects.
StartService parameters
- Computer - name of the computer to start 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 start
StartService ".", "MSSQLSERVER", True
StartService ".", "SQLSERVERAGENT", True
Sub StartService(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 & "'")
'Path = "winmgmts:{impersonationLevel=impersonate}!\\" & Computer & _
' "\root\cimv2:Win32_Service.Name='" & ServiceName & "'"
'Get the WMI administration object of the service
'Set oService = GetObject(Path)
'Check base properties
If oService.Started Then
' the service is Not started
wscript.echo "The service " & ServiceName & " is started."
exit Sub
End If
'Start the service
Result = oService.StartService
If 0 <> Result Then
wscript.echo "Start " & ServiceName & " error:" & Result
exit Sub
End If
Do While InStr(1,oService.State,"running",1) = 0 And Wait
'get the current service state
Set oService = cimv2.Get("Win32_Service.Name='" & ServiceName & "'")
wscript.echo now, "StartService", 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-start-service/"
Title="Simple script to start service on
local or remote computer."
>Start service using WMI/VBScript</A>
|
|