Scroll Top
Evotec Services sp. z o.o., ul. Drozdów 6, Mikołów, 43-190, Poland

Set service recovery options in PowerShell

PowerShell

Recently one of our Administrators reported that he had to manually startup some Pulseway services after windows updates. It sometimes happens that during Windows updates on hosts machines get into Paused state, and when they come back, the service goes down and stays down. The way to fix it is to set service recovery options. Since it's possible that the first two tries will fail, I wanted to fix this so it would keep Pulseway up and running always.

Solution

Since at the moment PowerShell doesn't allow to modify this setting one has to use SC.EXE to set failure options. Following script utilizes sc.exe and allows you to set actions for:

First failure
Second failure
Subsequent failure

Script has 2 mandatory parameters:
Service Display Name
ServerName

The rest of the parameters are optional.

function Set-ServiceRecovery{
    [alias('Set-Recovery')]
    param
    (
        [string] [Parameter(Mandatory=$true)] $ServiceDisplayName,
        [string] [Parameter(Mandatory=$true)] $Server,
        [string] $action1 = "restart",
        [int] $time1 =  30000, # in miliseconds
        [string] $action2 = "restart",
        [int] $time2 =  30000, # in miliseconds
        [string] $actionLast = "restart",
        [int] $timeLast = 30000, # in miliseconds
        [int] $resetCounter = 4000 # in seconds
    )
    $serverPath = "\\" + $server
    $services = Get-CimInstance -ClassName 'Win32_Service' -ComputerName $Server| Where-Object {$_.DisplayName -imatch $ServiceDisplayName}
    $action = $action1+"/"+$time1+"/"+$action2+"/"+$time2+"/"+$actionLast+"/"+$timeLast
    foreach ($service in $services){
        # https://technet.microsoft.com/en-us/library/cc742019.aspx
        $output = sc.exe $serverPath failure $($service.Name) actions= $action reset= $resetCounter
    }
}
Set-ServiceRecovery -ServiceDisplayName "Pulseway" -Server "MAIL1"

Keep in mind this script doesn't use Service Name but Display Name.

I've chosen this approach due to the different naming of services. This is true for Pulseway which used to be called PC Monitor.

Posty powiązane

Zostaw komentarz

You must be logged in to post a comment.