By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,557 Members | 1,184 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,557 IT Pros & Developers. It's quick & easy.

starting disabled service VBS/WMI

P: 3
Hi, I need some help, i don't know much about programming other than executing them get getting them from scripting sites. My program is it won't start a service if the startup type is Disabled. it will try and stop a service that is disabled and will display msg "Error stopping service." if i run script on a service that is already running, it stop then start the service and restarts it again. Thanks whoever has the time to help me.

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. ' ------ SCRIPT CONFIGURATION ------
  4. Dim strComputer : strComputer = "."
  5. Dim strSvcName : strSvcName = "Alerter"
  6. ' ------ END CONFIGURATION ---------
  7.  
  8. Dim objWMI : set objWMI = GetObject("winmgmts:\\" & strComputer & _
  9. "\root\cimv2")
  10. Dim objService: set objService = objWMI.Get("Win32_Service.Name='" & _
  11. strSvcName & "'")
  12.  
  13. WScript.Echo "Restarting " & objService.Name & "..."
  14. RecursiveServiceStop objService
  15. RecursiveServiceStart objService
  16. WScript.Echo "Successfully restarted service"
  17.  
  18. Function RecursiveServiceStop ( objSvc ) 
  19.  
  20. Dim colServices : set colServices = objWMI.ExecQuery("Associators of " _
  21. & "{Win32_Service.Name='" & objSvc.Name & "'} Where " _
  22. & "AssocClass=Win32_DependentService Role=Antecedent" )
  23.  
  24. Dim objS
  25. for each objS in colServices
  26. RecursiveServiceStop objS
  27. next
  28.  
  29. Dim intRC : intRC = objSvc.StopService
  30. if intRC > 0 then
  31. WScript.Echo " Error stopping service: " & objSvc.Name
  32. WScript.Quit
  33. else
  34. WScript.Echo " Successfully stopped service: " & objSvc.Name
  35. end if
  36. End Function
  37.  
  38. Function RecursiveServiceStart ( objSvc )
  39.  
  40. Dim intRC : intRC = objSvc.StartService
  41. if intRC > 0 then
  42. WScript.Echo " Error starting service: " & objSvc.Name
  43. WScript.Quit
  44. else
  45. WScript.Echo " Successfully started service: " & objSvc.Name
  46. end if
  47.  
  48. Dim colServices : set colServices = objWMI.ExecQuery("Associators of " _
  49. & "{Win32_Service.Name='" & objSvc.Name & "'} Where " _
  50. & "AssocClass=Win32_DependentService Role=Antecedent" )
  51.  
  52. Dim objS
  53. for each objS in colServices
  54. RecursiveServiceStart objS
  55. next
  56.  
  57. End Function
Nov 18 '06 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.