Skip to content

Commit af8d9eb

Browse files
authored
Merge pull request #142 from matt6697/dev
xScheduledTask - Fix deletion of scheduled task with unknown or empty task trigger
2 parents 39b261e + d678666 commit af8d9eb

4 files changed

Lines changed: 53 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
## Unreleased
44

55
- Fix master branch AppVeyor badge link URL in README.MD - See [Issue #140](https://github.com/PowerShell/xComputerManagement/issues/140).
6+
- Fix deletion of scheduled task with unknown or empty task trigger.
7+
Get-TargetResource returns an empty ScheduleType string if the task
8+
trigger is empty or unknown - See [Issue
9+
#137](https://github.com/PowerShell/xComputerManagement/issues/137).
610

711
## 4.0.0.0
812

Modules/xComputerManagement/DSCResources/MSFT_xScheduledTask/MSFT_xScheduledTask.psm1

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -438,9 +438,8 @@ function Get-TargetResource
438438

439439
default
440440
{
441-
New-InvalidArgumentException `
442-
-Message ($script:localizedData.TriggerTypeError -f $trigger.CimClass.CimClassName) `
443-
-ArgumentName CimClassName
441+
$returnScheduleType = ''
442+
Write-Verbose -Message ($script:localizedData.TriggerTypeUnknown -f $trigger.CimClass.CimClassName)
444443
}
445444
}
446445

Modules/xComputerManagement/DSCResources/MSFT_xScheduledTask/en-US/MSFT_xScheduledTask.strings.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ ConvertFrom-StringData @'
22
GetScheduledTaskMessage = Getting scheduled task '{0}' in '{1}'.
33
TaskNotFoundMessage = Task '{0}' not found in '{1}'. Returning an empty task with Ensure = "Absent".
44
TaskFoundMessage = Task '{0}' found in '{1}'. Retrieving settings, first action, first trigger and repetition settings.
5-
TriggerTypeError = Trigger type '{0}' not recognized.
5+
TriggerTypeUnknown = Trigger type '{0}' not recognized.
66
DetectedScheduleTypeMessage = Detected schedule type '{0}' for first trigger.
77
SetScheduledTaskMessage = Setting scheduled task '{0}' in '{1}'.
88
DisablingExistingScheduledTask = Disabling existing scheduled task '{0}' in '{1}'.

Tests/Unit/MSFT_xScheduledTask.Tests.ps1

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,6 +1494,52 @@ try
14941494
Test-TargetResource @testParameters | Should -Be $true
14951495
}
14961496
}
1497+
1498+
Context 'When a built-in scheduled task exists and is enabled, but it should be disabled and the trigger type is not recognized' {
1499+
$testParameters = @{
1500+
TaskName = 'Test task'
1501+
TaskPath = '\Test\'
1502+
Enable = $false
1503+
Verbose = $True
1504+
}
1505+
1506+
Mock -CommandName Get-ScheduledTask -MockWith {
1507+
@{
1508+
TaskName = $testParameters.TaskName
1509+
TaskPath = $testParameters.TaskPath
1510+
Actions = [pscustomobject] @{
1511+
Execute = 'C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe'
1512+
}
1513+
Triggers = [pscustomobject] @{
1514+
Repetition = @{
1515+
Duration = "PT15M"
1516+
Interval = "PT15M"
1517+
}
1518+
CimClass = @{
1519+
CimClassName = 'MSFT_TaskEventTrigger'
1520+
}
1521+
}
1522+
Settings = [pscustomobject] @{
1523+
Enabled = $true
1524+
}
1525+
} }
1526+
1527+
It 'Should return the correct values from Get-TargetResource' {
1528+
$result = Get-TargetResource @testParameters
1529+
$result.Enable | Should -Be $true
1530+
$result.Ensure | Should -Be 'Present'
1531+
$result.ScheduleType | Should -BeNullOrEmpty
1532+
}
1533+
1534+
It 'Should return false from the test method' {
1535+
Test-TargetResource @testParameters | Should -Be $false
1536+
}
1537+
1538+
It 'Should disable the scheduled task in the set method' {
1539+
Set-TargetResource @testParameters
1540+
Assert-MockCalled Register-ScheduledTask -Exactly -Times 1
1541+
}
1542+
}
14971543
}
14981544
}
14991545
#endregion

0 commit comments

Comments
 (0)