Script Center >
Scripting Forums
>
The Official Scripting Guys Forum!
>
Stopping multiples services using vbs
Stopping multiples services using vbs
- Hi there,
I have to change a Registry key in a couple of Exchange servers 2003 and we need restart three services after that.
About the Registry changes that's ok, but I have a script just to restart only one service and I appreciate if
someone could help me to edit this script or create a new one to reach this aim.
Follow below my script. All help is welcome.
Option ExplicitDim objWMIService, objItem, objService
Dim colListOfServices, strComputer, strService, intSleep
strComputer = "."
intSleep = 15000
strService = " 'smtpsvc' "Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery _
("Select * from Win32_Service Where Name ="_
& strService & " ")
For Each objService in colListOfServices
objService.StopService()
WSCript.Sleep intSleep
objService.StartService()
Next
WScript.Echo "Your "& strService & " service has Started"
WScript.Quit
The other services are:
- 'Microsoft Exchange Routing Engine'
- 'Microsoft Exchange MTA Stacks'
Thanks,
Marcelo.
Marcelo Braga -- Um discipulo de Jesus -- MCT/ MCSA/ MCITP/ MCTS Exchange 2007.
Answers
- My apologies, I did not actually test the script before posting. Here's a modified version; please try and feedback.
1 Option Explicit 2 Dim objWMIService, objItem, objService, nCounter 3 Dim colListOfServices, strComputer, intSleep 4 strComputer = "." 5 intSleep = 15000 6 7 Dim strService(3) 8 strService(1) = " 'smtpsvc' " 9 strService(2) = " 'Microsoft Exchange Routing Engine' " 10 strService(3) = " 'Microsoft Exchange MTA Stacks' " 11 12 Set objWMIService = GetObject("winmgmts:" _ 13 & "{impersonationLevel=impersonate}!\\" _ 14 & strComputer & "\root\cimv2") 15 16 For nCounter = 1 to 3 17 Set colListOfServices = objWMIService.ExecQuery _ 18 ("Select * from Win32_Service Where Name ="_ 19 & strService(nCounter) & " ") 20 21 For Each objService in colListOfServices 22 objService.StopService() 23 WSCript.Sleep intSleep 24 objService.StartService() 25 WScript.Echo "Your "& strService(nCounter) & " service has Started" 26 Next 27 Next 28 29
Regards,
Salvador Manaois III
C|EH MCSE MCSA MCITP | Enterprise & Server Admin
Bytes & Badz : http://badzmanaois.blogspot.com- Marked As Answer byMarcelo Braga - EUA Friday, February 13, 2009 6:01 PM
All Replies
- Hola Marcelo,
You can actually store the three services in an array as shown:
1 Option Explicit 2 Dim objWMIService, objItem, objService, nCounter 3 Dim colListOfServices, strComputer, strService, intSleep 4 strComputer = "." 5 intSleep = 15000 6 7 Dim strService(3) 8 strService(1) = " 'smtpsvc' " 9 strService(2) = " 'Microsoft Exchange Routing Engine' " 10 strService(3) = " 'Microsoft Exchange MTA Stacks' " 11 12 Set objWMIService = GetObject("winmgmts:" _ 13 & "{impersonationLevel=impersonate}!\\" _ 14 & strComputer & "\root\cimv2") 15 16 For nCounter = 1 to 3 17 Set colListOfServices = objWMIService.ExecQuery _ 18 ("Select * from Win32_Service Where Name ="_ 19 & strService(nCounter) & " ") 20 21 For Each objService in colListOfServices 22 objService.StopService() 23 WSCript.Sleep intSleep 24 objService.StartService() 25 Next 26 WScript.Echo "Your "& strService(nCounter) & " service has Started" 27 Next 28 29 WScript.Quit
Similarly, you can store a list of these 3 services on a text file and read off this using FileSystemObject for your parameters.
Regards,
Salvador Manaois III
C|EH MCSE MCSA MCITP | Enterprise & Server Admin
Bytes & Badz : http://badzmanaois.blogspot.com - Thank Salvador,
But there is a error msg:
Line: 7
Char: 5
Error: Named redefined
Could you check it?
Thanks again,
Marcelo.
Marcelo Braga -- Um discipulo de Jesus -- MCT/ MCSA/ MCITP/ MCTS Exchange 2007. - Maybe we have to change the variable type on line 3, right?
Marcelo Braga -- Um discipulo de Jesus -- MCT/ MCSA/ MCITP/ MCTS Exchange 2007. - Yes Salvador, the problem was in the 3rd line. I've just removed the strService from it and it works.
But, when I run the scripts, only the first service is stopping.
The message appears for each service, but only the first service is stopping.
Do you have any idea what is happening? Could you make a test your machine,
using other services?
Thanks a lot.
Marcelo.
Marcelo Braga -- Um discipulo de Jesus -- MCT/ MCSA/ MCITP/ MCTS Exchange 2007. - My apologies, I did not actually test the script before posting. Here's a modified version; please try and feedback.
1 Option Explicit 2 Dim objWMIService, objItem, objService, nCounter 3 Dim colListOfServices, strComputer, intSleep 4 strComputer = "." 5 intSleep = 15000 6 7 Dim strService(3) 8 strService(1) = " 'smtpsvc' " 9 strService(2) = " 'Microsoft Exchange Routing Engine' " 10 strService(3) = " 'Microsoft Exchange MTA Stacks' " 11 12 Set objWMIService = GetObject("winmgmts:" _ 13 & "{impersonationLevel=impersonate}!\\" _ 14 & strComputer & "\root\cimv2") 15 16 For nCounter = 1 to 3 17 Set colListOfServices = objWMIService.ExecQuery _ 18 ("Select * from Win32_Service Where Name ="_ 19 & strService(nCounter) & " ") 20 21 For Each objService in colListOfServices 22 objService.StopService() 23 WSCript.Sleep intSleep 24 objService.StartService() 25 WScript.Echo "Your "& strService(nCounter) & " service has Started" 26 Next 27 Next 28 29
Regards,
Salvador Manaois III
C|EH MCSE MCSA MCITP | Enterprise & Server Admin
Bytes & Badz : http://badzmanaois.blogspot.com- Marked As Answer byMarcelo Braga - EUA Friday, February 13, 2009 6:01 PM
- Hi salvador,
but it is not working properly yet...
The same thing happens... Only the first service is stopping and restarting.
I'm trying it with Spooler and PlugPlay services in my XP machine...
Could you test it again and check if these services are, in fact, stopping and restarting?
once again, thanks,
Marcelo.
Marcelo Braga -- Um discipulo de Jesus -- MCT/ MCSA/ MCITP/ MCTS Exchange 2007. - Hi Marcelo,
You have to use the exact names of the services you need to stop as they are stored in Win32_Service.Name property. 'Microsoft Exchange Routing Engine' and 'Microsoft Exchange MTA Stacks' are probably values of Win32_Service.Caption or Win32_Service.DisplayName properties. This script will list all Win32_Service.Name values on a computer:
strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & strComputer & "\root\cimv2") Set colServices = objWMIService.ExecQuery _ ("Select * From Win32_Service") For Each objService In colServices WScript.Echo objService.Name Next
For the services you want to stop use the names exactly as they appear on the list, those are the values you need to put in your original script.
urkec - Thank you guys,
The script is working fine...
Cheers,
Marcelo.
Marcelo Braga -- Um discipulo de Jesus -- MCT/ MCSA/ MCITP/ MCTS Exchange 2007.

