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
¯\_(ツ)_/¯
- Edited by jrvMicrosoft Community Contributor Thursday, February 14, 2013 3:03 PM
-
Monday, February 18, 2013 5:31 PMModerator
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>
- Edited by Thomas LeeMVP, Moderator Monday, February 18, 2013 5:32 PM
- Proposed As Answer by Thomas LeeMVP, Moderator Monday, February 18, 2013 5:33 PM
- Marked As Answer by IamMredMicrosoft Employee, Owner Tuesday, March 05, 2013 4:14 AM
-
Monday, February 18, 2013 5:56 PM
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.
¯\_(ツ)_/¯
- Edited by jrvMicrosoft Community Contributor Monday, February 18, 2013 5:58 PM

