@@ -2451,13 +2451,7 @@ function Set-ServiceFailureActionProperty {
2451
2451
# if it doesn't already exist.
2452
2452
if ($PSBoundParameters.ContainsKey (' FailureActionsOnNonCrashFailures' ))
2453
2453
{
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
2461
2455
}
2462
2456
}
2463
2457
}
@@ -2483,3 +2477,38 @@ function Test-HasRestartFailureAction
2483
2477
$hasRestartAction
2484
2478
}
2485
2479
}
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