Score:0

Scheduled task with powershell won't restart when the script fails

ba flag

Scenario

I want to invoke a rest api on daily basis with some retry mechanism in case the rest api is not available

Problem

Even when the action exits with a non zero exit code, it is not restarted.

Task Scheduler successfully completed task "\MyTasks\test" , instance "{334ca542-9459-49db-9d0a-17587c9fe171}" , action "powershell.exe" with return code 4294967245.

<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
   <System>
      <Provider Name="Microsoft-Windows-TaskScheduler" Guid="{de7b24ea-73c8-4a09-985d-5bdadcfa9017}" />
      <EventID>201</EventID>
      <Version>2</Version>
      <Level>4</Level>
      <Task>201</Task>
      <Opcode>2</Opcode>
      <Keywords>0x8000000000000000</Keywords>
      <TimeCreated SystemTime="2023-03-28T06:29:05.7395075Z" />
      <EventRecordID>191</EventRecordID>
      <Correlation ActivityID="{334ca542-9459-49db-9d0a-17587c9fe171}" />
      <Execution ProcessID="2004" ThreadID="36724" />
      <Channel>Microsoft-Windows-TaskScheduler/Operational</Channel>
      <Computer>DESKTOP-KVSEB08</Computer>
      <Security UserID="S-1-5-18" />
   </System>
   <EventData Name="ActionSuccess">
      <Data Name="TaskName">\MyTasks\test</Data>
      <Data Name="TaskInstanceId">{334ca542-9459-49db-9d0a-17587c9fe171}</Data>
      <Data Name="ActionName">powershell.exe</Data>
      <Data Name="ResultCode">4294967245</Data>
      <Data Name="EnginePID">32620</Data>
   </EventData>
</Event>

what I have

I have following script.ps

param ($uri)
try
{ 
    $response = Invoke-RestMethod -Method 'Post' -Uri $uri
    Write-Host $response
}
catch
{
  Write-Host $_
  exit -1;
}

And scheduled task with following action:

powershell.exe -Command ".\script.ps1 -uri https://fooobooo/bar > aaa.log; $LASTEXITCODE"

action screenshot

with retstart option: retry screenshot

When I run the task (either on demand or on schedule),

Question

Why the task is not restarted and how to fix it?


I'm attaching the exported task as well:

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2023-03-27T16:57:54.4226428</Date>
    <Author>DESKTOP-KVSEB08\danie</Author>
    <URI>\MyTask\test</URI>
  </RegistrationInfo>
  <Triggers>
    <TimeTrigger>
      <StartBoundary>2023-03-28T08:29:03</StartBoundary>
      <Enabled>true</Enabled>
    </TimeTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>S-1-5-21-2614557988-2043820688-1863228669-1001</UserId>
      <LogonType>InteractiveToken</LogonType>
      <RunLevel>LeastPrivilege</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
    <Priority>7</Priority>
    <RestartOnFailure>
      <Interval>PT1M</Interval>
      <Count>3</Count>
    </RestartOnFailure>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>powershell.exe</Command>
      <Arguments>-Command "./script.ps1 https:/localhost:5003/api/ExchangeRates/Update; exit $LASTEXITCODE"</Arguments>
      <WorkingDirectory>C:\tools</WorkingDirectory>
    </Exec>
  </Actions>
</Task>
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.