Task Scheduler.

Soldato
Joined
16 Sep 2018
Posts
12,819
After having spent the best part of a week trying to troubleshoot this I'm no further forward so I'm turning the the hive mind in the hope that someone has more experience with task scheduler, specifically on event triggers and delays.

So i have a task (xml) that gets imported via powershell after a fresh install when the desktop first loads that triggers on an event with a delay, for example when an EventID 10000 is logged in the NetworkProfile/Operational event log, and it works fine if it's run manually and even if when i create such an event in the log myself, 30 seconds after i create the event it fires and runs the script i want it to.

However, and this is what's had me pulling my hair out for the last week or so, when the system logs such an event the 30 second delay sometimes triggers the task at the right time, so 30sec after the event was logged, and other times it doesn't trigger the task until well after the delay period. So the triggering event will be logged at 10:21 and the task will sometimes run at 10:21 + 30 seconds and other times it runs at 10:21 +5min or +10min.

Does anyone know if this is just how Task Scheduler's delays work or is it something I'm getting wrong? FWIW this is the xml file that I'm importing...
XML:
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Author>Administrator</Author>
    <Description>Register the default Powershell repository when an internet connection is detected</Description>
    <URI>\Register Default Powershell Repository</URI>
  </RegistrationInfo>
  <Triggers>
    <EventTrigger>
      <StartBoundary>2018-10-04T08:42:05</StartBoundary>
      <Enabled>true</Enabled>
      <Subscription>&lt;QueryList&gt;&lt;Query Id="0" Path="Microsoft-Windows-NetworkProfile/Operational"&gt;&lt;Select Path="Microsoft-Windows-NetworkProfile/Operational"&gt;*[System[Provider[@Name='Microsoft-Windows-NetworkProfile'] and EventID=10000]]&lt;/Select&gt;&lt;/Query&gt;&lt;/QueryList&gt;</Subscription>
      <Delay>PT30S</Delay>
    </EventTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>S-1-5-21-3511074586-850209778-1449882620-500</UserId>
      <LogonType>InteractiveToken</LogonType>
      <RunLevel>HighestAvailable</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>true</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>true</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>false</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
    <UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
    <WakeToRun>false</WakeToRun>
    <Priority>0</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>powershell.exe</Command>
      <Arguments>C:\Windows\Setup\Scripts\PSRepository.ps1</Arguments>
    </Exec>
  </Actions>
</Task>
And this is how they're being imported...
Code:
$Tasks = Get-ChildItem -Path "$PathToScript\..\Tasks"
foreach ($Task in $Tasks) {
    $XmlString = Get-Content $Task.FullName -Raw
    $LocalUser = Get-LocalUser -Name $env:USERNAME
    Register-ScheduledTask -Xml $XmlString -TaskName $Task.BaseName -User $LocalUser
}
One thing to note, and I'm not sure if it's relevant, task history shows the initial task registration however if i bring up the properties of the task and do nothing but click OK it shows the registration info for the task has been updated despite not having changed anything. Is it possible powershell is not importing the task properly?
 
Back
Top Bottom