
Manage NT services with WMI/WBEM
Quote:
> Hello,
> I try to write a VB script to manage services on NT 4 (SP5) via WMI/WBEM. I
> can list services and get their current status, i.e. stopped or running.
> However, stopping or starting a service fails. Here is an example how I try
> it:
> set
> svc=GetObject("winmgmts:{impersonateLevel=impersonate}").ExecQuery("select *
> from Win32_Services where Name='SNMP'")
> A svc.Count correctly returns: 1
> If I call: svc.StopService()
> I get return code 3 and the service still runs.
> I also would like to be able to manage services remotely, with something
> like:
> set
> svc=GetObject("winmgmts:{impersonateLevel=impersonate}!//f2411bxs").ExecQuer
> y("select * from Win32_Services where Name='SNMP'")
> f2411bxs is the name of the remote host.
> I have WMI 1.1 core and SDK on all systems involved installed.
> Any info or tips would be appreciated.
> Thanks,
> Bernd
Here is a sample of starting and stopping..
Dim ServerName
Dim ServiceSet
Dim Service
Dim svcState
ServerName = InputBox("Enter the NetBIOS Name of the Server you would_
like")
Set ServiceSet = GetObject("winmgmts:
{impersonationLevel=impersonate}!//" & servername).ExecQuery("select _
* from Win32_Service")
For each Service in ServiceSet
WScript.Echo Service.Description & " is " & Service.State
If Service.State = "Running" Then 'Wscript.Echo Service.State
svcState = InputBox("Would you like to stop this service? " & (Chr(13)_
& Chr(10)) & "Yes or No" & (Chr(13) & Chr(10)) & "Quit to Exit","Stop_
Service: " & Service.Description,"No")
If svcState = "Yes" Then
Wscript.Echo "Stopping " & Service.Description
Service.StopService()
ElseIf svcState = "Quit" Then
Exit For
Else
End If
End If
Next