Connecting Tech Pros Worldwide Forums | Help | Site Map

starting disabled service VBS/WMI

Newbie
 
Join Date: Nov 2006
Location: Aus
Posts: 3
#1: Nov 18 '06
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.

Option Explicit

' ------ SCRIPT CONFIGURATION ------
Dim strComputer : strComputer = "."
Dim strSvcName : strSvcName = "Alerter"
' ------ END CONFIGURATION ---------

Dim objWMI : set objWMI = GetObject("winmgmts:\\" & strComputer & _
"\root\cimv2")
Dim objService: set objService = objWMI.Get("Win32_Service.Name='" & _
strSvcName & "'")

WScript.Echo "Restarting " & objService.Name & "..."
RecursiveServiceStop objService
RecursiveServiceStart objService
WScript.Echo "Successfully restarted service"

Function RecursiveServiceStop ( objSvc )

Dim colServices : set colServices = objWMI.ExecQuery("Associators of " _
& "{Win32_Service.Name='" & objSvc.Name & "'} Where " _
& "AssocClass=Win32_DependentService Role=Antecedent" )

Dim objS
for each objS in colServices
RecursiveServiceStop objS
next

Dim intRC : intRC = objSvc.StopService
if intRC > 0 then
WScript.Echo " Error stopping service: " & objSvc.Name
WScript.Quit
else
WScript.Echo " Successfully stopped service: " & objSvc.Name
end if
End Function

Function RecursiveServiceStart ( objSvc )

Dim intRC : intRC = objSvc.StartService
if intRC > 0 then
WScript.Echo " Error starting service: " & objSvc.Name
WScript.Quit
else
WScript.Echo " Successfully started service: " & objSvc.Name
end if

Dim colServices : set colServices = objWMI.ExecQuery("Associators of " _
& "{Win32_Service.Name='" & objSvc.Name & "'} Where " _
& "AssocClass=Win32_DependentService Role=Antecedent" )

Dim objS
for each objS in colServices
RecursiveServiceStart objS
next

End Function

bartonc's Avatar
Moderator
 
Join Date: Sep 2006
Location: Minden, Nevada, USA
Posts: 6,400
#2: Nov 18 '06

re: starting disabled service VBS/WMI


Hello, and welcome.

Two thinks will help you a lot:
1) Use code tags in your post.
2) Post your question in the VB forum at http://www.thescripts.com/forum/forum132.html

Thanks for posting - keep it up!
Newbie
 
Join Date: Nov 2006
Location: Aus
Posts: 3
#3: Nov 18 '06

re: starting disabled service VBS/WMI


Sorry, i just joined and i figured the script is also for administration. thanks
bartonc's Avatar
Moderator
 
Join Date: Sep 2006
Location: Minden, Nevada, USA
Posts: 6,400
#4: Nov 18 '06

re: starting disabled service VBS/WMI


Quote:

Originally Posted by Cenarius

Sorry, i just joined and i figured the script is also for administration. thanks

Thanks for keeping this thread up-to-date. I hope the VB people can help you.
Reply


Similar Microsoft Windows / Vista / XP / ME / 95 & 98 bytes