Skip to content

Instantly share code, notes, and snippets.

@Dalmirog-zz
Created January 23, 2015 19:47
Show Gist options
  • Save Dalmirog-zz/f0fccde2ce1f92da986c to your computer and use it in GitHub Desktop.
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
$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