how to combine get service and set service in a powershell script

Answered how to combine get service and set service in a powershell script

  • Thursday, February 14, 2013 2:38 PM
     
     

    As a system integration engineer, I receive automated emails alerts with the "scriptlogic" service stops.  Instead of logging into the server and going to services to start the script, i'd like to automate it.  My partner beleives we can create a macro that promps us for a server name.  Once that server name is provided we would like to have that server scan its services looking only for the script logic process.  For that we can use a get-service -name ScriptLogic.  I would like to what to add to the script to have it check the status and if it's not -eq to "started", we would like it set to started.  How could I script that?

    Thanks

All Replies

  • Thursday, February 14, 2013 3:01 PM
     
     

    Start with this:

    HELP GET-SERVICE-FULL

    HELP START-SERVICE -FULL

    Get-Service scriptlogic | Start-Service

    http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx


    ¯\_(ツ)_/¯


  • Monday, February 18, 2013 5:31 PM
    Moderator
     
     Answered

    As a system integration engineer, I receive automated emails alerts with the "scriptlogic" service stops.  Instead of logging into the server and going to services to start the script, i'd like to automate it.  My partner beleives we can create a macro that promps us for a server name.  Once that server name is provided we would like to have that server scan its services looking only for the script logic process.  For that we can use a get-service -name ScriptLogic.  I would like to what to add to the script to have it check the status and if it's not -eq to "started", we would like it set to started.  How could I script that?

    Thanks

    You want something like this:

    $foo = Read-Host "Enter hostname"
    $srv = Get-Service -ComputerName $foo -Name Scriptlogic
    if ($srv.status -eq 'stopped') {$srv | Start-Service}

    HTH


    Thomas Lee <DoctorDNS@Gmail.Com>


  • Monday, February 18, 2013 5:56 PM
     
      Has Code

    I should note that each service can be configured to automatically restart or to execute a script when it stops.

    F:\projects\scripts>sc failure
    DESCRIPTION:
            Changes the actions upon failure
    USAGE:
            sc <server> failure [service name] <option1> <option2>...
    OPTIONS:
            reset= <Length of period of no failures (in seconds)
                    after which to reset the failure count to 0 (may be INFINITE)>
                    (Must be used in conjunction with actions= )
            reboot= <Message broadcast before rebooting on failure>
            command= <Command line to be run on failure>
            actions= <Failure actions and their delay time (in milliseconds),
                      separated by / (forward slash) -- e.g., run/5000/reboot/800
                      Valid actions are <run|restart|reboot>  >
                      (Must be used in conjunction with the reset= option)

    With this you can set the service to start on a failure and have it restart repeatedly or execute a script or both.


    ¯\_(ツ)_/¯