Posted By

samcogan on 11/13/09


Tagged

PowerShell


Versions (?)

Powershell Send email on scheduled task failure


 / Published in: Other
 

  1. # attempt to run your exe. iex is an alias for the invoke-expression cmd
  2. iex c:\path_to_exe\myprog.exe
  3.  
  4. # $? lets us know if the previous command was successful or not
  5. # $LASTEXITCODE gives us the exit code of the last Win32 exe execution
  6. if (!$? -OR $LASTEXITCODE -gt 0)
  7. {
  8. $smtpServer = "smtp.mydomain.com"
  9. $fromAddress = "[email protected]"
  10. $toAddress = "[email protected]"
  11. $subject = "FAIL"
  12. $msgBody = "HEY, YOU GOT PROBLEMS"
  13.  
  14. # This block is optional depending on your SMTP server config
  15. # You need it if your SMTP server requires authentication
  16. $senderCreds = new-object System.Net.networkCredential
  17. $senderCreds.UserName = "senderusername"
  18. $senderCreds.Password = "senderpwd"
  19.  
  20. $smtpClient = new-object Net.Mail.SmtpClient($smtpServer)
  21. $smtpClient.Credentials = $senderCreds
  22. $smtpClient.Send($fromAddress,$toAddress,$subject,$msgBody)
  23. }

Report this snippet  

You need to login to post a comment.