Syntax for Execute On Workstation Unlock

前端 未结 1 363
被撕碎了的回忆
被撕碎了的回忆 2021-01-16 04:49

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

相关标签:
1条回答
  • 2021-01-16 05:18

    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.

    0 讨论(0)
提交回复
热议问题