Windows Task Scheduler reports incorrect/inconsistent result code

拟墨画扇 提交于 2019-12-13 15:43:04

问题


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)

回答1:


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



来源:https://stackoverflow.com/questions/19505759/windows-task-scheduler-reports-incorrect-inconsistent-result-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!