mbt masai
 
Welcome !
         

                                
After experiencing a lot of down time, We decided to move this site to CrystalTech.com. CrystalTech.com is powered by only the finest Windows servers providing the best performance, reliability, and value anywhere.

 Start, Stop, & Restart Services (Incl. dependencies)

Author Message
kirrilian

  • Total Posts : 629
  • Scores: 3
  • Reward points : 0
  • Joined: 3/15/2005
  • Location:
  • Status: offline
Start, Stop, & Restart Services (Incl. dependencies) Friday, April 27, 2007 6:42 AM (permalink)
0
Needed this for another script at work and couldnt find anything on the web that was complete and self-contained so I wrote this.
I forgot to mention that it checks for the service's state as well to ensure that it has indeed started/stopped

Usage:
service_control ".","IIS Admin","restart"
the arguments are commented in the sub

 Sub service_control(strComputer,sname,purpose)
    'strComputer is the computer you want to connect to
    'sname is the service name
    'purpose is whether or not you want to start, stop or restart a service
    Dim delay, err_return
    delay = 20000    '20 seconds
    WScript.stdout.Write "*"
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colListOfServices = objWMIService.ExecQuery _
            ("Select * from Win32_Service where DisplayName = '" & sname & "'")
    For Each objService In colListOfServices
        'Just to double check we have the right service
        If objService.displayname = sname Then
            WScript.stdout.Write "*"
            If purpose = "stop" Or purpose = "restart" Then
                    err_return = objService.stopservice
                    'service has dependencies, so we need to stop those first
                    If err_return = 3 Then
                        'GOTCHA - you have to use the service.name NOT service.displayname for this query!!
                        'even if you change Win32_Service.Name to Win32_Service.DisplayName
                        Set colServiceList2 = objWMIService.ExecQuery("Associators of " _
                           & "{Win32_Service.Name='" & objService.name & "'} Where " _
                                & "AssocClass=Win32_DependentService " & "Role=Antecedent" )
                        For Each objService2 in colServiceList2
                            objService2.StopService()
                            If Not objService2.state = "Stopped" Then
                                'you have to pause because the service wont start unless it is completely stopped
                                WScript.Sleep delay
                                service_control strComputer,sname,"stop"
                            Else
                                'WScript.Echo objService2.displayname & " has been stopped."
                            End If
                        Next
                        'stop the original service that had dependencies
                        objService.stopservice
                    Else
                        If Not objService.state = "Stopped" Then
                            'you have to pause because the service wont start unless it is completely stopped
                            WScript.Sleep delay
                            service_control strComputer,sname,"stop"
                        Else
                            'WScript.Echo objService.displayname & " has been stopped."
                        End If
                    End If
                    'if restart was sent, start service after it has stopped
                    If purpose = "restart" Then service_control strComputer, sname, "start" End if
            Elseif purpose = "start" Then    
                    WScript.stdout.Write "*"
                    err_return = objService.startservice
                    If NOT err_return = 10 Then
                        'GOTCHA - you have to use the service.name NOT service.displayname for this query!!
                        'even if you change Win32_Service.Name to Win32_Service.DisplayName
                        Set colServiceList2 = objWMIService.ExecQuery("Associators of " _
                           & "{Win32_Service.Name='" & objService.name & "'} Where " _
                                & "AssocClass=Win32_DependentService " & "Role=Antecedent" )
                        For Each objService2 in colServiceList2
                            objService2.StartService()
                            If Not objService2.state = "Running" Then
                                'you have to pause because the service wont start unless it is completely stopped
                                WScript.Sleep delay
                                service_control strComputer,sname,"start"
                            Else
                                WScript.Echo objService.displayname & " has been started."
                            End If
                        Next
                        err_return = objService.startservice
                        'service has dependencies, so we need to start those first
                        If err_return = 3 Then
                            'start the original service that had dependencies
                            objService.startservice
                            'End If
                            If Not objService.state = "Running" then
                                'you have to pause because the service wont start unless it is completely stopped
                                WScript.Sleep delay
                                service_control strComputer,sname,"start"
                            Else
                                'WScript.Echo objService.displayname & " has been started."
                            End If
                        End If
                    End If
                    'WScript.Echo objService.displayname & " has been started."
            End If
        End If
    Next
 End Sub 'service_control       
 


Code liberally borrowed from the scripting center
<message edited by kirrilian on Saturday, April 28, 2007 5:07 PM>
Have you searched [url="http://www.google.com"]here [/url]?
[url="http://tinyurl.com/as7xm"]VBScript Fundamentals[/url]
[url="http://kirrilian.dyndns.org/projects/code/"]My Site[/url]
#1
    dm_4ever

    • Total Posts : 3673
    • Scores: 82
    • Reward points : 0
    • Joined: 6/29/2006
    • Location: Orange County, California
    • Status: offline
    RE: Start, Stop, & Restart Services (Incl. dependencies) Saturday, May 05, 2007 8:56 AM (permalink)
    0
    I tried this out the other day and it worked great. 

    Just a thought...rather than waiting for an error to tell you there are dependent services, why not start off checking for dependent services and starting/stopping them accordingly?  
    dm_4ever

    My philosophy: K.I.S.S - Keep It Simple Stupid
    Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
    Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm
    #2
      kirrilian

      • Total Posts : 629
      • Scores: 3
      • Reward points : 0
      • Joined: 3/15/2005
      • Location:
      • Status: offline
      RE: Start, Stop, & Restart Services (Incl. dependencies) Monday, May 07, 2007 4:25 AM (permalink)
      0

      ORIGINAL: dm_4ever

      I tried this out the other day and it worked great. 

      Just a thought...rather than waiting for an error to tell you there are dependent services, why not start off checking for dependent services and starting/stopping them accordingly?  


      so that if there arent any, it will go ahead and start the single service

      btw, the return code was the fastest/easiest way to tell if there are dependent services
      Have you searched [url="http://www.google.com"]here [/url]?
      [url="http://tinyurl.com/as7xm"]VBScript Fundamentals[/url]
      [url="http://kirrilian.dyndns.org/projects/code/"]My Site[/url]
      #3
        kxrain

        • Total Posts : 1
        • Scores: 0
        • Reward points : 0
        • Joined: 1/15/2008
        • Status: offline
        RE: Start, Stop, & Restart Services (Incl. dependencies) Tuesday, January 15, 2008 1:45 AM (permalink)
        0
        Just a question you will save this as??
        #4
          ebgreen

          • Total Posts : 8088
          • Scores: 95
          • Reward points : 0
          • Joined: 7/12/2005
          • Status: offline
          RE: Start, Stop, & Restart Services (Incl. dependencies) Tuesday, January 15, 2008 8:09 AM (permalink)
          0
          This is a subroutine that is intended to be included in a script that you are writing. It is not intended as a stand alone file.
          "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
          Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
          http://www.visualbasicscript.com/m_47117/tm.htm
          #5
            kirrilian

            • Total Posts : 629
            • Scores: 3
            • Reward points : 0
            • Joined: 3/15/2005
            • Location:
            • Status: offline
            RE: Start, Stop, & Restart Services (Incl. dependencies) Friday, January 18, 2008 7:20 PM (permalink)
            0

            ORIGINAL: kxrain

            Just a question you will save this as??


            You save this as a .vbs file and make sure you insert another line in the beginning like I stated in the Usage

            Usage:
            service_control ".","IIS Admin","restart"

            otherwise the script wont do anything
            Have you searched [url="http://www.google.com"]here [/url]?
            [url="http://tinyurl.com/as7xm"]VBScript Fundamentals[/url]
            [url="http://kirrilian.dyndns.org/projects/code/"]My Site[/url]
            #6
              victory73

              • Total Posts : 31
              • Scores: 0
              • Reward points : 0
              • Joined: 12/3/2007
              • Status: offline
              RE: Start, Stop, & Restart Services (Incl. dependencies) Monday, January 21, 2008 2:42 PM (permalink)
              0
              I modified this script a bit, but it does not work.

              It did not return any errors, but did not stop the service.

               Sub service_control(strComputer,sname,purpose)
               strComputer="168.9.0.10"
               sname="Microsoft Exchange Connectivity Controller"
               purpose="stop"
                
               Dim delay, err_return
                delay = 20000    '20 seconds
                WScript.stdout.Writeline "*"
               Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
               Set objSWbemServices = objSWbemLocator.ConnectServer(strComputer, "root\cimv2", "administrator", "Password")
                
                Set colListOfServices = objSWbemServices.ExecQuery _
                        ("Select * from Win32_Service where DisplayName = '" & sname & "'")
                For Each objService In colListOfServices
                    
                    If objService.displayname = sname Then
                        WScript.stdout.Write "*"
                        If purpose = "stop" Or purpose = "restart" Then
                                err_return = objService.stopservice
                                
                                If err_return = 3 Then
                                    
                                    Set colServiceList2 = objSWbemServices.ExecQuery("Associators of " _
                                       & "{Win32_Service.Name='" & objService.name & "'} Where " _
                                            & "AssocClass=Win32_DependentService " & "Role=Antecedent" )
                                    For Each objService2 in colServiceList2
                                        objService2.StopService()
                                        If Not objService2.state = "Stopped" Then
                                           
                                            WScript.Sleep delay
                                            service_control strComputer,sname,"stop"
                                        Else
                                            
                                        End If
                                    Next
                                    
                                    objService.stopservice
                                Else
                                    If Not objService.state = "Stopped" Then
                                        
                                        WScript.Sleep delay
                                        service_control strComputer,sname,"stop"
                                    Else
                                        
                                    End If
                                End If
                                
                                If purpose = "restart" Then service_control strComputer, sname, "start" End if
                        Elseif purpose = "start" Then    
                                WScript.stdout.Write "*"
                                err_return = objService.startservice
                                If NOT err_return = 10 Then
                                   
                                    Set colServiceList2 = objSWbemServices.ExecQuery("Associators of " _
                                       & "{Win32_Service.Name='" & objService.name & "'} Where " _
                                            & "AssocClass=Win32_DependentService " & "Role=Antecedent" )
                                    For Each objService2 in colServiceList2
                                        objService2.StartService()
                                        If Not objService2.state = "Running" Then
                                            
                                            WScript.Sleep delay
                                            service_control strComputer,sname,"start"
                                        Else
                                            WScript.Echo objService.displayname & " has been started."
                                        End If
                                    Next
                                    err_return = objService.startservice
                                    
                                    If err_return = 3 Then
                                       
                                        objService.startservice
                                        
                                        If Not objService.state = "Running" then
                                           
                                            WScript.Sleep delay
                                            service_control strComputer,sname,"start"
                                        Else
                                            
                                        End If
                
                                    End If
                                End If
                                
                        End If
                    End If
                Next
               End Sub 'service_control      
               


              //EDIT 1/22/2008: Added code tags - dm_4ever
              <message edited by dm_4ever on Tuesday, January 22, 2008 2:17 PM>
              #7
                ebgreen

                • Total Posts : 8088
                • Scores: 95
                • Reward points : 0
                • Joined: 7/12/2005
                • Status: offline
                RE: Start, Stop, & Restart Services (Incl. dependencies) Tuesday, January 22, 2008 2:48 AM (permalink)
                0
                Well if that is your entire script, I don't see anwhere that you actually call the sub.
                "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
                Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
                http://www.visualbasicscript.com/m_47117/tm.htm
                #8
                  VFR800Boy

                  • Total Posts : 9
                  • Scores: 0
                  • Reward points : 0
                  • Joined: 1/4/2010
                  • Location: Melbourne, Victoria, Australia
                  • Status: offline
                  RE: Start, Stop, & Restart Services (Incl. dependencies) Wednesday, February 17, 2010 10:20 AM (permalink)
                  0
                  If the service does not Stop/Start within the delay period you are calling the sub from within itself. Doesn't this risk becoming a memory hog if the service is non-responsive?

                  What about
                      Do Until objService.state = "Running"
                           iRetry = iRetry + 1
                           If iRetry = iMaxRetry Then
                               Exit Do
                           End If
                           Wscript.Sleep iDelay
                      Loop
                   
                  #9
                    Philip

                    • Total Posts : 10
                    • Scores: 0
                    • Reward points : 0
                    • Joined: 3/3/2010
                    • Status: offline
                    Re:Start, Stop, & Restart Services (Incl. dependencies) Monday, July 26, 2010 3:50 AM (permalink)
                    0
                    I'm trying to run this script on a service with multiple dependencies, two for this specific service, and I'm not able to make it work.
                    If I use a service with one dependent service it works.
                    Any one could help modify the script to make it work with multiple dependencies please?
                     
                    Thanks
                    #10

                      Online Bookmarks Sharing: Share/Bookmark

                      Jump to:

                      Current active users

                      There are 0 members and 2 guests.

                      Icon Legend and Permission

                      • New Messages
                      • No New Messages
                      • Hot Topic w/ New Messages
                      • Hot Topic w/o New Messages
                      • Locked w/ New Messages
                      • Locked w/o New Messages
                      • Read Message
                      • Post New Thread
                      • Reply to message
                      • Post New Poll
                      • Submit Vote
                      • Post reward post
                      • Delete my own posts
                      • Delete my own threads
                      • Rate post

                      2000-2012 ASPPlayground.NET Forum Version 3.8
                      mbt shoes www.wileywilson.com