Created
January 23, 2015 19:47
-
-
Save Dalmirog-zz/f0fccde2ce1f92da986c to your computer and use it in GitHub Desktop.
Easily readable snipped to send email and retry X times in case of failure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$Try = 0 | |
$MaxRetries = 5 | |
$SecsBeforeRetry = 5 | |
$PSEmailServer = "smtp.company.com" | |
$from = "[email protected]" | |
$to = "[email protected]" | |
$subject = "Amazing Subject" | |
$body = "Interesting stuff" | |
do{ | |
try{ | |
Send-MailMessage -From $from -to $to -Subject $subject -Body $body -ErrorAction stop | |
write-output "`nEmail Sent!`n" | |
$try = $MaxTries | |
} | |
catch{ | |
$try++ | |
Write-Output "Failed Try: $try" | |
Write-output "Error: $($_.exception.message)" | |
if ($Try -eq $MaxTries){ | |
throw "Command failed $MaxRetries times" | |
} | |
Start-Sleep -Seconds $SecsBeforeRetry -Verbose | |
} | |
}until($Try -eq $MaxRetries) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment