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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HTA VBS Restart service

Status
Not open for further replies.

scottohum

IS-IT--Management
Jul 5, 2011
14
GB
i am using the following script in a HTA to restart a service. But it will only stop the service and wont start it again. When i try it as an individual vbs it restarts the service no problem but once i put it into the HTA script it will only stop the service. Can someone please help??

'Stop ServicestrServiceName = "if2kd"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery("Select * from Win32_Service Where Name ='" & strServiceName & "'")
For Each objService in colListOfServices
objService.StopService()
Next
wscript.sleep 3000
'Start ServicestrServiceName = "if2kd"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service Where Name ='" & strServiceName & "'")
For Each objService in colListOfServices
objService.StartService()
Next
 
If they've disappeared (been stopped), how can they be discovered again? Consolidate to eliminate redundancy and to maintain your objects

Code:
'Restart ServicestrServiceName = "if2kd"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service Where Name ='" & strServiceName & "'")

For Each objService in colListOfServices
   objService.StopService()
   wscript.sleep 3000
   objService.StartService()
Next

-Geates

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
3000 seconds is like saying 'how long is a piece of string'
your startservice() call will fail under circumstances where the service is still in the process of 'being stopped'.
if you wish to use the WMI methods then you really should take the trouble to sit and wait till the service you are interested in has really been stopped, before you can attempt to start it again. this is what you would have to do if you used the windows GUI

For i = 1 To 20 'wait for 20 seconds in total? perhaps longer is bettter
If Service.Stopped = True Then
Wscript.Echo "service is stopped, can try and start it"
Service.Start()
Else
Wscript.Sleep 1000
Wscript.Echo "sleeping 1 sec as service is still not stopped " & CStr(i)
End If
Next

I Hear, I Forget
I See, I Remember
I Do, I Understand

Ronald McDonald
 
sorry, you may want to Exit For after the Service.Start()
the code is just an example, of course Service is not a valid object nor are the methods / properties i suggest

I Hear, I Forget
I See, I Remember
I Do, I Understand

Ronald McDonald
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top