如何让任务调度程序从 powershell 脚本中检测失败的错误代码 [英] How get task scheduler to detect failed error code from powershell script

查看:160
本文介绍了如何让任务调度程序从 powershell 脚本中检测失败的错误代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些 powershell 脚本,当它们内部出现故障时,我试图将它们作为 Windows 任务调度程序中的失败状态触发.所以我在powershell脚本中做这样的事情.我尝试了 1 或 99 的退出代码,但看起来 Windows 任务调度程序并未将其视为失败状态.所以我的失败代码电子邮件不会发送给我.

I have a few powershell scripts that I'm trying to get to trigger as a failed state in the windows task scheduler when they have failures inside them. So I'm doing something like this inside the powershell script. I tried an exit code of 1 or 99, and it doesn't look like windows task scheduler is seeing that as a failure state. So my failure code email doesn't get sent out to notify me.

如何让任务调度程序看到我的 powershell 脚本失败?它总是有 129(创建任务流程)、100(任务开始)、200(动作开始)、110(任务触发)、201(动作完成)、102(任务完成)的事件代码.

How do I get task scheduler to see that my powershell script had a failure? It always has event codes of 129 (created task process), 100 (task started), 200 (action started), 110 (task triggered), 201 (action completed), 102 (task completed).

$global:ErrorStrings = New-Object System.Collections.Generic.List[System.Object] #I add strings onto the list as I find errors

$errorCodeAsString = ""
foreach ($item in $global:ErrorStrings.Members){
   $errorCodeAsString += (" " + $item + "..")
}
if($errorCodeAsString -ne "")
{
   write-output  "Error: $errorCodeAsString"
   Exit 99 #Exit 1 didn't cause task scheduler to see error at exit either
}
Exit 0

我知道我的列表中充满了错误,因为我创建了它们来测试它.我检查了作为字符串的 errorCode 是一个长度,然后点击退出 99 或 1.任务调度程序仍然显示正常的事件代码.

I know my list is populated with errors because I created them to test it. I checked that the errorCode as string was a length and hit the exit 99 or 1. The task scheduler is still showing the normal event codes.

我有一个关于计划失败的电子邮件警报,由于事件代码没有显示失败,它永远不会触发发送我的电子邮件.这是 Windows 10,以防万一.

I have an email alert on failure scheduled and since the event codes aren't showing failures, it will never trigger to send my email. This is windows 10, in case it matters.

我一直在查看 powershell 错误 sql, 任务调度程序成功错误技巧技巧计划任务powershell退出代码,但它没有帮助.

I've been looking at powershell errors sql, task scheduler success error, tips tricks scheduled tasks, powershell exit code, but it's not helping.

powershell 脚本在任务调度器中设置如下:

The powershell scripts are set up in task scheduler like this:

动作:启动程序

程序/脚本:PowerShell

添加参数: -ExecutionPolicy Bypass -File C:\Users\me\Documents\powershell\disasterBackup.ps1

Add arguments: -ExecutionPolicy Bypass -File C:\Users\me\Documents\powershell\disasterBackup.ps1

推荐答案

第 1 部分是让 PowerShell 向任务计划程序返回正确的最后退出代码.

Part 1 is to have PowerShell return the correct Last Exit Code to Task Scheduler.

这是 Task Scheduler 的特性之一.它只是报告说,是的,PowerShell.exe 运行成功.问题在于 PowerShell.exe 不会返回退出代码,因为是的,PowerShell.exe 运行正确,即使脚本没有运行.

This is one of the peculiarities of Task Scheduler. It simply is reporting that, yes, PowerShell.exe ran successfully. The problem is that PowerShell.exe doesn't report back the exit code, because, yes, PowerShell.exe ran correctly, even if the script did not.

我能够解决这个问题的方法是从运行带有 -File 参数的脚本切换到 -Command 参数.这样我就可以通过使用 $LASTEXITCODE 值显式退出,以正确的退出代码退出 PowerShell.exe:

The way I have been able to get around this is to switch from running the script with the -File parameter, which doesn't return the exit value, to a -Command parameter. That way I can exit PowerShell.exe with the correct exit code by explicitly exiting with the$LASTEXITCODE value:

#Run Scheduled task with the following command

powershell.exe -Command ". C:\Scripts\RunScript.ps1; exit $LASTEXITCODE"

所以你的情况是:

powershell.exe -ExecutionPolicy Bypass -Command ". C:\Users\me\Documents\powershell\disasterBackup.ps1; exit $LASTEXITCODE"

--- 编辑----

第 2 部分是在无法发送电子邮件或其他内容时在事件上触发计划任务.

Part 2 is to have a Scheduled Task Triggering on an Event when it fails to send an email or something.

任务计划程序的问题与我们退出 PowerShell 时遇到的问题相同.无论返回什么退出代码,任务总是记录事件 ID 201 - 操作已完成...这是正确的...无论如何,即使在内部运行的作业失败,任务也已完成.

The trouble with Task Scheduler is the same thing we had with PowerShell exiting. No matter what exit code is returned, the task always logs an Event ID 201 - Action Completed... which is correct... no matter what, the task completed even if the job that was run failed internally.

进一步查看记录事件的详细信息,我们可以看到 EventData 中的 ResultCode 确实设置正确.因此,通过 GUI 过滤它是一项简单的工作,对吗?.... 不...除了 EventID 之外没有过滤器.现在我们必须编写一个自定义事件过滤器以根据 ResultCode 触发.我们需要的 XML XPath 查询是这样的:

Looking further into the Details of the logged Event, we can see the ResultCode in the EventData does get set correctly. So it's a simple job to filter that through the GUI right?.... well no... There is no filter beyond EventID. Now we have to write a custom Event filter to trigger on based on the ResultCode. The XML XPath query that we need is this:

<QueryList>
  <Query Id="0" Path="Microsoft-Windows-TaskScheduler/Operational">
    <Select Path="Microsoft-Windows-TaskScheduler/Operational">
      *[System[(Level=4 or Level=0) and (EventID=201)]]
        and
      *[EventData[Data[@Name='ResultCode'] and (Data='2147942401')]]</Select>
  </Query>
</QueryList>

所以为了分解它,我们想要:

So to break it down, we want:

Event log: Microsoft-Windows-TaskScheduler/Operational
Event Level: 4 or 0 = Information
Event ID: 201
And
Event Data: ResultCode = 2147942401

如果我们将错误退出代码设置为 1,为什么 ResultCode = 2147942401?因为它实际上返回了 0x1,它是十六进制的 0x80070001,它等于十进制的 2147942401.因此,您必须查看事件的详细信息才能找到正确"的 ResultCode.

If we set the bad exit code to 1, why is ResultCode = 2147942401? because it actually returns 0x1 which is hexadecimal 0x80070001 which equals decimal 2147942401. So you will have to look at the Details of the Event to find the "Correct" ResultCode.

这篇关于如何让任务调度程序从 powershell 脚本中检测失败的错误代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆