Skip to content

Commit ad01864

Browse files
committed
Fixing code based on review adding integration tests and changing the way to get a DateInterval string to use System.Xml.XmlConvert
1 parent 81c3e83 commit ad01864

7 files changed

Lines changed: 112 additions & 48 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- Changed the scope from Global to Script ComputerManagementDsc.Common.Tests.ps1
1212
- ScheduledTask:
1313
- Added support for event based triggers, implemented using the ScheduleType OnEvent
14+
fixes [Issue #167](https://github.com/PowerShell/ComputerManagementDsc/issues/167)
1415

1516
## 5.1.0.0
1617

Modules/ComputerManagementDsc/DSCResources/MSFT_ScheduledTask/MSFT_ScheduledTask.psm1

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,8 @@ $script:localizedData = Get-LocalizedData `
204204
205205
.PARAMETER EventSubscription
206206
The event subscription in a string that can be parsed as valid XML. This parameter is only
207-
valid in combination with the OnEvent Schedule Type.
207+
valid in combination with the OnEvent Schedule Type. For the query schema please check:
208+
https://docs.microsoft.com/en-us/windows/desktop/WES/queryschema-schema
208209
209210
.PARAMETER Delay
210211
The time to wait after an event based trigger was triggered. This parameter is only
@@ -692,7 +693,8 @@ function Get-TargetResource
692693
693694
.PARAMETER EventSubscription
694695
The event subscription in a string that can be parsed as valid XML. This parameter is only
695-
valid in combination with the OnEvent Schedule Type.
696+
valid in combination with the OnEvent Schedule Type. For the query schema please check:
697+
https://docs.microsoft.com/en-us/windows/desktop/WES/queryschema-schema
696698
697699
.PARAMETER Delay
698700
The time to wait after an event based trigger was triggered. This parameter is only
@@ -942,7 +944,7 @@ function Set-TargetResource
942944
-ArgumentName DaysOfWeek
943945
}
944946

945-
if ($ScheduleType -eq 'OnEvent' -and -not([xml]$EventSubscription))
947+
if ($ScheduleType -eq 'OnEvent' -and -not ([xml]$EventSubscription))
946948
{
947949
New-InvalidArgumentException `
948950
-Message ($script:localizedData.OnEventSubscriptionError) `
@@ -1098,19 +1100,19 @@ function Set-TargetResource
10981100
break
10991101
}
11001102

1101-
'OnEvent'
1103+
'OnEvent'
11021104
{
1103-
Write-Verbose -Message ($script:localizedData.ConfigureTaskEventTrigger -f $TaskName)
1105+
Write-Verbose -Message ($script:localizedData.ConfigureTaskEventTrigger -f $TaskName)
11041106

11051107
$cimTriggerClass = Get-CimClass -ClassName MSFT_TaskEventTrigger -Namespace Root/Microsoft/Windows/TaskScheduler:MSFT_TaskEventTrigger
11061108
$trigger = New-CimInstance -CimClass $cimTriggerClass -ClientOnly
11071109
$trigger.Enabled = $true
1108-
$trigger.Delay = (New-ScheduledTaskTrigger -RandomDelay $Delay -Once -At (Get-Date)).RandomDelay
1110+
$trigger.Delay = [System.Xml.XmlConvert]::ToString([timespan]$Delay)
11091111
$trigger.Subscription = $EventSubscription
11101112
}
11111113
}
11121114

1113-
if($ScheduleType -ne 'OnEvent')
1115+
if ($ScheduleType -ne 'OnEvent')
11141116
{
11151117
$trigger = New-ScheduledTaskTrigger @triggerParameters -ErrorAction SilentlyContinue
11161118
}
@@ -1455,7 +1457,8 @@ function Set-TargetResource
14551457
14561458
.PARAMETER EventSubscription
14571459
The event subscription in a string that can be parsed as valid XML. This parameter is only
1458-
valid in combination with the OnEvent Schedule Type.
1460+
valid in combination with the OnEvent Schedule Type. For the query schema please check:
1461+
https://docs.microsoft.com/en-us/windows/desktop/WES/queryschema-schema
14591462
14601463
.PARAMETER Delay
14611464
The time to wait after an event based trigger was triggered. This parameter is only
@@ -1658,9 +1661,10 @@ function Test-TargetResource
16581661

16591662
if ($PSBoundParameters.ContainsKey('RandomDelay'))
16601663
{
1661-
if($ScheduleType -eq 'OnEvent')
1664+
if ($ScheduleType -eq 'OnEvent')
16621665
{
16631666
# A random delay is not supported when the ScheduleType is set to OnEvent.
1667+
Write-Verbose -Message ($script:localizedData.IgnoreRandomDelayWithTriggerTypeOnEvent -f $TaskName)
16641668
$null = $PSBoundParameters.Remove('RandomDelay')
16651669
}
16661670
else

Modules/ComputerManagementDsc/DSCResources/MSFT_ScheduledTask/MSFT_ScheduledTask.schema.mof

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@ class MSFT_ScheduledTask : OMI_BaseResource
4242
[Write, Description("Indicates that Task Scheduler runs the task only when a network is available. Task Scheduler uses the NetworkID parameter and NetworkName parameter that you specify in this cmdlet to determine if the network is available.")] Boolean RunOnlyIfNetworkAvailable;
4343
[Write, Description("Specifies the level of user rights that Task Scheduler uses to run the tasks that are associated with the principal. Defaults to 'Limited'."), ValueMap{"Limited","Highest"}, Values{"Limited","Highest"}] String RunLevel;
4444
[Write, Description("Specifies the security logon method that Task Scheduler uses to run the tasks that are associated with the principal."), ValueMap{"Group","Interactive","InteractiveOrPassword","None","Password","S4U","ServiceAccount"}, Values{"Group","Interactive","InteractiveOrPassword","None","Password","S4U","ServiceAccount"}] String LogonType;
45-
[Write, Description("Specifies the EventSubscription in XML. This can be easily generated using the Windows Eventlog Viewer. Can only be used in combination with ScheduleType OnEvent")] String EventSubscription;
45+
[Write, Description("Specifies the EventSubscription in XML. This can be easily generated using the Windows Eventlog Viewer. For the query schema please check: https://docs.microsoft.com/en-us/windows/desktop/WES/queryschema-schema. Can only be used in combination with ScheduleType OnEvent")] String EventSubscription;
4646
[Write, Description("Specifies a delay to the start of the trigger. The delay is a static delay before the task is executed. Can only be used in combination with ScheduleType OnEvent")] String Delay;
4747
};

Modules/ComputerManagementDsc/DSCResources/MSFT_ScheduledTask/en-US/MSFT_ScheduledTask.strings.psd1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ ConvertFrom-StringData @'
2121
CreateScheduledTaskPrincipalMessage = Creating scheduled task principal for account '{0}' using logon type '{1}'.
2222
RemovePreviousScheduledTaskMessage = Removing previous scheduled task '{0}' from '{1}'.
2323
CreateNewScheduledTaskMessage = Creating new scheduled task '{0}' in '{1}'.
24-
ConfigureTaskEventTrigger = Setting up an event based trigger on task {0}
24+
ConfigureTaskEventTrigger = Setting up an event based trigger on task {0}.
25+
IgnoreRandomDelayWithTriggerTypeOnEvent = The parameter RandomDelay in task {0} is ignored. A random delay is not supported when the trigger type is set to OnEvent.
2526
SetRepetitionTriggerMessage = Setting repetition trigger settings on task '{0}' in '{1}'.
2627
RegisterScheduledTaskMessage = Registering the scheduled task '{0}' in '{1}'.
2728
RetrieveScheduledTaskMessage = Retrieving the scheduled task '{0}' from '{1}'.

Tests/Integration/MSFT_ScheduledTask.Config.ps1

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,25 @@ Configuration ScheduledTaskExecuteAsAdd
168168
}
169169
}
170170

171+
Configuration ScheduledTaskOnEventAdd
172+
{
173+
Import-DscResource -ModuleName ComputerManagementDsc
174+
node 'localhost'
175+
{
176+
ScheduledTask ScheduledTaskOnEventAdd
177+
{
178+
TaskName = 'Test task OnEvent'
179+
TaskPath = '\ComputerManagementDsc\'
180+
Ensure = 'Present'
181+
ScheduleType = 'OnEvent'
182+
ActionExecutable = 'C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe'
183+
ActionArguments = '-Command Set-Content -Path c:\temp\seeme.txt -Value ''Worked!'''
184+
EventSubscription = '<QueryList><Query Id="0" Path="System"><Select Path="System">*[System[Provider[@Name=''Service Control Manager''] and (Level=2) and (EventID=7001)]]</Select></Query></QueryList>'
185+
Delay = '00:00:30'
186+
}
187+
}
188+
}
189+
171190
Configuration ScheduledTaskOnceMod
172191
{
173192
Import-DscResource -ModuleName ComputerManagementDsc
@@ -294,6 +313,25 @@ Configuration ScheduledTaskExecuteAsMod
294313
}
295314
}
296315

316+
Configuration ScheduledTaskOnEventMod
317+
{
318+
Import-DscResource -ModuleName ComputerManagementDsc
319+
node 'localhost'
320+
{
321+
ScheduledTask ScheduledTaskOnEventMod
322+
{
323+
TaskName = 'Test task OnEvent'
324+
TaskPath = '\ComputerManagementDsc\'
325+
Ensure = 'Present'
326+
ScheduleType = 'OnEvent'
327+
ActionExecutable = 'C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe'
328+
ActionArguments = '-Command Set-Content -Path c:\temp\seeme.txt -Value ''Worked!'''
329+
EventSubscription = '<QueryList><Query Id="0" Path="System"><Select Path="System">*[System[Provider[@Name=''Service Control Manager''] and (Level=2) and (EventID=7002)]]</Select></Query></QueryList>'
330+
Delay = '00:00:45'
331+
}
332+
}
333+
}
334+
297335
Configuration ScheduledTaskOnceDel
298336
{
299337
Import-DscResource -ModuleName ComputerManagementDsc
@@ -425,6 +463,25 @@ Configuration ScheduledTaskExecuteAsDel
425463
}
426464
}
427465

466+
Configuration ScheduledTaskOnEventDel
467+
{
468+
Import-DscResource -ModuleName ComputerManagementDsc
469+
node 'localhost'
470+
{
471+
ScheduledTask ScheduledTaskOnEventDel
472+
{
473+
TaskName = 'Test task OnEvent'
474+
TaskPath = '\ComputerManagementDsc\'
475+
Ensure = 'Absent'
476+
ScheduleType = 'OnEvent'
477+
ActionExecutable = 'C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe'
478+
ActionArguments = '-Command Set-Content -Path c:\temp\seeme.txt -Value ''Worked!'''
479+
EventSubscription = '<QueryList><Query Id="0" Path="System"><Select Path="System">*[System[Provider[@Name=''Service Control Manager''] and (Level=2) and (EventID=7001)]]</Select></Query></QueryList>'
480+
Delay = '00:00:30'
481+
}
482+
}
483+
}
484+
428485
Configuration ScheduledTaskDisableBuiltIn
429486
{
430487
Import-DscResource -ModuleName ComputerManagementDsc

Tests/Integration/MSFT_ScheduledTask.Integration.Tests.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ try
3636
AtLogon = 'ScheduledTaskLogon'
3737
AtStartup = 'ScheduledTaskStartup'
3838
ExecuteAs = 'ScheduledTaskExecuteAs'
39+
OnEvent = 'ScheduledTaskOnEvent'
3940
}
4041

4142
$configData = @{

0 commit comments

Comments
 (0)