问题描述
我有一个创建计划任务的脚本,我需要它在工作站解锁"上运行,但我找不到它的语法.
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 like:
(New-ScheduledTaskTrigger -whatDoiWriteHere).
推荐答案
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.
这篇关于在工作站解锁时执行的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!