如何从计划任务中删除触发器 [英] How can I remove a trigger from a scheduled task

查看:81
本文介绍了如何从计划任务中删除触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 PowerShell 中使用 Task Scheduler cmdlet 为 Perfmon 数据收集器集的计划任务配置触发器.我正在使用以下内容修改计划任务并在服务器启动时启动数据收集器集:

I'm using the Task Scheduler cmdlet in PowerShell to configure triggers for a scheduled task of a Perfmon Data Collector set. 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.

PowerShell cmdlet 似乎没有办法做到这一点.任何帮助将不胜感激.

Doesn't look like there is a way of doing that from the PowerShell cmdlets. Any help will be appreciated.

推荐答案

似乎没有用于删除触发器的 PowerShell cmdlet,但您可以通过 Schedule.Service COM 对象来完成.以下示例将从名为 MyTask 的任务中删除第一个触发器.

There doesn't appear to be a PowerShell cmdlet to delete a trigger, but you can do it through the Schedule.Service COM object. The following example will remove the first trigger from the task named 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.

注意:触发器枚举是从一开始的,而不是从零开始的,所以删除第一个触发器是$definition.Triggers.Remove(1)

Note: The trigger enumerations are one-based, not zero-based, so to remove the first trigger it is $definition.Triggers.Remove(1)

归功于 使用 PowerShell 更改计划任务,其中大部分内容.

Credit to Changing Scheduled Tasks with PowerShell for much of this.

$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)

要专门删除所有属于AtStartup"触发器的触发器,请将 #get task definition and change it: 部分替换为以下内容.请注意,它向后循环遍历触发器,因为如果删除一个触发器,任何后续触发器的 ID 都会更改.

To specifically delete all triggers that are "AtStartup" triggers, replace the # get task definition and change it: section with the following. Note that it is looping through the triggers backwards, because if one is removed, the IDs of any following triggers change.

# 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)
    }
}

我确信这可以通过管道到 where 子句来用更少的行来完成,但我总是需要更长的时间才能做到这一点,所以我只是循环处理,只要它是将影响最小.

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.

这篇关于如何从计划任务中删除触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆