From 1e924d67a3b7a821dbf2bec758208ecc697c0421 Mon Sep 17 00:00:00 2001 From: Yorick Kuijs Date: Wed, 5 Aug 2020 07:36:40 +0200 Subject: [PATCH] Fixed issue #306 (#346) --- CHANGELOG.md | 6 ++++++ .../DSC_ScheduledTask/DSC_ScheduledTask.psm1 | 12 +++++++++-- tests/Unit/DSC_ScheduledTask.Tests.ps1 | 21 +++++++++++++++++-- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e0d7ec4f..19fb21e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- ScheduledTask + - Fixed issue with disabling scheduled tasks that have "Run whether user is + logged on or not" configured - Fixes [Issue #306](https://github.com/dsccommunity/ComputerManagementDsc/issues/306). + ## [8.4.0] - 2020-08-03 ### Changed diff --git a/source/DSCResources/DSC_ScheduledTask/DSC_ScheduledTask.psm1 b/source/DSCResources/DSC_ScheduledTask/DSC_ScheduledTask.psm1 index 79b366ee..02cf7ac0 100644 --- a/source/DSCResources/DSC_ScheduledTask/DSC_ScheduledTask.psm1 +++ b/source/DSCResources/DSC_ScheduledTask/DSC_ScheduledTask.psm1 @@ -456,7 +456,15 @@ function Set-TargetResource -and -not $PSBoundParameters.ContainsKey('ActionExecutable')) { Write-Verbose -Message ($script:localizedData.DisablingExistingScheduledTask -f $TaskName, $TaskPath) - Disable-ScheduledTask -TaskName $TaskName -TaskPath $TaskPath + + if ($PSVersionTable.PSVersion -gt [System.Version]'5.0.0.0') + { + Disable-ScheduledTask -TaskName $TaskName -TaskPath $TaskPath + } + else + { + Disable-ScheduledTaskEx -TaskName $TaskName -TaskPath $TaskPath + } return } @@ -1635,7 +1643,7 @@ function ConvertTo-TimeSpanStringFromScheduledTaskString .PARAMETER TaskPath The path to the task to disable. #> -function Disable-ScheduledTask +function Disable-ScheduledTaskEx { [CmdletBinding()] param diff --git a/tests/Unit/DSC_ScheduledTask.Tests.ps1 b/tests/Unit/DSC_ScheduledTask.Tests.ps1 index a1247c30..894c9203 100644 --- a/tests/Unit/DSC_ScheduledTask.Tests.ps1 +++ b/tests/Unit/DSC_ScheduledTask.Tests.ps1 @@ -93,6 +93,7 @@ try Describe 'DSC_ScheduledTask' { BeforeAll { + Mock -CommandName Disable-ScheduledTask Mock -CommandName Register-ScheduledTask Mock -CommandName Set-ScheduledTask Mock -CommandName Unregister-ScheduledTask @@ -222,7 +223,15 @@ try It 'Should remove the scheduled task in the set method' { Set-TargetResource @testParameters - Assert-MockCalled Register-ScheduledTask -Exactly -Times 1 + + if ($PSVersionTable.PSVersion -gt [System.Version]'5.0.0.0') + { + Assert-MockCalled Disable-ScheduledTask -Exactly -Times 1 + } + else + { + Assert-MockCalled Register-ScheduledTask -Exactly -Times 1 + } } } @@ -1573,7 +1582,15 @@ try It 'Should disable the scheduled task in the set method' { Set-TargetResource @testParameters - Assert-MockCalled Register-ScheduledTask -Exactly -Times 1 + + if ($PSVersionTable.PSEdition -gt [System.Version]'5.0.0.0') + { + Assert-MockCalled Disable-ScheduledTask -Exactly -Times 1 + } + else + { + Assert-MockCalled Register-ScheduledTask -Exactly -Times 1 + } } }