Windows Task Scheduler don’t show execution errors

powershellscheduled-taskwindows

I'm trying to make a powerscript that will run from time to time through the windows task scheduler.

My problem is that the task scheduler do not display the task as failed, even though my script do fail.

This is the event that i get:

Level   Date and Time   Event ID    Task Category   Operational Code    Correlation Id
Information 15.03.2016 22:53:06 102 Task completed  (2) f03232d8-4196-4425-88a9-722028f9700a    "Task Scheduler successfully finished ""{F03232D8-4196-4425-88A9-722028F9700A}"" instance of the ""\Microsoft\Windows\PowerShell\ScheduledJobs\My Scheduled Task"" task for user ""VEGAR-M4800\vegar""."
Information 15.03.2016 22:53:06 201 Action completed    (2) f03232d8-4196-4425-88a9-722028f9700a    "Task Scheduler successfully completed task ""\Microsoft\Windows\PowerShell\ScheduledJobs\My Scheduled Task"" , instance ""{F03232D8-4196-4425-88A9-722028F9700A}"" , action ""StartPowerShellJob"" with return code 0."
Information 15.03.2016 22:53:03 200 Action started  (1) f03232d8-4196-4425-88a9-722028f9700a    "Task Scheduler launched action ""StartPowerShellJob"" in instance ""{F03232D8-4196-4425-88A9-722028F9700A}"" of task ""\Microsoft\Windows\PowerShell\ScheduledJobs\My Scheduled Task""."
Information 15.03.2016 22:53:03 100 Task Started    (1) f03232d8-4196-4425-88a9-722028f9700a    "Task Scheduler started ""{F03232D8-4196-4425-88A9-722028F9700A}"" instance of the ""\Microsoft\Windows\PowerShell\ScheduledJobs\My Scheduled Task"" task for user ""VEGAR-M4800\vegar""."
Information 15.03.2016 22:53:03 129 Created Task Process    Info    00000000-0000-0000-0000-000000000000    "Task Scheduler launch task ""\Microsoft\Windows\PowerShell\ScheduledJobs\My Scheduled Task"" , instance ""powershell.exe""  with process ID 12780."
Information 15.03.2016 22:53:03 107 Task triggered on scheduler Info    f03232d8-4196-4425-88a9-722028f9700a    "Task Scheduler launched ""{F03232D8-4196-4425-88A9-722028F9700A}""  instance of task ""\Microsoft\Windows\PowerShell\ScheduledJobs\My Scheduled Task"" due to a time trigger condition."

The Result.xml shows another story:

    <Results_Error z:Assembly="0" z:Id="61" z:Type="System.Collections.ObjectModel.Collection`1[[System.Management.Automation.ErrorRecord, System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]">
  <items xmlns="http://schemas.datacontract.org/2004/07/System.Management.Automation" z:Assembly="0" z:Id="62" z:Type="System.Collections.Generic.List`1[[System.Management.Automation.ErrorRecord, System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]">
    <_items z:Id="63" z:Size="4">
      <ErrorRecord z:Assembly="System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" z:Id="64" z:Type="System.Management.Automation.Runspaces.RemotingErrorRecord">
        <CliXml xmlns="" z:Assembly="0" z:Id="65" z:Type="System.String">&lt;Objs Version=&quot;1.1.0.1&quot; xmlns=&quot;http://schemas.microsoft.com/powershell/2004/04&quot;&gt;

&lt;Obj RefId=&quot;0&quot;&gt;

&lt;ToString&gt;@{Exception=System.Management.Automation.RemoteException: Could not find a part of the path 'C:\source\flis\'.; TargetObject=; FullyQualifiedErrorId=FileOpenFailure,Microsoft.PowerShell.Commands.OutFileCommand; InvocationInfo=; ErrorCategory_Category=1; ErrorCategory_Activity=Out-File; ErrorCategory_Reason=DirectoryNotFoundException; ErrorCategory_TargetName=; ErrorCategory_TargetType=; ErrorCategory_Message=OpenError: (:) [Out-File], DirectoryNotFoundException; SerializeExtendedInfo=False; ErrorDetails_ScriptStackTrace=at &amp;lt;ScriptBlock&amp;gt;, &amp;lt;No file&amp;gt;: line 1_x000D__x000A_at Do-Backup, &amp;lt;No file&amp;gt;: line 66_x000D__x000A_at &amp;lt;ScriptBlock&amp;gt;, &amp;lt;No file&amp;gt;: line 83}&lt;/ToString&gt;
....
</CliXml>
        <RemoteErrorRecord_OriginInfo xmlns="" z:Assembly="System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" z:Id="66" z:Type="System.Management.Automation.Remoting.OriginInfo">
          <_computerName xmlns="http://schemas.datacontract.org/2004/07/System.Management.Automation.Remoting" z:Id="67">localhost</_computerName>
          <_instanceId xmlns="http://schemas.datacontract.org/2004/07/System.Management.Automation.Remoting">00000000-0000-0000-0000-000000000000</_instanceId>
          <_runspaceID xmlns="http://schemas.datacontract.org/2004/07/System.Management.Automation.Remoting">975a8cbe-dbd1-43c0-80b4-19c282eee381</_runspaceID>
        </RemoteErrorRecord_OriginInfo>
      </ErrorRecord>
      <ErrorRecord i:nil="true"/>
      <ErrorRecord i:nil="true"/>
      <ErrorRecord i:nil="true"/>
    </_items>
    <_size>1</_size>
    <_version>1</_version>
  </items>
</Results_Error>

I have created the task it self by invoking the following inside a scipt:

function Create-Task
{
    Param($name, $description)

    $trigger = New-JobTrigger -Daily -At 1am
    $options = New-ScheduledJobOption -WakeToRun -RequireNetwork

    Register-ScheduledJob -Name $name -FilePath $MyInvocation.PSCommandPath -ArgumentList "Run", $MyInvocation.PSScriptRoot -Trigger $trigger -ScheduledJobOption $options
}

Any idea why I don't get a nice red error ?

Best Answer

PowerShell.exe does normally not return an error code even if the PowerShell script fails. Only if it's unable to load the PowerShell script will it return a non-zero error code.

I usually use the following command line to get my process to return a non-zero error code in case the script fails:

PowerShell.exe -Command "& {& 'C:\MyPowerShellScript.ps1' ; EXIT $LASTEXITCODE }"

If something inside the script fails the whole process will return a non-zero error level, and this can then be detected by the Task Scheduler.