我正在使用 Windows 任务计划程序运行一个可执行文件,该可执行文件在成功运行时返回 0。但是,如果 (1) 任务运行失败或 (2) 返回代码不是 0,我希望收到电子邮件通知。
这是 Windows Server 2003 上的 Windows 任务计划程序可以做的事情吗???
答案1
在我根除 cmd.exe 的过程中 [笑],这里有一个 Powershell 脚本,它也应该适合您:
# attempt to run your exe. iex is an alias for the invoke-expression cmd
iex c:\path_to_exe\myprog.exe
# $? lets us know if the previous command was successful or not
# $LASTEXITCODE gives us the exit code of the last Win32 exe execution
if (!$? -OR $LASTEXITCODE -gt 0)
{
$smtpServer = "smtp.mydomain.com"
$fromAddress = "[email protected]"
$toAddress = "[email protected]"
$subject = "FAIL"
$msgBody = "HEY, YOU GOT PROBLEMS"
# This block is optional depending on your SMTP server config
# You need it if your SMTP server requires authentication
$senderCreds = new-object System.Net.networkCredential
$senderCreds.UserName = "senderusername"
$senderCreds.Password = "senderpwd"
$smtpClient = new-object Net.Mail.SmtpClient($smtpServer)
$smtpClient.Credentials = $senderCreds
$smtpClient.Send($fromAddress,$toAddress,$subject,$msgBody)
}
答案2
一个非常简单的技巧是将命令包装在 CMD / 批处理脚本中,并使用返回的错误级别和布拉特有条件地发送电子邮件。