2

Server 2012

I have an existing scheduled task that I would like to add multiple triggers to.

I do this:

$trigger = New-ScheduledTaskTrigger -At 1PM -Once
Set-ScheduledTask -TaskName "Task Name" -Trigger $trigger

It will overwrite the trigger that is already in there.

However, I want to create multiple triggers.

I try this:

$trigger = New-JobTrigger -Once -At 1PM 
Add-JobTrigger -Trigger $trigger -Name "Task Name"

and this:

$trigger = New-ScheduledTaskTrigger -Once -At 1PM
Add-JobTrigger -Trigger $trigger -Name "Task Name"

In both cases, I get the error "A scheduled job definition with Name Task Name could not be found" (There is a task there called "Task Name")

What do I need to do to simply add multiple triggers to a single task?

1
  • To be clear, do you want the job to start after either trigger fires or after both triggers fire? – uSlackr Aug 17 '17 at 21:08
6

The -Trigger parameter can take an array of ScheduledJobTrigger objects so for your example you can do.

$trigger = @(
    $(New-ScheduledTaskTrigger -At 1PM -Once),
    $(New-ScheduledTaskTrigger -At 2PM -Once),
    $(New-ScheduledTaskTrigger -At 3PM -Once)
)
Set-ScheduledTask -TaskName "Task Name" -Trigger $trigger

The same is valid with Add-JobTrigger too.

$trigger = @(
    $(New-ScheduledTaskTrigger -At 1PM -Once),
    $(New-ScheduledTaskTrigger -At 2PM -Once),
    $(New-ScheduledTaskTrigger -At 3PM -Once)
)
Add-JobTrigger -Trigger $trigger -Name "Task Name"
3
  • is this possible to do via schtasks command? I tried schtasks /change /tn mytaskname /st 03:00 /sd 2020/10/20 /sc once and command succeeded but it didn't create any new trigger for the task – Vitas Oct 19 '20 at 20:42
  • 1
    Yes and no. It is not possible to do using parameters like you are trying here but you can with a scheduled task XML file and pass that in with /xml. devblogs.microsoft.com/scripting/… – Persistent13 Nov 2 '20 at 2:46
  • meanwhile I figured out a powershell way with New-ScheduledTaskTrigger, New-CimSession and Set-ScheduledTask, but I am afraid it might have a problem with some older powershell versions... – Vitas Nov 2 '20 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.