Skip to content

Instantly share code, notes, and snippets.

@davidlu1001
Last active May 17, 2024 00:11
Show Gist options
  • Save davidlu1001/90203aebed700af3aaea71d6c77128e5 to your computer and use it in GitHub Desktop.
Save davidlu1001/90203aebed700af3aaea71d6c77128e5 to your computer and use it in GitHub Desktop.
Get/Set Service Recovery Settings
<#
.SYNOPSIS
Manages the recovery options of Windows services.
.DESCRIPTION
This script can retrieve or set the recovery options of specified Windows services using the `sc.exe` utility. It supports operations on multiple services at once by accepting service names with wildcards or regex patterns. The script allows for configuring how the service responds to first, second, and subsequent failures.
.PARAMETER ServiceNames
Array of service names or patterns to match against existing services.
.PARAMETER Ops
Specifies the operation to perform: 'get' (retrieve recovery options) or 'set' (configure recovery options).
.PARAMETER FirstFailure
Specifies the action to take on the first failure.
.PARAMETER FirstDelay
Specifies the delay before taking the first failure action (in milliseconds).
.PARAMETER SecondFailure
Specifies the action to take on the second failure.
.PARAMETER SecondDelay
Specifies the delay before taking the second failure action (in milliseconds).
.PARAMETER SubsequentFailure
Specifies the action to take on subsequent failures.
.PARAMETER SubsequentDelay
Specifies the delay before taking the subsequent failure action (in milliseconds).
.PARAMETER ResetDelay
Specifies the time (in seconds) after which the failure count should reset to zero.
.EXAMPLE
.\manageServiceRecovery.ps1 -ServiceNames 'wuauserv', 'bits' -Ops 'get'
Retrieves the recovery options for the 'wuauserv' and 'bits' services.
.EXAMPLE
.\manageServiceRecovery.ps1 -ServiceNames '*sql*' -Ops 'set' -FirstFailure 'restart' -FirstDelay 60000 -SecondFailure 'restart' -SecondDelay 300000 -SubsequentFailure 'restart' -SubsequentDelay 900000 -ResetDelay 86400
Sets the recovery options for all services that include 'sql' in their names, configuring restart actions with specified delays.
.NOTES
Ensure that `sc.exe` is available and the script is run with administrative privileges to manage service settings effectively.
#>
param (
[Parameter(Mandatory)]
[string[]]$ServiceNames,
[Parameter(Mandatory)]
[ValidateSet("get", "set")]
[string]$Ops,
[string]$FirstFailure = "restart",
[int]$FirstDelay = 60000,
[string]$SecondFailure = "restart",
[int]$SecondDelay = 900000,
[string]$SubsequentFailure = "restart",
[int]$SubsequentDelay = 900000,
[int]$ResetDelay = 86400
)
foreach ($ServiceName in $ServiceNames) {
$services = Get-Service | Where-Object { $_.Name -match $ServiceName }
if ($services.Count -gt 0) {
foreach ($srv in $services) {
switch ($Ops) {
"get" {
Write-Output "Retrieving recovery options for Service: $($srv.Name)"
Try {
& sc.exe qc $srv.Name
& sc.exe qfailure $srv.Name
} Catch {
Write-Error "Failed to retrieve settings for $($srv.Name): $_"
}
}
"set" {
if ($PSCmdlet.ShouldProcess("$($srv.Name)", "Set recovery options")) {
Write-Output "Setting recovery options for Service: $($srv.Name)"
Try {
$result = & sc.exe failure $srv.Name reset= $ResetDelay actions= "$FirstFailure/$FirstDelay/$SecondFailure/$SecondDelay/$SubsequentFailure/$SubsequentDelay" 2>&1
Write-Output "Recovery options set successfully."
} Catch {
Write-Error "Failed to set recovery options: $_"
}
}
}
}
}
} else {
Write-Output "No service found matching $ServiceName"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment