' Name: StartStopServices ' Author: Egil Hansen - www.egil.dk ' Date: 2007.03.18 ' Description: This script can be used to start/stop a number ' of services and their dependtents, in a toggle ' start/stop fashion. ' It also allows you to specify a explicit action, ' either start or stop. ' ' Usage: Simply add the services to the array arrServices below, ' in the order you want the services to be stopped/started. ' Option Explicit ' Add services Dim arrServices(2) arrServices(0) = "IISADMIN" arrServices(1) = "MSSQLSERVER" arrServices(2) = "msftesql" ' Declare variables Dim strService, objIntExplorer ' Look for explicit requests from user If ExplicitStartStop() => 0 Then ' Start IE window Set objIntExplorer = Wscript.CreateObject("InternetExplorer.Application") Call SetupIE(objIntExplorer) Select Case ExplicitStartStop() Case 0 ' Iterate over each service, decide what to do based on their current status For Each strService in arrServices If IsServiceRunning(strService) Then StopServiceAndDependents(strService) Else StartServiceAndDependents(strService) End If Next Case 1 ' Iterate over each service and stop them For Each strService in arrServices StopServiceAndDependents(strService) Next Case 2 ' Iterate over each service and start them For Each strService in arrServices StartServiceAndDependents(strService) Next End Select ' Close IE window Call CloseIE(objIntExplorer) End If ' Exit Wscript.Quit(0) ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' End of main script section, helper functions below ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private Sub PrintLn(str) On Error Resume Next objIntExplorer.Document.WriteLn (str & "
") End Sub '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Set up Internet Explorer for use as a status message window '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private Sub SetupIE(objIntExplorer) Dim strTitle strTitle = "Start/Stop services scriptt" ' Create reference to objIntExplorer ' This will be used for the user messages. Also set IE display attributes With objIntExplorer .Navigate "about:blank" .ToolBar = 0 .Menubar = 0 .StatusBar = 0 .Width = 500 .Height = 250 .Left = 50 .Top = 20 End With ' Set some formating With objIntExplorer.Document .WriteLn ("") .WriteLn ("") .WriteLn ("" & strTitle & "") .WriteLn ("") .WriteLn ("") .WriteLn ("") .WriteLn ("
") End With ' Wait for IE to finish Do While (objIntExplorer.Busy) Wscript.Sleep 100 Loop ' Show IE objIntExplorer.Visible = 1 End Sub Private Sub CloseIE(objIntExplorer) ' Print end text Call PrintLn ("") Call PrintLn ("(closing window in 5 seconds)") ' Write standard html end-tags Call PrintLn ("
") Call PrintLn ("") Call PrintLn ("") ' Sleep for x seconds Wscript.Sleep (5000) On Error Resume Next objIntExplorer.Quit() ' Clean up Set objIntExplorer = Nothing End Sub ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Looks at possible arguments, start/stop/? ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private Function ExplicitStartStop() Dim objArgs, intC, blnQuit, strCommandLine Set objArgs = WScript.Arguments ' Set default status ExplicitStartStop = 0 ' Check the command line for arguments For intC = 0 To objArgs.Count - 1 strCommandLine = objArgs(intC) Select Case LCase(strCommandLine) Case "/stop" ExplicitStartStop = 1 Case "/start" ExplicitStartStop = 2 Case "/?" MsgBox "Start/Stop services script" & vbCRLF & vbCRLF & _ " Example: StartStopServices.vbs /start" & vbCRLF & vbCRLF & _ " /start" & vbCRLF & _ " Force a start of the services, regardless of their current status" & vbCRLF & _ " /stop" & vbCRLF & _ " Force a stop of the services, regardless of their current status" & vbCRLF & _ " /?" & vbCRLF & _ " This help screen" & vbCRLF & vbCRLF ExplicitStartStop = -1 End Select Next ' Clean up Set objArgs = Nothing End Function ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Starts a Service and Its Dependents ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private Function StartServiceAndDependents(strServiceName) Dim strComputer, objWMIService, colServiceList, objService ' Connect to local host strComputer = "." ' Connect to WMI interface on computer Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") ' Select the specified service Set colServiceList = objWMIService.ExecQuery("Select * from Win32_Service where Name='" & strServiceName & "'") ' Start the service For each objService in colServiceList Wscript.Echo "Starting " & objService.DisplayName Call PrintLn("Starting " & objService.DisplayName) StartServiceAndDependents = objService.StartService() Next ' Wait a bit Wscript.Sleep 20000 ' Get list of all dependent services Set colServiceList = objWMIService.ExecQuery("Associators of {Win32_Service.Name='" & strServiceName & "'} Where AssocClass=Win32_DependentService Role=Antecedent") ' Iterate over each service For each objService in colServiceList Wscript.Echo "Starting " & objService.DisplayName Call PrintLn("Starting " & objService.DisplayName) objService.StartService() Next ' Clean up Set objWMIService = Nothing Set colServiceList = Nothing Set objService = Nothing End Function ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Stops a Service and Its Dependents ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private Function StopServiceAndDependents(strServiceName) Dim strComputer, objWMIService, colServiceList, objService ' Connect to local host strComputer = "." ' Connect to WMI interface on computer Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") ' Get list of all dependent services Set colServiceList = objWMIService.ExecQuery("Associators of {Win32_Service.Name='" & strServiceName & "'} Where AssocClass=Win32_DependentService Role=Antecedent") ' Iterate over each service For each objService in colServiceList Wscript.Echo "Stopping " & objService.DisplayName Call PrintLn("Stopping " & objService.DisplayName) objService.StopService() Next ' Wait a bit before continuing Wscript.Sleep 20000 ' Select the specified service Set colServiceList = objWMIService.ExecQuery("Select * from Win32_Service where Name='" & strServiceName & "'") ' Stop the service For each objService in colServiceList Wscript.Echo "Stopping " & objService.DisplayName Call PrintLn("Stopping " & objService.DisplayName) StopServiceAndDependents = objService.StopService() Next ' Clean up Set objWMIService = Nothing Set colServiceList = Nothing Set objService = Nothing End Function ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Retriev the status of the service ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private Function IsServiceRunning(strServiceName) Dim strComputer, objWMIService, colRunningServices, objService ' Connect to local host strComputer = "." ' Connect to WMI interface on computer Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") ' Select the specified service Set colRunningServices = objWMIService.ExecQuery("Select * from Win32_Service where Name='" & strServiceName & "'") ' Read the services state For Each objService in colRunningServices Wscript.Echo objService.DisplayName & VbTab & objService.State Call PrintLn("" & objService.DisplayName & ": " & objService.State & "") If objService.State = "Running" Then IsServiceRunning = true Else IsServiceRunning = false End If Next ' Clean up Set objWMIService = Nothing Set colRunningServices = Nothing Set objService = Nothing End Function