我使用
Windows任务计划程序运行可执行文件,成功运行时返回0.但是,如果(1)任务无法运行,或者(2)返回代码(如果不是0),我想要一封电子邮件通知.
这是Windows Server 2003上的Windows任务计划程序可以执行的操作吗?
在我寻求根除cmd.exe [grin]的过程中,这里有一个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 = "sender@mydomain.com" $toAddress = "recipient@mydomain.com" $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) }