Skip to content

Commit 286ae32

Browse files
authored
Merge pull request #118 from PlagueHO/Issue-115
Fix ExecutionTimeLimit to Enable No Limit - Fixes #115
2 parents 7a2c2db + 91d6a60 commit 286ae32

6 files changed

Lines changed: 104 additions & 30 deletions

File tree

DSCResources/MSFT_xScheduledTask/MSFT_xScheduledTask.psm1

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ function Set-TargetResource
915915
$settingParameters.Add('IdleWaitTimeout', $IdleWaitTimeout)
916916
}
917917

918-
if ($ExecutionTimeLimit -gt [System.TimeSpan] '00:00:00')
918+
if ($PSBoundParameters.ContainsKey('ExecutionTimeLimit'))
919919
{
920920
$settingParameters.Add('ExecutionTimeLimit', $ExecutionTimeLimit)
921921
}
@@ -932,6 +932,17 @@ function Set-TargetResource
932932

933933
$setting = New-ScheduledTaskSettingsSet @settingParameters
934934

935+
<#
936+
On Windows Server 2012 R2 setting a blank timespan for ExecutionTimeLimit
937+
does not result in the PT0S timespan value being set. So set this
938+
if it has not been set.
939+
#>
940+
if ($PSBoundParameters.ContainsKey('ExecutionTimeLimit') -and `
941+
[System.String]::IsNullOrEmpty($setting.ExecutionTimeLimit))
942+
{
943+
$setting.ExecutionTimeLimit = 'PT0S'
944+
}
945+
935946
$triggerParameters = @{}
936947

937948
if ($RandomDelay -gt [System.TimeSpan]::FromSeconds(0))

Examples/xScheduledTask/1-CreateScheduledTaskOnce.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
task folder 'MyTasks' that starts a new powershell process once at 00:00 repeating
55
every 15 minutes for 8 hours. The task is delayed by a random amount up to 1 hour
66
each time. The task will run even if the previous task is still running and it
7-
will prevent hard termintaing of the previously running task instance.
7+
will prevent hard termintaing of the previously running task instance. The task
8+
execution will have no time limit.
89
#>
910
Configuration Example
1011
{
@@ -27,6 +28,7 @@ Configuration Example
2728
ScheduleType = 'Once'
2829
RepeatInterval = '00:15:00'
2930
RepetitionDuration = '08:00:00'
31+
ExecutionTimeLimit = '00:00:00'
3032
ActionWorkingPath = (Get-Location).Path
3133
Enable = $true
3234
RandomDelay = '01:00:00'

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ xVirtualMemory has the following properties:
217217

218218
### Unreleased
219219

220+
* xScheduledTask:
221+
* Enable Execution Time Limit of task to be set to indefinite
222+
by setting `ExecutionTimeLimit` to '00:00:00' - See [Issue #115](https://github.com/PowerShell/xComputerManagement/issues/115)
220223
* xPowerPlan:
221224
* Updated to meet HQRM guidelines.
222225
* Converted calls to `throw` to use `New-InvalidOperationException`

Tests/Integration/MSFT_xScheduledTask.Config.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Configuration xScheduledTaskOnceCrossTimezone
1717
DisallowHardTerminate = $true
1818
RunOnlyIfIdle = $false
1919
Priority = 9
20+
ExecutionTimeLimit = '00:00:00'
2021
}
2122
}
2223
}
@@ -40,6 +41,7 @@ Configuration xScheduledTaskOnceAdd
4041
DisallowHardTerminate = $true
4142
RunOnlyIfIdle = $false
4243
Priority = 9
44+
ExecutionTimeLimit = '00:00:00'
4345
}
4446
}
4547
}
@@ -180,6 +182,7 @@ Configuration xScheduledTaskOnceMod
180182
RepeatInterval = '00:20:00'
181183
RepetitionDuration = '08:00:00'
182184
DisallowDemandStart = $true
185+
ExecutionTimeLimit = '02:00:00'
183186
}
184187
}
185188
}

Tests/Integration/MSFT_xScheduledTask.Integration.Tests.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ try
186186
$current.RunOnlyIfIdle | Should Be $false
187187
$current.Priority | Should Be 9
188188
$current.RunLevel | Should Be 'Limited'
189+
$current.ExecutionTimeLimit | Should Be '00:00:00'
189190
}
190191

191192
AfterAll {

Tests/Unit/MSFT_xScheduledTask.Tests.ps1

Lines changed: 82 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ try
179179

180180
It 'Should update the scheduled task in the set method' {
181181
Set-TargetResource @testParameters
182-
Assert-MockCalled -CommandName Unregister-ScheduledTask -Times 1
183-
Assert-Mockcalled -CommandName Register-ScheduledTask -Times 1
182+
Assert-MockCalled -CommandName Unregister-ScheduledTask -Exactly -Times 1
183+
Assert-Mockcalled -CommandName Register-ScheduledTask -Exactly -Times 1
184184
}
185185
}
186186

@@ -267,8 +267,8 @@ try
267267

268268
It 'Should update the scheduled task in the set method' {
269269
Set-TargetResource @testParameters
270-
Assert-MockCalled -CommandName Unregister-ScheduledTask -Times 1
271-
Assert-Mockcalled -CommandName Register-ScheduledTask -Times 1
270+
Assert-MockCalled -CommandName Unregister-ScheduledTask -Exactly -Times 1
271+
Assert-Mockcalled -CommandName Register-ScheduledTask -Exactly -Times 1
272272
}
273273
}
274274

@@ -354,8 +354,8 @@ try
354354

355355
It 'Should update the scheduled task in the set method' {
356356
Set-TargetResource @testParameters
357-
Assert-MockCalled -CommandName Unregister-ScheduledTask -Times 1
358-
Assert-Mockcalled -CommandName Register-ScheduledTask -Times 1
357+
Assert-MockCalled -CommandName Unregister-ScheduledTask -Exactly -Times 1
358+
Assert-Mockcalled -CommandName Register-ScheduledTask -Exactly -Times 1
359359
}
360360
}
361361

@@ -439,8 +439,8 @@ try
439439

440440
It 'Should update the scheduled task in the set method' {
441441
Set-TargetResource @testParameters
442-
Assert-MockCalled -CommandName Unregister-ScheduledTask -Times 1
443-
Assert-Mockcalled -CommandName Register-ScheduledTask -Times 1
442+
Assert-MockCalled -CommandName Unregister-ScheduledTask -Exactly -Times 1
443+
Assert-Mockcalled -CommandName Register-ScheduledTask -Exactly -Times 1
444444
}
445445
}
446446

@@ -490,8 +490,8 @@ try
490490

491491
It 'Should update the scheduled task in the set method' {
492492
Set-TargetResource @testParameters
493-
Assert-MockCalled -CommandName Unregister-ScheduledTask -Times 1
494-
Assert-Mockcalled -CommandName Register-ScheduledTask -Times 1
493+
Assert-MockCalled -CommandName Unregister-ScheduledTask -Exactly -Times 1
494+
Assert-Mockcalled -CommandName Register-ScheduledTask -Exactly -Times 1
495495
}
496496
}
497497

@@ -541,8 +541,8 @@ try
541541

542542
It 'Should update the scheduled task in the set method' {
543543
Set-TargetResource @testParameters
544-
Assert-MockCalled -CommandName Unregister-ScheduledTask -Times 1
545-
Assert-Mockcalled -CommandName Register-ScheduledTask -Times 1
544+
Assert-MockCalled -CommandName Unregister-ScheduledTask -Exactly -Times 1
545+
Assert-Mockcalled -CommandName Register-ScheduledTask -Exactly -Times 1
546546
}
547547
}
548548

@@ -590,8 +590,8 @@ try
590590

591591
It 'Should update the scheduled task in the set method' {
592592
Set-TargetResource @testParameters
593-
Assert-MockCalled -CommandName Unregister-ScheduledTask -Times 1
594-
Assert-Mockcalled -CommandName Register-ScheduledTask -Times 1
593+
Assert-MockCalled -CommandName Unregister-ScheduledTask -Exactly -Times 1
594+
Assert-Mockcalled -CommandName Register-ScheduledTask -Exactly -Times 1
595595
}
596596
}
597597

@@ -639,8 +639,8 @@ try
639639

640640
It 'Should update the scheduled task in the set method' {
641641
Set-TargetResource @testParameters
642-
Assert-MockCalled -CommandName Unregister-ScheduledTask -Times 1
643-
Assert-Mockcalled -CommandName Register-ScheduledTask -Times 1
642+
Assert-MockCalled -CommandName Unregister-ScheduledTask -Exactly -Times 1
643+
Assert-Mockcalled -CommandName Register-ScheduledTask -Exactly -Times 1
644644
}
645645
}
646646

@@ -691,12 +691,66 @@ try
691691

692692
It 'Should update the scheduled task in the set method' {
693693
Set-TargetResource @testParameters
694-
Assert-MockCalled -CommandName Unregister-ScheduledTask -Times 1
695-
Assert-Mockcalled -CommandName Register-ScheduledTask -Times 1
694+
Assert-MockCalled -CommandName Unregister-ScheduledTask -Exactly -Times 1
695+
Assert-Mockcalled -CommandName Register-ScheduledTask -Exactly -Times 1
696696
}
697697

698698
}
699699

700+
Context 'A scheduled task is enabled without an execution time limit and but has an execution time limit set' {
701+
$testParameters = @{
702+
TaskName = 'Test task'
703+
TaskPath = '\Test\'
704+
ActionExecutable = 'C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe'
705+
ScheduleType = 'Once'
706+
RepeatInterval = (New-TimeSpan -Minutes 15).ToString()
707+
RepetitionDuration = (New-TimeSpan -Hours 8).ToString()
708+
ExecutionTimeLimit = (New-TimeSpan -Seconds 0).ToString()
709+
Enable = $true
710+
Verbose = $True
711+
}
712+
713+
Mock -CommandName Get-ScheduledTask { return @{
714+
TaskName = $testParameters.TaskName
715+
TaskPath = $testParameters.TaskPath
716+
Actions = @(@{
717+
Execute = $testParameters.ActionExecutable
718+
Arguments = $testParameters.Arguments
719+
})
720+
Triggers = @(@{
721+
Repetition = @{
722+
Duration = "PT$([System.TimeSpan]::Parse($testParameters.RepetitionDuration).TotalHours)H"
723+
Interval = "PT$([System.TimeSpan]::Parse($testParameters.RepeatInterval).TotalMinutes)M"
724+
}
725+
CimClass = @{
726+
CimClassName = 'MSFT_TaskTimeTrigger'
727+
}
728+
})
729+
Settings = @(@{
730+
Enabled = $true
731+
ExecutionTimeLimit = "PT$([System.TimeSpan]::Parse($testParameters.RepeatInterval).TotalSeconds + 60)S"
732+
})
733+
Principal = @{
734+
UserId = 'SYSTEM'
735+
}
736+
} }
737+
738+
It 'Should return the correct values from Get-TargetResource' {
739+
$result = Get-TargetResource @testParameters
740+
$result.Ensure | Should Be 'Present'
741+
}
742+
743+
It 'Should return false from the test method' {
744+
Test-TargetResource @testParameters | Should Be $false
745+
}
746+
747+
It 'Should update the scheduled task in the set method' {
748+
Set-TargetResource @testParameters
749+
Assert-MockCalled -CommandName Unregister-ScheduledTask -Exactly -Times 1
750+
Assert-Mockcalled -CommandName Register-ScheduledTask -Exactly -Times 1
751+
}
752+
}
753+
700754
Context 'A scheduled task is enabled and has the correct settings' {
701755
$testParameters = @{
702756
TaskName = 'Test task'
@@ -848,8 +902,8 @@ try
848902

849903
It 'Should update the scheduled task in the set method' {
850904
Set-TargetResource @testParameters
851-
Assert-MockCalled -CommandName Unregister-ScheduledTask -Times 1
852-
Assert-Mockcalled -CommandName Register-ScheduledTask -Times 1
905+
Assert-MockCalled -CommandName Unregister-ScheduledTask -Exactly -Times 1
906+
Assert-Mockcalled -CommandName Register-ScheduledTask -Exactly -Times 1
853907
}
854908
}
855909

@@ -977,8 +1031,8 @@ try
9771031

9781032
It 'Should update the scheduled task in the set method' {
9791033
Set-TargetResource @testParameters
980-
Assert-MockCalled -CommandName Unregister-ScheduledTask -Times 1
981-
Assert-Mockcalled -CommandName Register-ScheduledTask -Times 1
1034+
Assert-MockCalled -CommandName Unregister-ScheduledTask -Exactly -Times 1
1035+
Assert-Mockcalled -CommandName Register-ScheduledTask -Exactly -Times 1
9821036
}
9831037
}
9841038

@@ -1039,8 +1093,8 @@ try
10391093

10401094
It 'Should update the scheduled task in the set method' {
10411095
Set-TargetResource @testParameters
1042-
Assert-MockCalled -CommandName Unregister-ScheduledTask -Times 1
1043-
Assert-Mockcalled -CommandName Register-ScheduledTask -Times 1
1096+
Assert-MockCalled -CommandName Unregister-ScheduledTask -Exactly -Times 1
1097+
Assert-Mockcalled -CommandName Register-ScheduledTask -Exactly -Times 1
10441098
}
10451099
}
10461100

@@ -1087,8 +1141,8 @@ try
10871141

10881142
It 'Should update the scheduled task in the set method' {
10891143
Set-TargetResource @testParameters
1090-
Assert-MockCalled -CommandName Unregister-ScheduledTask -Times 1
1091-
Assert-Mockcalled -CommandName Register-ScheduledTask -Times 1
1144+
Assert-MockCalled -CommandName Unregister-ScheduledTask -Exactly -Times 1
1145+
Assert-Mockcalled -CommandName Register-ScheduledTask -Exactly -Times 1
10921146
}
10931147
}
10941148

@@ -1135,8 +1189,8 @@ try
11351189

11361190
It 'Should update the scheduled task in the set method' {
11371191
Set-TargetResource @testParameters
1138-
Assert-MockCalled -CommandName Unregister-ScheduledTask -Times 1
1139-
Assert-Mockcalled -CommandName Register-ScheduledTask -Times 1
1192+
Assert-MockCalled -CommandName Unregister-ScheduledTask -Exactly -Times 1
1193+
Assert-Mockcalled -CommandName Register-ScheduledTask -Exactly -Times 1
11401194
}
11411195
}
11421196

0 commit comments

Comments
 (0)