Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xScheduledTask - Fix deletion of scheduled task with unknown or empty task trigger #142

Merged
merged 4 commits into from
Feb 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
## Unreleased

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

## 4.0.0.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,9 +438,8 @@ function Get-TargetResource

default
{
New-InvalidArgumentException `
-Message ($script:localizedData.TriggerTypeError -f $trigger.CimClass.CimClassName) `
-ArgumentName CimClassName
$returnScheduleType = ''
Write-Verbose -Message ($script:localizedData.TriggerTypeUnknown -f $trigger.CimClass.CimClassName)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ ConvertFrom-StringData @'
GetScheduledTaskMessage = Getting scheduled task '{0}' in '{1}'.
TaskNotFoundMessage = Task '{0}' not found in '{1}'. Returning an empty task with Ensure = "Absent".
TaskFoundMessage = Task '{0}' found in '{1}'. Retrieving settings, first action, first trigger and repetition settings.
TriggerTypeError = Trigger type '{0}' not recognized.
TriggerTypeUnknown = Trigger type '{0}' not recognized.
DetectedScheduleTypeMessage = Detected schedule type '{0}' for first trigger.
SetScheduledTaskMessage = Setting scheduled task '{0}' in '{1}'.
DisablingExistingScheduledTask = Disabling existing scheduled task '{0}' in '{1}'.
Expand Down
46 changes: 46 additions & 0 deletions Tests/Unit/MSFT_xScheduledTask.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1494,6 +1494,52 @@ try
Test-TargetResource @testParameters | Should -Be $true
}
}

Context 'When a built-in scheduled task exists and is enabled, but it should be disabled and the trigger type is not recognized' {
$testParameters = @{
TaskName = 'Test task'
TaskPath = '\Test\'
Enable = $false
Verbose = $True
}

Mock -CommandName Get-ScheduledTask -MockWith {
@{
TaskName = $testParameters.TaskName
TaskPath = $testParameters.TaskPath
Actions = [pscustomobject] @{
Execute = 'C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe'
}
Triggers = [pscustomobject] @{
Repetition = @{
Duration = "PT15M"
Interval = "PT15M"
}
CimClass = @{
CimClassName = 'MSFT_TaskEventTrigger'
}
}
Settings = [pscustomobject] @{
Enabled = $true
}
} }

It 'Should return the correct values from Get-TargetResource' {
$result = Get-TargetResource @testParameters
$result.Enable | Should -Be $true
$result.Ensure | Should -Be 'Present'
$result.ScheduleType | Should -BeNullOrEmpty
}

It 'Should return false from the test method' {
Test-TargetResource @testParameters | Should -Be $false
}

It 'Should disable the scheduled task in the set method' {
Set-TargetResource @testParameters
Assert-MockCalled Register-ScheduledTask -Exactly -Times 1
}
}
}
}
#endregion
Expand Down