7 Replies Latest reply on Nov 9, 2010 3:51 AM by David Lipsham

    Managing Windows Service in a NSH script.

      Hi all,

      I am looking for the best way to manage windows services in an NSH script.

      Essentially I will pass parameters into the script that will be the names of services I want to operate on.

      Then I want to do the normal things like check to see if the service exists, stop, start or restart the services.

       

      I'm presuming that "net stop/start servicename" is the best way to stop and start the services ... this is no problem.

       

      To check if a service exists I was looking at doing a blquery .....

      A normal query would like like ....

       

      blquery -h hostname -e 'service_exists ("w32time")'

       

      The problem I have is that I want to pass a number of different services into this command using the parameters that I passed into the NSH script.

      So essentially I want .....

       

      blquery -h $hostname -e 'service_exists ("$service_name)'

       

      I dont seem to be able to pass a variable into the blquery command.

       

      Is this the best way to approach this?

      Happy to take advice on best practices with dealing with windows service management in NSH scripts.

       

      Thanks

      Dave

        • 1. Re: Managing Windows Service in a NSH script.
          Stefan Kruth

          Hi David,

          if there is no other need to run a script, I'd go for a compliance job instead. Windows services are native objects that can be verified by compliance rules and modified by deploying a remediation package.

           

          Cheers, Stefan

          • 2. Re: Managing Windows Service in a NSH script.

            Hi Stefan,

            thanks for getting back to me.

            The way that I'm structuring this I think that I really need to use a script.

            The logic for managing the windows services will be part of another script that also manages tomcat instances.

             

            Dave

            • 3. Re: Managing Windows Service in a NSH script.

              In your nsh script use nexec to invoke "cmd /k sc " to get what you want about services on the remote target

              1 of 1 people found this helpful
              • 4. Re: Managing Windows Service in a NSH script.
                Joshua Skirde

                Hi David,

                 

                In the command you originally posted you're using single quotes around the 'service_exists("$service")' which is taking the string literal and not performing variable substitution.

                If you use double quotes around "service_exists" and escape the quotes around $service you should be okay.

                 

                It will look like this:

                blquery -h $hostname -e "service_exists(\"$service\")"

                 

                Kind regards,

                Joshua

                1 of 1 people found this helpful
                • 5. Re: Managing Windows Service in a NSH script.

                  Hi Joshua,

                  thanks for the post.  That worked very nicely thanks.

                   

                  I must admit I'm a little surprised that there isn't a blcli command to manage windows services seeing as it can be done from within the console.

                  • 6. Re: Managing Windows Service in a NSH script.
                    Joshua Skirde

                    Hi David,

                     

                    You don't need a blcli command to manage services. In fact, due to the overhead of starting up a JVM to run the blcli, it would take a lot longer than just using the Microsoft methods for interacting with services locally (as provided by Adam).

                     

                    Here are some examples ... You could put them in a cmd script or a blpackage or prefix with nexec and run from within a network shell script.

                     

                    C:\Windows\system32>set SERVICE="WSearch"

                    C:\Windows\system32>cmd /c sc query %SERVICE%

                    SERVICE_NAME: WSearch
                            TYPE               : 10  WIN32_OWN_PROCESS
                            STATE              : 1  STOPPED
                            WIN32_EXIT_CODE    : 0  (0x0)
                            SERVICE_EXIT_CODE  : 0  (0x0)
                            CHECKPOINT         : 0x0
                            WAIT_HINT          : 0x0

                    C:\Windows\system32>echo %ERRORLEVEL%
                    0

                    C:\Windows\system32>cmd /c sc query fred
                    [SC] EnumQueryServicesStatus:OpenService FAILED 1060:

                    The specified service does not exist as an installed service.


                    C:\Windows\system32>echo %ERRORLEVEL%
                    1060

                    C:\Windows\system32>cmd /c sc start %SERVICE%

                    SERVICE_NAME: WSearch
                            TYPE               : 10  WIN32_OWN_PROCESS
                            STATE              : 2  START_PENDING
                                                    (NOT_STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
                            WIN32_EXIT_CODE    : 0  (0x0)
                            SERVICE_EXIT_CODE  : 0  (0x0)
                            CHECKPOINT         : 0x0
                            WAIT_HINT          : 0x7d0
                            PID                : 4492
                            FLAGS              :

                    C:\Windows\system32>echo %ERRORLEVEL%
                    0

                    C:\Windows\system32>cmd /c sc stop %SERVICE%


                    SERVICE_NAME: WSearch
                            TYPE               : 10  WIN32_OWN_PROCESS
                            STATE              : 3  STOP_PENDING
                                                    (NOT_STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
                            WIN32_EXIT_CODE    : 0  (0x0)
                            SERVICE_EXIT_CODE  : 0  (0x0)
                            CHECKPOINT         : 0x1
                            WAIT_HINT          : 0x7530

                    C:\Windows\system32>echo %ERRORLEVEL%
                    0

                     

                    Kind regards,

                    Joshua

                    • 7. Re: Managing Windows Service in a NSH script.

                      Thanks Joshua,

                      a good point about the resource taken for the jvm to start.

                      I ended up using this in a NSH script ....

                      WINSERVICE_STATUS=`nexec -e sc query ${COMPONENT_INSTANCE} | grep STATE | awk '{ print $4 }'`

                       

                      Essentially sets WINSERVICE_STATUS to the status of the service ${COMPONENT_INSTANCE}

                       

                      Dave