Windows 任务计划程序电子邮件通知?

raf*_*ael 3 windows-server-2003 scheduled-task

我正在使用 Windows 任务计划程序运行一个可执行文件,当它成功运行时返回 0。但是,如果 (1) 任务无法运行或 (2) 返回代码不是 0,我想要电子邮件通知。

这是 Windows Server 2003 上的 Windows 任务计划程序可以做的事情吗???

squ*_*man 5

在我寻求根除 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)
}
Run Code Online (Sandbox Code Playgroud)

  • 是的,因为我不必同时保留一份 blat 或其他邮件的副本。 (3认同)