Skip to content

Commit 08c1f32

Browse files
committed
Adding support for event based triggers by the OnEvent schedule type
1 parent 12f3a7e commit 08c1f32

4 files changed

Lines changed: 260 additions & 12 deletions

File tree

Modules/ComputerManagementDsc/DSCResources/MSFT_ScheduledTask/MSFT_ScheduledTask.psm1

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ function Get-TargetResource
234234

235235
[Parameter()]
236236
[System.String]
237-
[ValidateSet('Once', 'Daily', 'Weekly', 'AtStartup', 'AtLogOn')]
237+
[ValidateSet('Once', 'Daily', 'Weekly', 'AtStartup', 'AtLogOn','OnEvent')]
238238
$ScheduleType,
239239

240240
[Parameter()]
@@ -376,7 +376,15 @@ function Get-TargetResource
376376
[Parameter()]
377377
[ValidateSet('Group', 'Interactive', 'InteractiveOrPassword', 'None', 'Password', 'S4U', 'ServiceAccount')]
378378
[System.String]
379-
$LogonType
379+
$LogonType,
380+
381+
[Parameter()]
382+
[System.String]
383+
$EventSubscription,
384+
385+
[Parameter()]
386+
[System.String]
387+
$Delay = '00:00:00'
380388
)
381389

382390
$TaskPath = ConvertTo-NormalizedTaskPath -TaskPath $TaskPath
@@ -436,6 +444,12 @@ function Get-TargetResource
436444
break
437445
}
438446

447+
'MSFT_TaskEventTrigger'
448+
{
449+
$returnScheduleType = 'OnEvent'
450+
break
451+
}
452+
439453
default
440454
{
441455
$returnScheduleType = ''
@@ -510,6 +524,8 @@ function Get-TargetResource
510524
RunOnlyIfNetworkAvailable = $settings.RunOnlyIfNetworkAvailable
511525
RunLevel = [System.String] $task.Principal.RunLevel
512526
LogonType = [System.String] $task.Principal.LogonType
527+
EventSubscription = $trigger.Subscription
528+
Delay = ConvertTo-TimeSpanStringFromScheduledTaskString -TimeSpan $trigger.Delay
513529
}
514530
}
515531
}
@@ -697,7 +713,7 @@ function Set-TargetResource
697713

698714
[Parameter()]
699715
[System.String]
700-
[ValidateSet('Once', 'Daily', 'Weekly', 'AtStartup', 'AtLogOn')]
716+
[ValidateSet('Once', 'Daily', 'Weekly', 'AtStartup', 'AtLogOn','OnEvent')]
701717
$ScheduleType,
702718

703719
[Parameter()]
@@ -839,7 +855,15 @@ function Set-TargetResource
839855
[Parameter()]
840856
[ValidateSet('Group', 'Interactive', 'InteractiveOrPassword', 'None', 'Password', 'S4U', 'ServiceAccount')]
841857
[System.String]
842-
$LogonType
858+
$LogonType,
859+
860+
[Parameter()]
861+
[System.String]
862+
$EventSubscription,
863+
864+
[Parameter()]
865+
[System.String]
866+
$Delay = '00:00:00'
843867
)
844868

845869
$TaskPath = ConvertTo-NormalizedTaskPath -TaskPath $TaskPath
@@ -902,6 +926,13 @@ function Set-TargetResource
902926
-ArgumentName DaysOfWeek
903927
}
904928

929+
if ($ScheduleType -eq 'OnEvent' -and -not([xml]$EventSubscription))
930+
{
931+
New-InvalidArgumentException `
932+
-Message ($script:localizedData.OnEventSubscriptionError) `
933+
-ArgumentName EventSubscription
934+
}
935+
905936
# Configure the action
906937
$actionParameters = @{
907938
Execute = $ActionExecutable
@@ -989,7 +1020,8 @@ function Set-TargetResource
9891020
# Configure the trigger
9901021
$triggerParameters = @{}
9911022

992-
if ($RandomDelay -gt [System.TimeSpan]::FromSeconds(0))
1023+
# A random delay is not supported when the scheduleType is set to OnEvent
1024+
if ($RandomDelay -gt [System.TimeSpan]::FromSeconds(0) -and $ScheduleType -ne 'OnEvent')
9931025
{
9941026
$triggerParameters.Add('RandomDelay', $RandomDelay)
9951027
}
@@ -1049,9 +1081,23 @@ function Set-TargetResource
10491081

10501082
break
10511083
}
1084+
1085+
'OnEvent'
1086+
{
1087+
Write-Verbose -Message ($script:localizedData.ConfigureTaskEventTrigger -f $TaskName)
1088+
1089+
$cimTriggerClass = Get-CimClass -ClassName MSFT_TaskEventTrigger -Namespace Root/Microsoft/Windows/TaskScheduler:MSFT_TaskEventTrigger
1090+
$trigger = New-CimInstance -CimClass $cimTriggerClass -ClientOnly
1091+
$trigger.Enabled = $true
1092+
$trigger.Delay = (New-ScheduledTaskTrigger -RandomDelay $Delay -Once -At (Get-Date)).RandomDelay
1093+
$trigger.Subscription = $EventSubscription
1094+
}
10521095
}
10531096

1054-
$trigger = New-ScheduledTaskTrigger @triggerParameters -ErrorAction SilentlyContinue
1097+
if($ScheduleType -ne 'OnEvent')
1098+
{
1099+
$trigger = New-ScheduledTaskTrigger @triggerParameters -ErrorAction SilentlyContinue
1100+
}
10551101

10561102
if (-not $trigger)
10571103
{
@@ -1423,7 +1469,7 @@ function Test-TargetResource
14231469

14241470
[Parameter()]
14251471
[System.String]
1426-
[ValidateSet('Once', 'Daily', 'Weekly', 'AtStartup', 'AtLogOn')]
1472+
[ValidateSet('Once', 'Daily', 'Weekly', 'AtStartup', 'AtLogOn','OnEvent')]
14271473
$ScheduleType,
14281474

14291475
[Parameter()]
@@ -1565,7 +1611,15 @@ function Test-TargetResource
15651611
[Parameter()]
15661612
[ValidateSet('Group', 'Interactive', 'InteractiveOrPassword', 'None', 'Password', 'S4U', 'ServiceAccount')]
15671613
[System.String]
1568-
$LogonType
1614+
$LogonType,
1615+
1616+
[Parameter()]
1617+
[System.String]
1618+
$EventSubscription,
1619+
1620+
[Parameter()]
1621+
[System.String]
1622+
$Delay = '00:00:00'
15691623
)
15701624

15711625
$TaskPath = ConvertTo-NormalizedTaskPath -TaskPath $TaskPath
@@ -1580,7 +1634,15 @@ function Test-TargetResource
15801634

15811635
if ($PSBoundParameters.ContainsKey('RandomDelay'))
15821636
{
1583-
$PSBoundParameters['RandomDelay'] = (ConvertTo-TimeSpanFromTimeSpanString -TimeSpanString $RandomDelay).ToString()
1637+
if($ScheduleType -eq 'OnEvent')
1638+
{
1639+
# A random delay is not supported when the ScheduleType is set to OnEvent.
1640+
$null = $PSBoundParameters.Remove('RandomDelay')
1641+
}
1642+
else
1643+
{
1644+
$PSBoundParameters['RandomDelay'] = (ConvertTo-TimeSpanFromTimeSpanString -TimeSpanString $RandomDelay).ToString()
1645+
}
15841646
}
15851647

15861648
if ($PSBoundParameters.ContainsKey('RepetitionDuration'))

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +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;
46+
[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;
4547
};

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ ConvertFrom-StringData @'
1010
DaysIntervalError = DaysInterval must be greater than zero (0) for Daily schedules. DaysInterval specified is '{0}'.
1111
WeeksIntervalError = WeeksInterval must be greater than zero (0) for Weekly schedules. WeeksInterval specified is '{0}'.
1212
WeekDayMissingError = At least one weekday must be selected for Weekly schedule.
13+
OnEventSubscriptionError = No (valid) XML Event Subscription was provided. This is required when the scheduletype is OnEvent.
1314
TriggerCreationError = Error creating new scheduled task trigger.
1415
ConfigureTriggerRepetitionMessage = Configuring trigger repetition.
1516
RepetitionIntervalError = Repetition interval is set to '{0}' but repetition duration is '{1}'.
@@ -20,6 +21,7 @@ ConvertFrom-StringData @'
2021
CreateScheduledTaskPrincipalMessage = Creating scheduled task principal for account '{0}' using logon type '{1}'.
2122
RemovePreviousScheduledTaskMessage = Removing previous scheduled task '{0}' from '{1}'.
2223
CreateNewScheduledTaskMessage = Creating new scheduled task '{0}' in '{1}'.
24+
ConfigureTaskEventTrigger = Setting up an event based trigger on task {0}
2325
SetRepetitionTriggerMessage = Setting repetition trigger settings on task '{0}' in '{1}'.
2426
RegisterScheduledTaskMessage = Registering the scheduled task '{0}' in '{1}'.
2527
RetrieveScheduledTaskMessage = Retrieving the scheduled task '{0}' from '{1}'.

0 commit comments

Comments
 (0)