简体   繁体   English

使用触发器类型过滤 Powershell 删除任务计划程序中的任务?

[英]Delete a task in Task Scheduler using Powershell filtering on trigger type?

I have a particular task in Task Scheduler on some Windows 10 machines.我在一些Windows 10机器上的Task Scheduler中有一个特定的任务。

This task has an At system startup trigger.此任务有一个At system startup触发。 Although the task is the same across systems, the task name could possibly be different on some machines.尽管跨系统的任务相同,但任务名称在某些机器上可能不同。

I need to remove this task from each of the machines.我需要从每台机器上删除这个任务。 For this purpose, I was looking to run a Powershell script on each system with the command Unregister-ScheduledTask .为此,我希望使用命令Unregister-ScheduledTask在每个系统上运行Powershell脚本。

However, I noticed that TaskName would probably be the primary option with this command that I could use.但是,我注意到TaskName可能是我可以使用的此命令的主要选项。 I was hoping to filter based on the At system startup trigger, just to make sure that different name doesn't cause deletion being unsuccessful on some systems.我希望根据At system startup触发器进行过滤,只是为了确保不同的名称不会导致某些系统上的删除失败。

Is there any other trick to delete tasks based on trigger type?还有其他技巧可以根据触发器类型删除任务吗?

What I currently have, uses the task name:我目前拥有的,使用任务名称:

if ($(Get-ScheduledTask -TaskName "SendEmailAtStartup" -ErrorAction SilentlyContinue).TaskName -eq "SendEmailAtStartup") {
     Unregister-ScheduledTask -TaskName "SendEmailAtStartup" -Confirm:$False
 }

I think you're trying to see tasks that are triggered at startup.我认为您正在尝试查看启动时触发的任务。 Did you schedule something at startup?你在启动时安排了什么吗? There's are several values in the .Triggers() field that may work for you. .Triggers()字段中有几个值可能对您有用。 Based on this list.基于此列表。 You likely want MSFT_TaskLogonTrigger or MSFT_TaskBootTrigger so we'll use those.您可能需要MSFT_TaskLogonTriggerMSFT_TaskBootTrigger ,因此我们将使用它们。

I'm going to use an if/elseif chain here to give you some options.我将在这里使用 if/elseif 链来为您提供一些选择。

$schedtasks = Get-ScheduledTask | Select-Object -Property *

foreach ($task in $schedtasks) {

    if ($task.triggers -like 'MSFT_TaskLogonTrigger') {

        ## do this for logon trigger
        Write-Warning -Message ('I found {0}.taskname with the LOGON trigger!' -f $task)
        ## do some stuff
    }
    elseif ($task.triggers -like 'MSFT_TaskBootTrigger') {

        ## do this for boot trigger
        Write-Warning -Message ('I found {0}) with the BOOT trigger!' -f $task.taskname)
        ## do some cool stuff

    }
    else {

        ## nothing was found
        Write-Warning -Message 'This task didn't match any of our triggers.'
        ## but we will might want to log this task or tell someone

    }
}

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

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