Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stopping service via WMI

Status
Not open for further replies.

bmquiroz

IS-IT--Management
Joined
Sep 26, 2003
Messages
207
Location
US
I am using the following code to stop Exchange services. What I would like to do is have the script stop the service, wait until it's completely stopped, then go to the next service and so forth. Can someone help?

Thanks!

sComputer = "."
serviceName = "Microsoft Exchange Management"
Set oWMI = GetObject("WinMgmts:{impersonationLevel=Impersonate}!//" & sComputer & "/root/cimv2")
Set colServices = oWMI.execQuery("SELECT * FROM Win32_Service Where displayname = '" & serviceName & "'")
For Each oService In colServices
errReturn = oService.StopService()
WScript.Echo "Stopping " & serviceName & " service.."
Next

sComputer = "."
serviceName = "Microsoft Exchange Routing Engine"
Set oWMI = GetObject("WinMgmts:{impersonationLevel=Impersonate}!//" & sComputer & "/root/cimv2")
Set colServices = oWMI.execQuery("SELECT * FROM Win32_Service Where displayname = '" & serviceName & "'")
For Each oService In colServices
errReturn = oService.StopService()
WScript.Echo "Stopping " & serviceName & " service.."
Next
 
Hello SIPIN,

Are you saying that it is working like one oService has not fully stopped before the second oService starts stopping?

regards - tsuji
 
Yes, anyway to have one completely stop before starting the next?

Thanks!
 
SAPIN,

Stopped or not, the wmi service has done its due. It is not a matter of "waiting", it is a matter of success/failure. Echo out the errReturn. It show you the reason why. If it is not equal to zero, it fails one reason or another. Check the documentation for interpretation.

- tsuji
 
That's what I figured, just wanted to see if there was a way to tail the process until it finishes. Alternately, I guess I could run "net stop" under a shell but I wanted to see if there was a way using WMI. I'll keep searching.

Thanks!
 
>just wanted to see if there was a way to tail the process until it finishes
It does as far as .stopservice is concerned.

- tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top