答案1
New-ScheduledTaskTrigger
不幸的是,没有办法创建事件驱动触发器。PS 社区有要求实施。
我们应该能够将你正在处理的 XML 与代码结合起来使用 .NET 创建任务。只需替换用户、密码和服务名称。
$taskRunAsuser = "domain\user"
$taskRunAsUserPwd = "password"
$Service = new-object -ComObject ("Schedule.Service")
$Service.Connect($env:computername)
$RootFolder = $Service.GetFolder("\")
$TaskDefinition = $Service.NewTask(0)
$settings = $taskDefinition.Settings
$settings.Enabled = $true
$settings.StartWhenAvailable = $true
$settings.Hidden = $false
$Triggers = $TaskDefinition.Triggers
$Trigger = $Triggers.Create(0)
#$Trigger.Id = '4800'
$Trigger.Subscription = "<QueryList>
<Query Id='0'>
<Select Path='System'>
*[EventData[Data[@Name='param1'] and (Data='YOUR SERVICE NAME')]]
and
*[EventData[Data[@Name='param2'] and (Data='running')]]
</Select>
</Query>
</QueryList>"
$Trigger.Enabled = $true
$Action = $TaskDefinition.Actions.Create(0)
$Action.Path = 'ping.exe'
$Action.Arguments = '8.8.8.8 > C:\ping.log'
$rootFolder.RegisterTaskDefinition('Test',$TaskDefinition,6,$taskRunAsUser,$taskRunAsUserPwd,1)