Start one service on multiple servers

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
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.
This topic is 13 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
hsadmin
Posts: 4
Last visit: Wed Oct 06, 2010 10:30 pm

Start one service on multiple servers

Post by hsadmin »

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!
User avatar
hsadmin
Posts: 4
Last visit: Wed Oct 06, 2010 10:30 pm

Start one service on multiple servers

Post by hsadmin »

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!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Start one service on multiple servers

Post by jvierra »

Get a collection of servers from either a file of Active Directory and call the function once for each server.


Just make your code into a function.

jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Start one service on multiple servers

Post by jvierra »

Here is how to do this in PowerShell which is much easier and more reliable.

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
User avatar
hsadmin
Posts: 4
Last visit: Wed Oct 06, 2010 10:30 pm

Start one service on multiple servers

Post by hsadmin »

But I can do it for one server easily with my script above.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Start one service on multiple servers

Post by jvierra »

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
	
User avatar
hsadmin
Posts: 4
Last visit: Wed Oct 06, 2010 10:30 pm

Start one service on multiple servers

Post by hsadmin »

Thanks for help.
This topic is 13 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked