Advertisement
jheinrichs79

Send-PasswordExpiryReminderEmail

Nov 2nd, 2019
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Send-PasswordExpiryReminderEmail {
  2. param (
  3.         [string]$ToAddress
  4.     )
  5.  
  6.     $FromAddress = '=== ENTER FROM EMAIL HERE ==='
  7.     $SmtpServer = 'smtp.office365.com'
  8.     $SmtpPort = '587'
  9.     $Attachments = '==ENTER Attachment Here==='
  10.  
  11.     $UserName = '== ENTER USERNAME HERE'
  12.     $Password = ConvertTo-SecureString "==== ENTER PASSWORD =====" -AsPlainText -Force
  13.     $smtpCred = New-Object System.Management.Automation.PSCredential($username, $password)
  14.  
  15.     $mailParam = @{
  16.         To = $ToAddress
  17.         From = $FromAddress
  18.         Subject = 'Mail Migration Test'
  19.         Body = 'Your Teranet Password will expire in 5 days. `
  20.                Please read the inlcuded document on how to reset your password. `
  21.                This is an unmonitored email address. Please do not reply.'
  22.         SmtpServer = $SmtpServer
  23.         Port = $SmtpPort
  24.         Credential = $smtpCred
  25.         Attachments = $Attachments
  26.     }
  27.     Send-MailMessage @mailParam -UseSsl
  28. }
  29.  
  30.  
  31. $SearchBase = 'OU=Users,OU=Winnipeg,OU=MB,OU=TPR,DC=tprmb,DC=ca'
  32. $users = Get-ADUser -SearchBase $SearchBase -filter {Enabled -eq $True -and PasswordNeverExpires -eq $False} -Properties "SamAccountName","msDS-UserPasswordExpiryTimeComputed",mail |
  33.   Select-Object -Property "SamAccountName",Mail, @{Name="PwdExpiryDate"; Expression={[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")}}
  34.  
  35. $TodaysDate = get-date
  36. foreach ($user in $users){
  37.     if ($null -ne $user.mail){
  38.         $ts = New-TimeSpan -start $TodaysDate -end $user.PwdExpiryDate
  39.         $DaysUntilExpiry = $ts.Days
  40.          if($DaysUntilExpiry -eq 5){
  41.             write-host $user.Mail "-" $DaysUntilExpiry
  42.             Send-PasswordExpiryReminderEmail -ToAddress $user.Mail
  43.          }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement