# Set service recovery options in PowerShell

Recently one of our Administrators reported that he had to manually startup some Pulseway services after windows updates. It sometimes happens…

blog

22 Oct 2017 pl

![Set service recovery options in PowerShell](/wp-content/uploads/2016/02/PowerShell.png)

[Zobacz obrazy (2)](#pf-content-image-1-0)

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](https://technet.microsoft.com/en-us/library/cc742019.aspx) to set failure options. Following script utilizes sc.exe and allows you to set actions for:

- First failure
- Second failure
- Subsequent failure

[!\[Zakładka Recovery usługi Pulseway pokazująca restart usługi po pierwszej, drugiej i kolejnych awariach.\](/wp-content/uploads/2017/10/img_59ec8dd72a12b.png)](/wp-content/uploads/2017/10/img_59ec8dd72a12b.png)

Script has 2 mandatory parameters:

- Service Display Name
- ServerName

The rest of the parameters are optional.

```powershell
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**.

[!\[Właściwości usługi Pulseway pokazujące nazwę usługi PC Monitor oraz nazwę wyświetlaną Pulseway.\](/wp-content/uploads/2017/10/img_59ec8dbea9295.png)](/wp-content/uploads/2017/10/img_59ec8dbea9295.png)

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

## Powiązane projekty

Projekty powiązane z tagami, kategoriami i treścią tego wpisu.

[Wszystkie projekty](/projekty/)

Trwa dopasowywanie projektów...

[Powrót do bloga](/blog/)

## Powiązane wpisy

- [Check an API write after a PowerShell timeout](/blog/verify-uncertain-api-outcomes-powershell/)
- [Inspect and convert mixed Office archives with PSWriteOffice](/blog/inspect-and-convert-mixed-office-archives-pswriteoffice/)
- [Compose PDF reports and automate page workflows with PSWriteOffice](/blog/automate-pdf-reports-and-page-workflows-pswriteoffice/)

<!-- PowerForge markdown artifact sha256:DA5A778D8A8E626FFE6334C07DFA952114A8E99C1C4093414AA966867B5E2F9D -->
