I've combined a script that allows me to remotely change the service status to manual and start the service and all dependancies:
strComputer = "computername"Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!" & strComputer & "rootcimv2")Set colServiceList = objWMIService.ExecQuery _ ("Select * from Win32_Service where Name='TermService'")For each objService in colServiceList errReturnCode = objService.Change( , , , , "Manual") errReturn = objService.StartService() NextWscript.Sleep 20000Set colServiceList = objWMIService.ExecQuery("Associators of " _ & "{Win32_Service.Name='TermService'} Where " _ & "AssocClass=Win32_DependentService " & "Role=Dependent" )For each objService in colServiceList objService.StartService()Next
I need to make a script on this example that would do the same but for 30 servers. I can of course write it 30 times but isn't there an easier way?
Thanks!
Start one service on multiple servers
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Start one service on multiple servers
I've combined a script that allows me to remotely change the service status to manual and start the service and all dependancies:
strComputer = "computername"Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!" & strComputer & "rootcimv2")Set colServiceList = objWMIService.ExecQuery _ ("Select * from Win32_Service where Name='TermService'")For each objService in colServiceList errReturnCode = objService.Change( , , , , "Manual") errReturn = objService.StartService() NextWscript.Sleep 20000Set colServiceList = objWMIService.ExecQuery("Associators of " _ & "{Win32_Service.Name='TermService'} Where " _ & "AssocClass=Win32_DependentService " & "Role=Dependent" )For each objService in colServiceList objService.StartService()Next
I need to make a script on this example that would do the same but for 30 servers. I can of course write it 30 times but isn't there an easier way?
Thanks!
strComputer = "computername"Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!" & strComputer & "rootcimv2")Set colServiceList = objWMIService.ExecQuery _ ("Select * from Win32_Service where Name='TermService'")For each objService in colServiceList errReturnCode = objService.Change( , , , , "Manual") errReturn = objService.StartService() NextWscript.Sleep 20000Set colServiceList = objWMIService.ExecQuery("Associators of " _ & "{Win32_Service.Name='TermService'} Where " _ & "AssocClass=Win32_DependentService " & "Role=Dependent" )For each objService in colServiceList objService.StartService()Next
I need to make a script on this example that would do the same but for 30 servers. I can of course write it 30 times but isn't there an easier way?
Thanks!
Start one service on multiple servers
Here is how to do this in PowerShell which is much easier and more reliable.
The plus here is that 'Start-Service' waits for the service to start before continuing.
We can put in a filter that will check to see if the service actually started before moving on to the dependend services.
While this can be done with vbscript it is a bit trickier to accomplish smoothly.
jvierra2010-10-02 14:14:06
Code: Select all
$computers = Get-Content servers.txt
$computers | ForEach-Object{
get-service termservice -computer $_ |
set-service -startuptype Manual -computer $_ |{
$_.Start()
$_.WaitForStatus("Running")
$_
} |
get-service -dependent -computer $_ | ForEach-Object{
$_.Start()
$_.WaitForStatus("Running")
$_
}
}
The plus here is that 'Start-Service' waits for the service to start before continuing.
We can put in a filter that will check to see if the service actually started before moving on to the dependend services.
While this can be done with vbscript it is a bit trickier to accomplish smoothly.
jvierra2010-10-02 14:14:06
Start one service on multiple servers
Here is how we read a file in VBScript
Code: Select all
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("servers.txt")
While Not file.AtEndOfStrem
server = file.ReadLine()
StartService server
Wend