ps/Modules/Alkami.PowerShell.Services/Public/Set-ServiceRecoveryOneRestart.ps1
2023-05-30 22:51:22 -07:00

62 lines
2.4 KiB
PowerShell

function Set-ServiceRecoveryOneRestart {
<#
.SYNOPSIS
Set the service with specified name to restart once, then no more recovery actions
.PARAMETER ServiceName
The service name to set the state for
#>
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions","",Justification="Alkami does not typically check for should-proceed for intentionally state changing operations")]
[CmdletBinding()]
param (
[string]$ServiceName
)
$logLead = (Get-logLeadName)
if ($null -eq (Get-Service -Name $ServiceName)) {
Write-Warning "$logLead : [$ServiceName] does not appear to be a valid service. Can not set the service recovery restart"
return
}
<#
from the sc.exe failure configuration options message:
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)
#>
# action/time/<empty>/<empty>/<empty>/<empty>
# This sets the first one to be restart after 10s, then nothing else
$recoveryActions = "restart/10000////"
# 86400 = default (24 hours)
Write-Host "$logLead : Setting recovery actions for [$ServiceName]"
Invoke-SCExe @("failure",$ServiceName,"actions=",$recoveryActions,"reset=",86400)
if ($LASTEXITCODE -ne 0) {
Write-Error "$logLead : Attempt to call sc.exe and set failure recovery actions errored out. Please review logs"
} else {
Write-Host "$logLead : Recovery options set successfully for [$ServiceName]"
}
Write-Host "$logLead : Setting failureflag to 1"
Invoke-SCExe @("failureflag",$ServiceName,"1")
if ($LASTEXITCODE -ne 0) {
Write-Error ("$logLead : An error occurred setting the failure flag option for service $ServiceName")
}
}