Skip to content

Commit 196d491

Browse files
ScheduledTask - Update allowed values of ScheduleType to accept AtLogon (#421)
- ScheduledTask - Allowed values for ScheduleType updated to accept AtLogon over AtLogOn - Fixes Issue #420
1 parent d05adb0 commit 196d491

6 files changed

Lines changed: 73 additions & 16 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
55

66
## [Unreleased]
77

8+
- ScheduledTask
9+
- Allowed values for ScheduleType updated to accept AtLogon over AtLogOn - Fixes [Issue #420](https://github.com/dsccommunity/ComputerManagementDsc/issues/420)
10+
811
### Changed
912

1013
- ComputerManagementDsc

source/DSCResources/DSC_ScheduledTask/DSC_ScheduledTask.psm1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ function Set-TargetResource
263263

264264
[Parameter()]
265265
[System.String]
266-
[ValidateSet('Once', 'Daily', 'Weekly', 'AtStartup', 'AtLogOn', 'OnEvent')]
266+
[ValidateSet('Once', 'Daily', 'Weekly', 'AtStartup', 'AtLogon', 'OnEvent')]
267267
$ScheduleType,
268268

269269
[Parameter()]
@@ -669,9 +669,9 @@ function Set-TargetResource
669669
break
670670
}
671671

672-
'AtLogOn'
672+
'AtLogon'
673673
{
674-
$triggerParameters.Add('AtLogOn', $true)
674+
$triggerParameters.Add('AtLogon', $true)
675675

676676
if (-not [System.String]::IsNullOrWhiteSpace($User) -and $LogonType -ne 'Group')
677677
{
@@ -1156,7 +1156,7 @@ function Test-TargetResource
11561156

11571157
[Parameter()]
11581158
[System.String]
1159-
[ValidateSet('Once', 'Daily', 'Weekly', 'AtStartup', 'AtLogOn', 'OnEvent')]
1159+
[ValidateSet('Once', 'Daily', 'Weekly', 'AtStartup', 'AtLogon', 'OnEvent')]
11601160
$ScheduleType,
11611161

11621162
[Parameter()]

source/DSCResources/DSC_ScheduledTask/DSC_ScheduledTask.schema.mof

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class DSC_ScheduledTask : OMI_BaseResource
77
[Write, Description("The path to the .exe for this task.")] string ActionExecutable;
88
[Write, Description("The arguments to pass the executable.")] string ActionArguments;
99
[Write, Description("The working path to specify for the executable.")] string ActionWorkingPath;
10-
[Write, Description("When should the task be executed."), ValueMap{"Once", "Daily", "Weekly", "AtStartup", "AtLogOn", "OnEvent"}, Values{"Once", "Daily", "Weekly", "AtStartup", "AtLogOn", "OnEvent"}] string ScheduleType;
10+
[Write, Description("When should the task be executed."), ValueMap{"Once", "Daily", "Weekly", "AtStartup", "AtLogon", "OnEvent"}, Values{"Once", "Daily", "Weekly", "AtStartup", "AtLogon", "OnEvent"}] string ScheduleType;
1111
[Write, Description("How many units (minutes, hours, days) between each run of this task?")] String RepeatInterval;
1212
[Write, Description("The time of day this task should start at - defaults to 12:00 AM. Not valid for AtLogon and AtStartup tasks.")] DateTime StartTime;
1313
[Write, Description("Enable the scheduled task option to synchronize across time zones. This is enabled by including the timezone offset in the scheduled task trigger. Defaults to false which does not include the timezone offset.")] boolean SynchronizeAcrossTimeZone;

source/Examples/Resources/ScheduledTask/5-ScheduledTask_CreateScheduledTasksAtLogon_Config.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Configuration ScheduledTask_CreateScheduledTasksAtLogon_Config
3434
TaskName = 'Test task Logon'
3535
TaskPath = '\MyTasks'
3636
ActionExecutable = 'C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe'
37-
ScheduleType = 'AtLogOn'
37+
ScheduleType = 'AtLogon'
3838
RepeatInterval = '00:15:00'
3939
RepetitionDuration = '08:00:00'
4040
}

tests/Integration/DSC_ScheduledTask.config.ps1

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ Configuration ScheduledTaskLogonAdd
168168
TaskName = 'Test task Logon'
169169
TaskPath = '\ComputerManagementDsc\'
170170
ActionExecutable = 'C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe'
171-
ScheduleType = 'AtLogOn'
171+
ScheduleType = 'AtLogon'
172172
RepeatInterval = '00:15:00'
173173
RepetitionDuration = '08:00:00'
174174
}
@@ -208,7 +208,7 @@ Configuration ScheduledTaskExecuteAsAdd
208208
TaskName = 'Test task Logon'
209209
TaskPath = '\ComputerManagementDsc\'
210210
ActionExecutable = 'C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe'
211-
ScheduleType = 'AtLogOn'
211+
ScheduleType = 'AtLogon'
212212
ExecuteAsCredential = $executeAsCredential
213213
LogonType = 'Interactive'
214214
RunLevel = 'Highest'
@@ -233,7 +233,7 @@ Configuration ScheduledTaskExecuteAsGroupAdd
233233
ActionExecutable = 'C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe'
234234
LogonType = 'Group'
235235
ExecuteAsCredential = $executeAsCredential
236-
ScheduleType = 'AtLogOn'
236+
ScheduleType = 'AtLogon'
237237
RunLevel = 'Limited'
238238
}
239239
}
@@ -390,7 +390,7 @@ Configuration ScheduledTaskStartupMod
390390
TaskName = 'Test task Startup'
391391
TaskPath = '\ComputerManagementDsc\'
392392
ActionExecutable = 'C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe'
393-
ScheduleType = 'AtLogOn'
393+
ScheduleType = 'AtLogon'
394394
RepeatInterval = '00:10:00'
395395
RepetitionDuration = '08:00:00'
396396
}
@@ -412,7 +412,7 @@ Configuration ScheduledTaskExecuteAsMod
412412
TaskName = 'Test task Logon'
413413
TaskPath = '\ComputerManagementDsc\'
414414
ActionExecutable = 'C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe'
415-
ScheduleType = 'AtLogOn'
415+
ScheduleType = 'AtLogon'
416416
ExecuteAsCredential = $executeAsCredential
417417
LogonType = 'Interactive'
418418
RunLevel = 'Highest'
@@ -437,7 +437,7 @@ Configuration ScheduledTaskExecuteAsGroupMod
437437
ActionExecutable = 'C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe'
438438
LogonType = 'Group'
439439
ExecuteAsCredential = $executeAsCredential
440-
ScheduleType = 'AtLogOn'
440+
ScheduleType = 'AtLogon'
441441
RunLevel = 'Limited'
442442
}
443443
}
@@ -598,7 +598,7 @@ Configuration ScheduledTaskStartupDel
598598
TaskName = 'Test task Startup'
599599
TaskPath = '\ComputerManagementDsc\'
600600
ActionExecutable = 'C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe'
601-
ScheduleType = 'AtLogOn'
601+
ScheduleType = 'AtLogon'
602602
RepeatInterval = '00:10:00'
603603
RepetitionDuration = '08:00:00'
604604
Ensure = 'Absent'
@@ -617,7 +617,7 @@ Configuration ScheduledTaskExecuteAsDel
617617
TaskName = 'Test task Logon'
618618
TaskPath = '\ComputerManagementDsc\'
619619
ActionExecutable = 'C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe'
620-
ScheduleType = 'AtLogOn'
620+
ScheduleType = 'AtLogon'
621621
Ensure = 'Absent'
622622
}
623623
}
@@ -634,7 +634,7 @@ Configuration ScheduledTaskExecuteAsGroupDel
634634
TaskName = 'Test task Logon with BuiltIn Group'
635635
TaskPath = '\ComputerManagementDsc\'
636636
ActionExecutable = 'C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe'
637-
ScheduleType = 'AtLogOn'
637+
ScheduleType = 'AtLogon'
638638
Ensure = 'Absent'
639639
}
640640
}

tests/Unit/DSC_ScheduledTask.Tests.ps1

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2203,7 +2203,7 @@ try
22032203
$result.Enable | Should -Be $testParameters.Enable
22042204
$result.Ensure | Should -Be 'Present'
22052205
$result.StartTime | Should -Be (Get-Date -Date $startTimeString)
2206-
$result.ScheduleType | Should -Be 'AtLogon'
2206+
$result.ScheduleType | Should -BeExactly 'AtLogon'
22072207
$result.Delay | Should -Be $testParameters.Delay
22082208
}
22092209

@@ -2212,6 +2212,60 @@ try
22122212
}
22132213
}
22142214

2215+
Context "When scheduling a task to trigger at user logon" {
2216+
BeforeAll {
2217+
Mock -CommandName New-ScheduledTaskTrigger -MockWith {
2218+
$cimInstance = New-CIMInstance -ClassName 'MSFT_TaskLogonTrigger' -Namespace 'root\Microsoft\Windows\TaskScheduler' -Property @{
2219+
# Fill the CIM instance with the properties we expect to be used by the resource.
2220+
UserId = $testParameters.User
2221+
Delay = ''
2222+
} -ClientOnly
2223+
2224+
<#
2225+
Must add the TypeName property to the CIM instance for the array .PSObject.PSTypeNames
2226+
to have the correct name for it to be recognized by the New-ScheduledTask command.
2227+
#>
2228+
$cimInstance | Add-Member -TypeName 'Microsoft.Management.Infrastructure.CimInstance#MSFT_TaskTrigger'
2229+
2230+
return $cimInstance
2231+
}
2232+
2233+
Mock -CommandName New-ScheduledTask -MockWith {
2234+
<#
2235+
Mock an object with properties that are used by the resource
2236+
for the newly created scheduled task.
2237+
#>
2238+
return [PSCustomObject] @{
2239+
Triggers = @(
2240+
@{
2241+
StartBoundary = '2018-09-27T18:45:08+02:00'
2242+
}
2243+
)
2244+
}
2245+
}
2246+
2247+
Mock -CommandName Register-ScheduledTask
2248+
}
2249+
2250+
It "Should correctly configure the task with 'AtLogon' ScheduleType and the specified user" {
2251+
$testParameters = $getTargetResourceParameters + @{
2252+
ScheduleType = 'AtLogon'
2253+
User = 'MockedUser'
2254+
ActionExecutable = 'C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe'
2255+
LogonType = 'Password'
2256+
}
2257+
2258+
Set-TargetResource @testParameters
2259+
2260+
Assert-MockCalled -CommandName New-ScheduledTaskTrigger -ParameterFilter {
2261+
$AtLogon -eq $true -and $User -eq 'MockedUser'
2262+
} -Exactly -Times 1 -Scope It
2263+
2264+
Assert-MockCalled -CommandName New-ScheduledTask -Exactly -Times 1 -Scope It
2265+
}
2266+
}
2267+
2268+
22152269
Context 'When a scheduled task is configured with the ScheduleType AtStartup and is in desired state' {
22162270
$testParameters = $getTargetResourceParameters + @{
22172271
ActionExecutable = 'C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe'

0 commit comments

Comments
 (0)