Windows任务调度报告不正确/不一致的结果code [英] Windows Task Scheduler reports incorrect/inconsistent result code

查看:846
本文介绍了Windows任务调度报告不正确/不一致的结果code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我试图在计划任务中未能获得电子邮件通知。我的任务可以通过显示出code(错误级别)不,我想使用,并按照这个答案说明的过滤方法触发的电子邮件。

问题:我在不同的机器和Windows的版本正从任务计划不一致的行为。为了测试,我用下面的非常简单的任务。


  • 当用户登录时只运行。 (其中user是当前用户)

  • 操作:启动一个程序

    • 程序/脚本: CMD

    • 参数: / C退出/ B 1

    • 开始在:空白


  • 其他的一切:默认

我已经启用任务历史记录,当我手动两种Windows 7旗舰版和Windows Server上运行任务2008 R2企业版,就像在类中创建下面的行动已完成正确的历史项:


  

任务计划程序成功完成任务\\测试,例如{ABCDEFGH-fab0-4a21-b51a-e25baaaa0000},动作C:\\ WINDOWS \\ SYSTEM32 \\ CMD.EXE,返回code 1


上次运行结果的文本的列对应于这一点:(为0x1)

然而,当在Windows Server 2012(天青VM)上运行,将出现以下错误消息:


  

任务计划程序成功完成任务\\测试,例如{ABCDEFGH-fab0-4a21-b51a-e25bbbbb1111},动作C:\\ WINDOWS \\ SYSTEM32 \\ cmd.exe的,返回code 0


(重点煤矿)。不过在最后运行结果文本的栏写着:函数不正确:(0x80070001)。这表明任务调度处理器的至少一部分识别某种程度的错误情况。如果我换成 / C退出/ B 0的参数,此列写着:的操作成功完成。 (为0x0)

我试过值的不同组合对用户的背景下,开始目录,参数(有和没有 / B )和配置在不同的操作系统为:无果

问:如何获得Windows 2012机器上的任务计划程序,以显示在历史项正确的返回code

解决方法:当我还是想明白了这个古怪的动作(或许它表明一些显著的问题/误会),我加入以下到我的任务脚本解决我原来的问题:

  $ objMailMessage =新对象System.Net.Mail.MailMessage
$ objMailMessage.From =username@gmail.com
$ objMailMessage.To.Add(recipient@example.com)
$ objMailMessage.Subject =任务失败。
#$ objMailMessage.Body =的详细信息。$ SMTP =新的对象Net.Mail.SmtpClient(smtp.gmail.com,587)
$ smtp.EnableSsl = $真
$ smtp.Credentials =新的对象Net.NetworkCredential(username@gmail.com,通)
$ smtp.send($ objMailMessage)


解决方案

我看到服务器2012年的批处理文件看起来像它成功了,在一个类似的问题给出了0事件日志中返回值,但是最后0x80070001的运行结果。

我看到MSFT具有可用于Server 2012的一个修补程序可能解决此问题:

http://support.microsoft.com/kb/3003689

Background: I am attempting to get email notification upon failure of a scheduled task. My task can indicate failure via exit code (errorlevel) and I want to use that and follow the filter approach described in this answer to trigger an email.

Problem: I am getting inconsistent behavior from Task Scheduler on different machines and versions of Windows. To test, I'm using the following very simple task.

  • Run only when user is logged on. (where "user" is the current user)
  • Action: Start a program
    • Program/script: cmd
    • Arguments: /c "exit /b 1"
    • Start in: blank
  • Everything else: default

I've enabled Task History, and when I manually run the task on both Windows 7 Ultimate and Windows Server 2008 R2 Enterprise, a correct history item like the following is created in category "Action completed":

Task Scheduler successfully completed task "\test" , instance "{abcdefgh-fab0-4a21-b51a-e25baaaa0000}" , action "C:\Windows\system32\cmd.EXE" with return code 1.

The text in the Last Run Result column corresponds to this: (0x1).

However, when run on Windows Server 2012 (Azure VM), the following erroneous message appears:

Task Scheduler successfully completed task "\test" , instance "{abcdefgh-fab0-4a21-b51a-e25bbbbb1111}" , action "C:\Windows\system32\cmd.EXE" with return code 0.

(Emphasis mine.) However the text in the Last Run Result column reads: Incorrect function: (0x80070001). This indicates that at least part of the Task Scheduling processor recognizes some degree of error condition. If I replace the arguments with /c "exit /b 0", this column reads: The operation completed successfully. (0x0)

I've tried various combinations of values for user context, "start in" directory, arguments (with and without /b), and different operating systems under "Configure for:", to no avail.

Question: How can I get Task Scheduler on the Windows 2012 machine to display the correct return code in the history item?

Workaround: While I still want to understand this bizarre behavior (perhaps it indicates some more significant problem/misunderstanding), I am solving my original issue by adding the following to my task script:

$objMailMessage = New-Object System.Net.Mail.MailMessage
$objMailMessage.From = "username@gmail.com"
$objMailMessage.To.Add("recipient@example.com")
$objMailMessage.Subject = "The task failed."
#$objMailMessage.Body = "Detailed information."

$smtp = new-object Net.Mail.SmtpClient("smtp.gmail.com", 587)
$smtp.EnableSsl = $true
$smtp.Credentials = new-object Net.NetworkCredential("username@gmail.com","pass")
$smtp.send($objMailMessage)

解决方案

I'm seeing a similar issue on Server 2012 with a batch file that looks like it succeeds, shows a return value of 0 in event log, but a Last Run Result of 0x80070001.

I see MSFT has a hotfix available for Server 2012 which might address this issue:

http://support.microsoft.com/kb/3003689

这篇关于Windows任务调度报告不正确/不一致的结果code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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