Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Error trapping for Services running in XP

1 view
Skip to first unread message

Chris Sharp

unread,
Apr 21, 2003, 6:03:35 PM4/21/03
to
Is is possible when using WMI to locate, then stop a service, but first, do
a check whether the service exists or not? I couldn't find any examples at
MSoft's scriptcenter pages that specifically look for scripts. Thanks for
any help.

-CP Sharp


Torgeir Bakken (MVP)

unread,
Apr 21, 2003, 6:50:48 PM4/21/03
to
Chris Sharp wrote:

Hi

sComputer = "."
Set oWMI = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")

sService = "fax"

On Error Resume Next
Set oService = oWMI.Get("WIN32_Service.Name='" & sService & "'")

If Err.Number = 0 Then
On Error Goto 0
If LCase(oService.State) = "running" Then
oService.Stopservice
Do
WScript.Sleep 500 ' Pause 1/2 second.
' You need to reconnect to the service for each
' loop when testing on the state status
Set oService = oWMI.Get("WIN32_Service.Name='" & sService & "'")
Loop Until LCase(oService.State) = "stopped"
WScript.Echo "The service is now stopped"
Else
WScript.Echo "The service was already stopped"
End If
Else
WScript.Echo "The service does not exist"
End If


--
torgeir
Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of the 1328 page
Scripting Guide: http://www.microsoft.com/technet/scriptcenter


0 new messages