简体   繁体   中英

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.

This task has an At system startup trigger. 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 .

However, I noticed that TaskName would probably be the primary option with this command that I could use. 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.

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. Based on this list. You likely want MSFT_TaskLogonTrigger or MSFT_TaskBootTrigger so we'll use those.

I'm going to use an if/elseif chain here to give you some options.

$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

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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