From da51d18c9acb576878756c5cd0677b40abc16fc1 Mon Sep 17 00:00:00 2001 From: Bill Date: Mon, 27 Apr 2020 15:33:27 +0000 Subject: [PATCH] Fix error if key already exists. --- .../DSC_xServiceResource.psm1 | 40 +++++++++---------- .../DSC_xServiceResource.schema.mof | 1 + 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/source/DSCResources/DSC_xServiceResource/DSC_xServiceResource.psm1 b/source/DSCResources/DSC_xServiceResource/DSC_xServiceResource.psm1 index da0942d0d..c8d22ac83 100644 --- a/source/DSCResources/DSC_xServiceResource/DSC_xServiceResource.psm1 +++ b/source/DSCResources/DSC_xServiceResource/DSC_xServiceResource.psm1 @@ -2393,7 +2393,7 @@ function Set-ServiceFailureActionProperty { # This setting is a combination a flag in the _SERVICE_FAILURE_ACTIONSA struct, # and the actual string value for the message stored in the 'RebootMessage' registry property. # If hasRebootMessage is already true, then we know the key exists and we can just set it. - if ($failureActions.hasRebootMessage) + if (Get-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\$serviceName -Name 'RebootMessage' -ErrorAction SilentlyContinue) { Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\$serviceName -Name 'RebootMessage' -Value $RebootMessage | Out-Null } @@ -2411,7 +2411,7 @@ function Set-ServiceFailureActionProperty { # This is the same as the RebootMessage property above. It's a combination of a flag in the struct, and an external property that stores the value. if ($PSBoundParameters.ContainsKey('FailureCommand')) { - if ($failureActions.hasFailureCommand) + if (Get-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\$serviceName -Name 'FailureCommand' -ErrorAction SilentlyContinue) { Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\$serviceName -Name 'FailureCommand' -Value $FailureCommand | Out-Null } @@ -2451,7 +2451,7 @@ function Set-ServiceFailureActionProperty { # if it doesn't already exist. if ($PSBoundParameters.ContainsKey('FailureActionsOnNonCrashFailures')) { - if ($failureActions.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 @@ -2463,23 +2463,23 @@ function Set-ServiceFailureActionProperty { } function Test-HasRestartFailureAction - { - [CmdletBinding()] - param ( - [Parameter()] - [System.Object[]] - $Collection - ) - - process { - $hasRestartAction = $false - - foreach ($action in $collection) { - if ($action.type -eq 'RUN_COMMAND') { - $hasRestartAction = $true - } - } +{ + [CmdletBinding()] + param ( + [Parameter()] + [System.Object[]] + $Collection + ) + + process { + $hasRestartAction = $false - $hasRestartAction + foreach ($action in $collection) { + if ($action.type -eq 'RUN_COMMAND') { + $hasRestartAction = $true + } } + + $hasRestartAction } +} diff --git a/source/DSCResources/DSC_xServiceResource/DSC_xServiceResource.schema.mof b/source/DSCResources/DSC_xServiceResource/DSC_xServiceResource.schema.mof index ff2f9b7f2..af7cd26ee 100644 --- a/source/DSCResources/DSC_xServiceResource/DSC_xServiceResource.schema.mof +++ b/source/DSCResources/DSC_xServiceResource/DSC_xServiceResource.schema.mof @@ -20,6 +20,7 @@ class DSC_xServiceResource : OMI_BaseResource [Write,Description("The command line to run if a service fails.")] String FailureCommand; [Write,EmbeddedInstance("DSC_xFailureAction"),Description("The actions to take when a service fails.")] String FailureActionsCollection[]; [Write,Description("A flag indicating whether failure actions should be invoked on non-crash failures.")] Boolean FailureActionsOnNonCrashFailures; + [Write,Description("An optional broadcast message to send to logged in users if the machine reboots as a result of a failure action.")] String RebootMessage; }; [ClassVersion("1.0.0")]