Windows 任务计划程序不显示执行错误

Windows 任务计划程序不显示执行错误

我正在尝试制作一个可以通过 Windows 任务计划程序不时运行的 powerscript。

我的问题是,即使我的脚本确实失败了,任务调度程序也不会显示任务失败。

这是我得到的事件:

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."

Result.xml 展示了另一个故事:

    <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>

我已经通过调用脚本中的以下内容创建了任务本身:

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
}

知道为什么我没有收到漂亮的红色错误吗?

答案1

即使 PowerShell 脚本失败,PowerShell.exe 通常也不会返回错误代码。只有无法加载 PowerShell 脚本时,它才会返回非零错误代码。

我通常使用以下命令行来让我的进程在脚本失败时返回非零错误代码:

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

如果脚本中的某些内容失败,整个过程将返回非零错误级别,然后任务计划程序就可以检测到。

相关内容