简体   繁体   English

如何从计划任务中删除触发器

[英]How can I remove a trigger from a scheduled task

I'm using the Task Scheduler cmdlet in PowerShell to configure triggers for a scheduled task of a Perfmon Data Collector set. 我在PowerShell中使用Task Scheduler cmdlet为Perfmon Data Collector集的计划任务配置触发器。 I'm using the following to modify the Scheduled task and start the data collector set when the server starts: 我正在使用以下内容修改计划任务并在服务器启动时启动数据收集器集:

$trigger = New-ScheduledTaskTrigger -AtStartup
Set-ScheduledTask -TaskPath '\Microsoft\Windows\PLA\' -Taskname $TemplateName -Trigger $trigger

But I want to know if there is a way of removing this trigger from the scheduled task config. 但我想知道是否有一种方法可以从计划任务配置中删除此触发器。 I don't want to disable the Scheduled task, just want to remove this trigger from the Scheduled Task. 我不想禁用计划任务,只想从计划任务中删除此触发器。

Doesn't look like there is a way of doing that from the PowerShell cmdlets. 看起来有一种方法可以从PowerShell cmdlet中执行此操作。 Any help will be appreciated. 任何帮助将不胜感激。

There doesn't appear to be a PowerShell cmdlet to delete a trigger, but you can do it through the Schedule.Service COM object. 似乎没有用于删除触发器的PowerShell cmdlet,但您可以通过Schedule.Service COM对象执行此操作。 The following example will remove the first trigger from the task named MyTask. 以下示例将从名为MyTask的任务中删除第一个触发器。

You may need to add a loop to inspect the triggers in order to find the specific trigger you want to remove based on the start time or other criteria. 您可能需要添加一个循环来检查触发器,以便根据开始时间或其他条件查找要删除的特定触发器。

Note: The trigger enumerations are one-based, not zero-based, so to remove the first trigger it is $definition.Triggers.Remove(1) 注意:触发器枚举是从一开始的,而不是从零开始的,所以要删除第一个触发器,它是$definition.Triggers.Remove(1)

Credit to Changing Scheduled Tasks with PowerShell for much of this. 归功于使用PowerShell更改计划任务的大部分内容。

$taskName = "MyTask"
$triggerToDelete = 1

# connect to Task Scheduler:
$service = New-Object -ComObject Schedule.Service
$service.Connect($env:COMPUTERNAME)

# pick a specific task in a container:
$folder = $service.GetFolder('\')
$task = $folder.GetTask($taskName)

# get task definition and change it:
$definition = $task.Definition
$definition.Triggers.Remove($triggerToDelete)

# write back changed task definition:
# 4 = Update
$folder.RegisterTaskDefinition($task.Name, $definition, 4, $null, $null, $null)

To specifically delete all triggers that are "AtStartup" triggers, replace the # get task definition and change it: section with the following. 要专门删除所有“AtStartup”触发器的触发器,请使用以下内容替换#get # get task definition and change it: section。 Note that it is looping through the triggers backwards, because if one is removed, the IDs of any following triggers change. 请注意,它会向后循环触发器,因为如果删除了一个触发器,则任何后续触发器的ID都会更改。

# get task definition and change it:
$definition = $task.Definition

$numTriggers = $definition.Triggers.Count

# loop backwards through the triggers and 
# remove any that are "Startup" (Type = 8)
for($triggerId=$numTriggers; $triggerId -gt 0; $triggerId--){
    if($definition.Triggers.Item($triggerId).Type -eq "8"){
        $definition.Triggers.Remove($triggerId)
    }
}

I'm sure this could be done with fewer lines by piping to a where clause, but it always takes me longer to get that right so I just loop through things as long as it's something that will have minimal impact. 我确信通过管道到where子句可以用更少的线来完成,但是总是需要更长的时间才能做到这一点,所以我只需循环一下,只要它能够产生最小的影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM