且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在工作站解锁时执行的语法

更新时间:2023-10-05 09:23:34

New-ScheduledTaskTrigger 不支持这种特定的触发器类型 (MSFT_TaskSessionStateChangeTrigger).幸运的是,它只是现有 CIM 类的包装器,您仍然可以直接使用它们:

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 ...

您可以通过在 Task Scheduler 中手动创建模板任务,然后在其上使用 Get-ScheduledTask 并检查其属性来轻松地获取所需的值.

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.

有趣的是,由于 ScheduledTasks cmdlet 仅使用 CDXML 元数据(无显式代码)定义,因此扩展模块以支持这些类型应该相对容易.不过,我还没有进一步研究.

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.