Skip to content

Commit

Permalink
start using sc
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomNoun7 committed Jul 8, 2020
1 parent da51d18 commit 1ad978c
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions source/DSCResources/DSC_xServiceResource/DSC_xServiceResource.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -2451,13 +2451,7 @@ function Set-ServiceFailureActionProperty {
# if it doesn't already exist.
if ($PSBoundParameters.ContainsKey('FailureActionsOnNonCrashFailures'))
{
if (Get-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\$serviceName -Name 'FailureActionsOnNonCrashFailures' -ErrorAction SilentlyContinue) {
Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\$serviceName -Name 'FailureActionsOnNonCrashFailures' -Value $FailureActionsOnNonCrashFailures | Out-Null
}
else
{
New-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\$serviceName -Name 'FailureActionsOnNonCrashFailures' -Value $FailureActionsOnNonCrashFailures | Out-Null
}
Invoke-SCFailureFlag -ServiceName $ServiceName -Flag $FailureActionsOnNonCrashFailures
}
}
}
Expand All @@ -2483,3 +2477,37 @@ function Test-HasRestartFailureAction
$hasRestartAction
}
}

<#
.SYNOPSIS
Use sc.exe to set the failure flag
.DESCRIPTION
The FailureActionsOnNonCrashFailures feature of the service controller
cannnot be reliably managed in code via anything but sc.exe. Managing the
registry key value on its own has proved inadequate. This cmdlet gives us
an easily mockable and testable way to invoke sc to manage this flag.
.EXAMPLE
PS C:\> Invoke-SCFailureFlag -ServiceName WSearch -Flag 1
This will invoke the equivelent of '& sc.exe failureFlag WSearch 1'
#>
function Invoke-SCFailureFlag {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[System.String]
$ServiceName,
[Parameter(Mandatory = $true)]
[ValidateSet(0,1)]
[System.Int32]
$Flag
)

process {
& sc.exe @('failureFlag', $ServiceName, $Flag) | Out-Null
# A quick sanity check to make sure the value was set as expected without having to parse the string output of sc.
$failureFlag = (Get-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\$serviceName -Name 'FailureActionsOnNonCrashFailures').failureActionsOnNonCrashFailures
if ($failureFlag -ne $Flag) {
throw "sc.exe did not succesfully set the failure flag for $servicename"
}
}
}

0 comments on commit 1ad978c

Please sign in to comment.