From 1ad978cf9235152fa99769e1cdffb09a4ab9ade0 Mon Sep 17 00:00:00 2001 From: Bill Hurt Date: Wed, 6 May 2020 06:37:36 -0700 Subject: [PATCH] start using sc --- .../DSC_xServiceResource.psm1 | 42 +++++++++++++++---- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/source/DSCResources/DSC_xServiceResource/DSC_xServiceResource.psm1 b/source/DSCResources/DSC_xServiceResource/DSC_xServiceResource.psm1 index c8d22ac83..5bb15558a 100644 --- a/source/DSCResources/DSC_xServiceResource/DSC_xServiceResource.psm1 +++ b/source/DSCResources/DSC_xServiceResource/DSC_xServiceResource.psm1 @@ -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 } } } @@ -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" + } + } +}