From 830db9035c2341e5f172d598011f1a5cdf9588a3 Mon Sep 17 00:00:00 2001 From: Robert Simpson Date: Wed, 10 Jun 2020 11:03:38 -0500 Subject: [PATCH] Issue-#333 Commit requested changes --- CHANGELOG.md | 2 +- .../DSC_ScheduledTask/DSC_ScheduledTask.psm1 | 5 +++ tests/Unit/DSC_ScheduledTask.Tests.ps1 | 32 +++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9bdcb8ed..f6b8e3d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixed build failures caused by changes in `ModuleBuilder` module v1.7.0 by changing `CopyDirectories` to `CopyPaths` - Fixes [Issue #332](https://github.com/dsccommunity/ComputerManagementDsc/issues/332). -- StartedTask +- ScheduledTask - Add "StopExisting" to valid values for MultipleInstances parameter - Fixes [Issue #333](https://github.com/dsccommunity/ComputerManagementDsc/issues/333). ## [8.2.0] - 2020-05-05 diff --git a/source/DSCResources/DSC_ScheduledTask/DSC_ScheduledTask.psm1 b/source/DSCResources/DSC_ScheduledTask/DSC_ScheduledTask.psm1 index c3ff4d5d..95c4dffa 100644 --- a/source/DSCResources/DSC_ScheduledTask/DSC_ScheduledTask.psm1 +++ b/source/DSCResources/DSC_ScheduledTask/DSC_ScheduledTask.psm1 @@ -583,6 +583,11 @@ function Set-TargetResource $setting = New-ScheduledTaskSettingsSet @settingParameters + <# The following workaround is needed because the TASK_INSTANCES_STOP_EXISTING value of + https://docs.microsoft.com/en-us/windows/win32/taskschd/tasksettings-multipleinstances is missing + from the Microsoft.PowerShell.Cmdletization.GeneratedTypes.ScheduledTask.MultipleInstancesEnum, + which is used for the other values of $MultipleInstances. (at least currently, as of June, 2020) + #> if ($MultipleInstances -eq 'StopExisting') { $setting.CimInstanceProperties.Item('MultipleInstances').Value = 3 diff --git a/tests/Unit/DSC_ScheduledTask.Tests.ps1 b/tests/Unit/DSC_ScheduledTask.Tests.ps1 index 6d451b97..be6eb36b 100644 --- a/tests/Unit/DSC_ScheduledTask.Tests.ps1 +++ b/tests/Unit/DSC_ScheduledTask.Tests.ps1 @@ -128,6 +128,38 @@ try } } + Context 'No scheduled task exists, but it should, with MultipleInstances = StopExisting' { + $testParameters = $getTargetResourceParameters + @{ + ActionExecutable = 'C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe' + ScheduleType = 'Once' + RepeatInterval = (New-TimeSpan -Minutes 15).ToString() + RepetitionDuration = (New-TimeSpan -Minutes 150).ToString() + MultipleInstances = 'StopExisting' + Verbose = $true + } + + Mock -CommandName Get-ScheduledTask -MockWith { return $null } + + It 'Should return the correct values from Get-TargetResource for initial state' { + $result = Get-TargetResource @getTargetResourceParameters + $result.Ensure | Should -Be 'Absent' + } + + It 'Should return false from the test method' { + Test-TargetResource @testParameters | Should -BeFalse + } + + It 'Should create the scheduled task in the set method' { + Set-TargetResource @testParameters + } + + It 'Should return the correct values from Get-TargetResource for updated state' { + $result = Get-TargetResource @getTargetResourceParameters + $result.MultipleInstances | Should -Be $testParameters.MultipleInstances + } + + } + Context 'A scheduled task exists, but it should not' { $testParameters = $getTargetResourceParameters + @{ ActionExecutable = 'C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe'