Script Center > Scripting Forums > The Official Scripting Guys Forum! > Stopping multiples services using vbs
Ask a questionAsk a question
 

AnswerStopping multiples services using vbs

  • Friday, February 13, 2009 5:16 AMMarcelo Braga - EUA Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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 Explicit

    Dim 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

  • Friday, February 13, 2009 6:19 AMSalvador Manaois IIIModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    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

All Replies

  • Friday, February 13, 2009 5:30 AMSalvador Manaois IIIModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    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
  • Friday, February 13, 2009 5:47 AMMarcelo Braga - EUA Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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.
  • Friday, February 13, 2009 5:49 AMMarcelo Braga - EUA Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Maybe we have to change the variable type on line 3, right?
    Marcelo Braga -- Um discipulo de Jesus -- MCT/ MCSA/ MCITP/ MCTS Exchange 2007.
  • Friday, February 13, 2009 6:12 AMMarcelo Braga - EUA Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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.
  • Friday, February 13, 2009 6:19 AMSalvador Manaois IIIModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    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
  • Friday, February 13, 2009 1:46 PMMarcelo Braga - EUA Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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.
  • Friday, February 13, 2009 3:02 PMurkecModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    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
  • Friday, February 13, 2009 6:02 PMMarcelo Braga - EUA Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thank you guys,
    The script is working fine...
    Cheers,
    Marcelo.

    Marcelo Braga -- Um discipulo de Jesus -- MCT/ MCSA/ MCITP/ MCTS Exchange 2007.