0

I have a powershell script, that write http response body to output.

When I run it from command line like this, it writes the response to Emails.log file

powershell "./InvokeMyApi.ps1 /api/emails/SendEmails" > Emails.log

However, when I invoke it from Task Scheduler, it only writes empty file.

To rule out missing permission, I granted write access to the folder to everyone

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.3" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2022-01-10T17:18:47.7178478</Date>
    <Author>MyCompany\Liero</Author>
    <URI>\MyApp\SendEmails/URI>
  </RegistrationInfo>
  <Triggers>
    <CalendarTrigger>
      <Repetition>
        <Interval>PT5M</Interval>
        <Duration>P1D</Duration>
        <StopAtDurationEnd>false</StopAtDurationEnd>
      </Repetition>
      <StartBoundary>2023-04-17T05:00:34</StartBoundary>
      <Enabled>true</Enabled>
      <ScheduleByDay>
        <DaysInterval>1</DaysInterval>
      </ScheduleByDay>
    </CalendarTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>S-1-5-21-1085031214-1292428093-1177238915-3242</UserId>
      <LogonType>Password</LogonType>
      <RunLevel>LeastPrivilege</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>true</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
    <UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
    <WakeToRun>true</WakeToRun>
    <ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
    <Priority>7</Priority>
    <RestartOnFailure>
      <Interval>PT1M</Interval>
      <Count>3</Count>
    </RestartOnFailure>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>powershell</Command>
      <Arguments>"./InvokeIsysApi.ps1 /api/emails/SendEmails" &gt; Emails.log</Arguments>
      <WorkingDirectory>C:\Tools</WorkingDirectory>
    </Exec>
  </Actions>
</Task>

How to get output from my powershell script to a file?

Liero
  • 101
  • 1

1 Answers1

0

Are you sure the script itself is definitely running successfully via Task Scheduler, so there's output actually being generated to be piped to the text file?

Could be the old issue of the context that it's being run in via task schedule not having execution policy set to allow it to run (which I don't think shows in Task Scheduler as a fail, as Powershell was successfully called, but the failure happens afterwards). Try setting the task to run it like this instead

powershell -executionpolicy bypass -command "./InvokeMyApi.ps1 /api/emails/SendEmails" > Emails.log

which will then ensure the execution policy has been set regardless of the profile it's running under.

Personally I'd also specify the full folder paths to the files as well, rather than just relying on Task Scheduler running in the correct folder at the time, but since it is at least generating an empty file I'd guess that's not your issue.

Keith Langmead
  • 857
  • 1
  • 7
  • 14
  • It does run, I see it in task's history tab: `Task Scheduler successfully completed task "blabla" , instance "nlabla" , action "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.EXE" with return code 0.` – Liero May 16 '23 at 07:36
  • Another thing you could check is within the scheduled task, in the General tab, what is "Configure for:" at the bottom set to. I've been caught out in the past where the server is newer, but for whatever reason Task Scheduler is defaulting to a much older "Configure for" version (like 2008!), which then doesn't allow some of your functionality. In that case, updating it to use the current version may resolve it. – Keith Langmead May 16 '23 at 11:10