Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function Email-It ($inputFile){
- #Define our email Settings
- $smtpServer = "SMTP.SERVER.COM"
- $messageSubject = "$env:computername Group Membership Report"
- #Create the message
- $message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
- $message.Subject = $messageSubject
- $message.Attachments.Add($inputFile)
- $message.Body = "Attached you will find the group membership report for $env:computername."
- #Connect to the mail server and send the message
- for ($k=0; $k -lt 3; $k++){
- try {
- $ErrorActionPreference = "Stop"
- $smtp = New-Object Net.Mail.SmtpClient($smtpServer)
- $smtp.Send($message)
- $ErrorActionPreference = "Continue"
- #Try to send it three times, or report error on failure
- $k = 3
- }
- catch {
- if ($k -lt 3) {
- Write-Output "The message did not send. Waiting 30 seconds and trying again."
- }
- else {
- Write-Output "The message did not send 3 times. Please check the error details and modify the SMTP settings defined under the Email-It function header"
- $_.ErrorDetails | Format-List *
- }
- Start-Sleep -s 30
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment