Guest User

Untitled

a guest
May 25th, 2017
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Email-It ($inputFile){
  2.     #Define our email Settings
  3.     $smtpServer = "SMTP.SERVER.COM"
  4.     $smtpTo = "[email protected]"
  5.     $messageSubject = "$env:computername Group Membership Report"
  6.    
  7.     #Create the message
  8.     $message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
  9.     $message.Subject = $messageSubject
  10.     $message.Attachments.Add($inputFile)
  11.     $message.Body = "Attached you will find the group membership report for $env:computername."
  12.  
  13.     #Connect to the mail server and send the message
  14.     for ($k=0; $k -lt 3; $k++){
  15.         try {
  16.             $ErrorActionPreference = "Stop"
  17.             $smtp = New-Object Net.Mail.SmtpClient($smtpServer)
  18.             $smtp.Send($message)
  19.             $ErrorActionPreference = "Continue"
  20.            
  21.             #Try to send it three times, or report error on failure
  22.             $k = 3
  23.         }
  24.         catch {
  25.             if ($k -lt 3) {
  26.                 Write-Output "The message did not send.  Waiting 30 seconds and trying again."
  27.             }
  28.             else {
  29.                 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"
  30.                 $_.ErrorDetails | Format-List *
  31.             }
  32.             Start-Sleep -s 30
  33.         }
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment