How to set Service recovery options via PowerShell

Run this function, then run the new Cmd-let that it creates. Replace the server name with the one you want to set the settings for. Replace the Service name with the Display name of the service you want to set.

  1. function Set-ServiceRecovery{
  2. [alias('Set-Recovery')]
  3. param
  4. (
  5. [string] [Parameter(Mandatory=$true)] $ServiceDisplayName,
  6. [string] [Parameter(Mandatory=$true)] $Server,
  7. [string] $action1 = "restart",
  8. [int] $time1 = 30000, # in miliseconds
  9. [string] $action2 = "restart",
  10. [int] $time2 = 30000, # in miliseconds
  11. [string] $actionLast = "restart",
  12. [int] $timeLast = 30000, # in miliseconds
  13. [int] $resetCounter = 4000 # in seconds
  14. )
  15. $serverPath = "\\" + $server
  16. $services = Get-CimInstance -ClassName 'Win32_Service' -ComputerName $Server| Where-Object {$_.DisplayName -imatch $ServiceDisplayName}
  17. $action = $action1+"/"+$time1+"/"+$action2+"/"+$time2+"/"+$actionLast+"/"+$timeLast
  18. foreach ($service in $services){
  19. # https://technet.microsoft.com/en-us/library/cc742019.aspx
  20. $output = sc.exe $serverPath failure $($service.Name) actions= $action reset= $resetCounter
  21. }
  22. }
  23. Set-ServiceRecovery -ServiceDisplayName "My Service" -Server "PHOSTA"

Add comment