If you want to restart a failed service you do not need to run a script. In the services MMC snapin right click on a service, select properties, click the recovery tab. Here you can set what actions you want taken should the service stop. There is alot of flexibility available. You will need a script if y ou are trying to stop the service , do something then start the script, preface the batch file with net stop "myserviceshortname" and end with net start "myserviceshortname"

In vbscipt it’s a little more code to stop a service and its’ dependants:

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\" & strComputer & "rootcimv2")
Set colServiceList = objWMIService.ExecQuery("Associators of " _
   & "{Win32_Service.Name="myservice"} Where " _
        & "AssocClass=Win32_DependentService " & "Role=Antecedent" )
For each objService in colServiceList
    objService.StopService()
Next
Wscript.Sleep 20000
Set colServiceList = objWMIService.ExecQuery _
        ("Select * from Win32_Service where Name="myservice"")
For each objService in colServiceList
    errReturn = objService.StopService()
Next

Here’s starting a service and anything it depends on (this should be familiar)

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\" & strComputer & "rootcimv2")
Set colServiceList = objWMIService.ExecQuery _
    ("Select * from Win32_Service where Name="Myservice"")
For each objService in colServiceList
    errReturn = objService.StartService()
Next
Wscript.Sleep 20000
Set colServiceList = objWMIService.ExecQuery("Associators of " _
   & "{Win32_Service.Name="myservice"} Where " _
        & "AssocClass=Win32_DependentService " & "Role=Dependent" )
For each objService in colServiceList
    objService.StartService()
Next

Leave a Reply

Your email address will not be published. Required fields are marked *