How to configure "If the task is already running, then the following rule applies" in Windows Task Scheduler using PowerShell script?

6.6k views Asked by At

Task Scheduler settings page

I am trying to achieve the following settings (select "If the task is already running, then the following rule applies") through PowerShell script but unable to get appropriate settings to configure that.

I am using the following code to configure that

$Trigger = New-ScheduledTaskTrigger -At 07:00am -Daily

$Settings = New-ScheduledTaskSettingsSet -ExecutionTimeLimit (New-TimeSpan -Hour 1) -Compatibility Win7 -StartWhenAvailable -Priority 7

$User = "SYSTEM"

$Action = New-ScheduledTaskAction -Execute "some script" -Argument "some argument" -WorkingDirectory "working dir"

Register-ScheduledTask -TaskName "Test Task" -Trigger $Trigger -User $User -Action $Action -Settings $Settings -RunLevel Highest –Force

To do the advanced configuration for the triggers

$Task = Get-ScheduledTask -TaskName "Example Task"

$Task.Triggers[0].ExecutionTimeLimit = "PT10M"

$Task | Set-ScheduledTask -User $User
2

There are 2 answers

5
Ansgar Wiechers On

The setting is configured via New-ScheduledTaskSettingsSet and the parameter you're looking for is -MultipleInstances:

-MultipleInstances

Specifies the policy that defines how Task Scheduler handles multiple instances of the task. The acceptable values for this parameter are:

IgnoreNew. The new task instance is ignored. Parallel. The new task instance starts immediately. Queue. The new task instance starts as soon as the current instance completes.

Type: MultipleInstancesEnum
Accepted values: Parallel, Queue, IgnoreNew
Position: Named
Default value: None

However, the documentation lists only 3 values, and the respective enum (at least at the time of this writing also only has the listed 3 values:

  • ParallelRun a new instance in parallel
  • QueueQueue a new instance
  • IgnoreNewDo not start a new instance

If you create a task manually via the GUI and select the setting "Stop the existing instance" the value .Settings.MultipleInstances is empty, but if you create a Settings object via New-ScheduledTaskSettingsSet omitting the parameter -MultipleInstances it defaults to IgnoreNew. Attempts to change that to an empty value result in validation errors.

This is obviously a bug (missing value in the referenced enum).

0
pkucas On

The enum now contains 'StopExisting'.

This is my solution in C#.

static void RegisterMyTask(string taskPath, string remoteServer)
{
    try
    {
        using TaskService ts = new(remoteServer);
        TaskDefinition taskDef = ts.NewTask();
        taskDef.Settings.MultipleInstances = TaskInstancesPolicy.StopExisting;

        ...

However PowerShell probably has the new enum as well. As I would guess:

-MultipleInstances StopExisting

I had a task scheduled on multiple remote servers.

On day 1 it started running.

Day 2 it failed to start a new instance, but didn't kill the existing instance

Day 3 it failed to start a new instance, but didn't kill the existing instance. Then 30 seconds later, the existing instance was killed by the day 1 timeout setting, but the new instance had already failed for the day.

This fixed my problem.