Skip to content

Commit a7c31e9

Browse files
committed
start using sc
1 parent 2d2e1de commit a7c31e9

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed

source/DSCResources/DSC_xServiceResource/DSC_xServiceResource.psm1

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2451,13 +2451,7 @@ function Set-ServiceFailureActionProperty {
24512451
# if it doesn't already exist.
24522452
if ($PSBoundParameters.ContainsKey('FailureActionsOnNonCrashFailures'))
24532453
{
2454-
if (Get-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\$serviceName -Name 'FailureActionsOnNonCrashFailures' -ErrorAction SilentlyContinue) {
2455-
Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\$serviceName -Name 'FailureActionsOnNonCrashFailures' -Value $FailureActionsOnNonCrashFailures | Out-Null
2456-
}
2457-
else
2458-
{
2459-
New-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\$serviceName -Name 'FailureActionsOnNonCrashFailures' -Value $FailureActionsOnNonCrashFailures | Out-Null
2460-
}
2454+
Invoke-SCFailureFlag -ServiceName $ServiceName -Flag $FailureActionsOnNonCrashFailures
24612455
}
24622456
}
24632457
}
@@ -2483,3 +2477,38 @@ function Test-HasRestartFailureAction
24832477
$hasRestartAction
24842478
}
24852479
}
2480+
2481+
<#
2482+
.SYNOPSIS
2483+
Use sc.exe to set the failure flag
2484+
.DESCRIPTION
2485+
The FailureActionsOnNonCrashFailures feature of the service controller
2486+
cannnot be reliably managed in code via anything but sc.exe. Managing the
2487+
registry key value on its own has proved inadequate. This cmdlet gives us
2488+
an easily mockable and testable way to invoke sc to manage this flag.
2489+
.EXAMPLE
2490+
PS C:\> Invoke-SCFailureFlag -ServiceName WSearch -Flag 1
2491+
This will invoke the equivelent of '& sc.exe failureFlag WSearch 1'
2492+
#>
2493+
function Invoke-SCFailureFlag {
2494+
[CmdletBinding()]
2495+
param (
2496+
[Parameter(Mandatory = $true)]
2497+
[System.String]
2498+
$ServiceName,
2499+
[Parameter(Mandatory = $true)]
2500+
[ValidateSet(0,1)]
2501+
[System.Int32]
2502+
$Flag
2503+
)
2504+
2505+
process {
2506+
& sc.exe @('failureFlag', $ServiceName, $Flag) | Out-Null
2507+
# A quick sanity check to make sure the value was set as expected without having to parse the string output of sc.
2508+
$failureFlag = (Get-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\$serviceName -Name 'FailureActionsOnNonCrashFailures').failureActionsOnNonCrashFailures
2509+
if ($failureFlag -ne $Flag) {
2510+
throw "sc.exe did not succesfully set the failure flag for $servicename"
2511+
}
2512+
}
2513+
}
2514+

0 commit comments

Comments
 (0)