I have a script that creates a scheduled task and I need it to run \"on workstation unlock\", but I can\'t find the syntax for it.
My intention is to do something l
New-ScheduledTaskTrigger
has no support for this particular trigger type (MSFT_TaskSessionStateChangeTrigger
). Fortunately it's just a wrapper around the existing CIM classes, and you can still use those directly:
$stateChangeTrigger = Get-CimClass `
-Namespace ROOT\Microsoft\Windows\TaskScheduler `
-ClassName MSFT_TaskSessionStateChangeTrigger
$onUnlockTrigger = New-CimInstance `
-CimClass $stateChangeTrigger `
-Property @{
StateChange = 8 # TASK_SESSION_STATE_CHANGE_TYPE.TASK_SESSION_UNLOCK (taskschd.h)
} `
-ClientOnly
$task = New-ScheduledTask -Trigger $onUnlockTrigger -Action ...
Register-ScheduledTask -InputObject $task ...
You can easily crib the required values by creating a template task manually in Task Scheduler, then using Get-ScheduledTask
on that and inspecting its properties.
Interestingly, because the ScheduledTasks
cmdlets are defined using CDXML metadata only (no explicit code) it should be relatively easy to extend the module to support these types. I haven't looked into that further, though.