Return to Snippet

Revision: 20368
at November 13, 2009 11:00 by samcogan


Initial Code
# 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)
}

Initial URL


Initial Description


Initial Title
Powershell Send email on scheduled task failure

Initial Tags


Initial Language
Other