How can I not allow Task Scheduler to execute a task out of its schedule?

BARROSO TRISTAN, JAVIER RICARDO 20 Reputation points
2024-04-30T11:41:06.2266667+00:00

How can I not allow Task Scheduler to execute a task out of its schedule?

I have a task which is scheduled daily at the night, and some times it is executed in the morning. The task is a powershell script (which I could prepend a short assert to exit if it is running out of its working hours, but I would like to understand Windows Task Scheduler)

Why Windows is running the task at 09:01 ?

Thank you!

PS C:\Windows\system32> Get-ScheduledTask "INTUNE41 - Apagar Equipo - 2024-02-12 093039" | select -ExpandProperty triggers
Enabled            : True
EndBoundary        :
ExecutionTimeLimit :
Id                 :
Repetition         : MSFT_TaskRepetitionPattern
StartBoundary      : 2024-01-02T22:30:00+02:00
DaysInterval       : 1
RandomDelay        :
PSComputerName     :
Enabled            : True
EndBoundary        :
ExecutionTimeLimit :
Id                 :
Repetition         : MSFT_TaskRepetitionPattern
StartBoundary      : 2024-01-02T07:30:00+02:00
DaysInterval       : 1
RandomDelay        :
PSComputerName     :
PS C:\Windows\system32> Get-ScheduledTask "INTUNE41 - Apagar Equipo - 2024-02-12 093039" | Get-ScheduledTaskInfo
LastRunTime        : 30/04/2024 9:01:01
LastTaskResult     : 0
NextRunTime        : 30/04/2024 22:30:30
NumberOfMissedRuns : 0
TaskName           : INTUNE41 - Apagar Equipo - 2024-02-12 093039

The task schedule was created with the next xml definition:

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Author>.\Administrador</Author>
    <URI>\apagar</URI>
  </RegistrationInfo>
  <Triggers>
    <CalendarTrigger>
      <StartBoundary>2024-01-02T22:30:00+02:00</StartBoundary>
      <Enabled>true</Enabled>
      <ScheduleByDay>
        <DaysInterval>1</DaysInterval>
      </ScheduleByDay>
    </CalendarTrigger>
    <CalendarTrigger>
      <StartBoundary>2024-01-02T07:30:00+02:00</StartBoundary>
      <Enabled>true</Enabled>
      <ScheduleByDay>
        <DaysInterval>1</DaysInterval>
      </ScheduleByDay>
    </CalendarTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>S-1-5-18</UserId>
      <RunLevel>HighestAvailable</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>false</AllowHardTerminate>
    <StartWhenAvailable>false</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <Duration>PT10M</Duration>
      <WaitTimeout>PT1H</WaitTimeout>
      <StopOnIdleEnd>false</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>true</RunOnlyIfIdle>
    <DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
    <UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>powershell</Command>
      <Arguments>-executionpolicy bypass -file "c:\intune\Apagar Equipo\Aplicacion\Apagar Equipo.ps1"</Arguments>
    </Exec>
  </Actions>
</Task>
Windows 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
10,753 questions
Windows 10 Security
Windows 10 Security
Windows 10: A Microsoft operating system that runs on personal computers and tablets.Security: The precautions taken to guard against crime, attack, sabotage, espionage, or another threat.
2,776 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 32,086 Reputation points
    2024-04-30T14:18:47.91+00:00

    You have idle conditions set that might impact the start time. If the trigger is for 7:30 AM, then the idle window should indicate that 8:30 AM is the last start time. But you also have "Synchronize across time zones" checked for your triggers, so depending on your location that could also impact the start time.

    Enable task history if you have not done so already. Check the history tab for trigger and other events that would explain why the task is being started.

    User's image

    If it helps, I wrote a Powershell script that I called TaskWatcher.ps1 to monitor a scheduled task.

    https://learn.microsoft.com/en-us/archive/msdn-technet-forums/c0be6139-5198-46b2-9135-4e6dae49adaa

    Update the Task2Watch variable in the script to reference your task name. Logon to the PC and see what changes in task status it detects.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. BARROSO TRISTAN, JAVIER RICARDO 20 Reputation points
    2024-04-30T15:10:46+00:00

    Thank you @MotoX80 , ok so I think you are giving me a good hint. Today my computer booted eaelier than other days, maybe Windows is executing the task because of the second trigger, but It waited until I had not activity. So, I suppose (i have not the computer here): 07:25 boot the computer; 07:30 task scheduler add to the queue the task, 08:50 I left the computer by more than 10 minutes, 09:01 Windows task manager launch the task. I will review idle options. Thank and sorry for the text format of this comment (new carriage character not works here (android browser))

    0 comments No comments

  2. BARROSO TRISTAN, JAVIER RICARDO 20 Reputation points
    2024-05-02T07:11:21.64+00:00

    After reading https://learn.microsoft.com/en-us/windows/win32/taskschd/task-idle-conditions, I think I have to change

    <StartBoundary>2024-01-02T22:30:00+02:00</StartBoundary>
    ...
    <RunOnlyIfIdle>true</RunOnlyIfIdle>
    

    To:

    <StartBoundary>2024-01-02T22:30:00</StartBoundary>
    ...
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    
    0 comments No comments