Advertisement
pekdotnet

windows password expiration reminder

Aug 8th, 2012
923
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import-module ActiveDirectory
  2.  
  3. $emailFrom = 'someone@examples.com'
  4. $smtpServer = 'smtp.example.com.com'
  5.  
  6. $adSearchContainer = 'domain.local/Users/*'
  7.  
  8. $subject = 'your subject'
  9. $body = 'your body'
  10. $smtp = new-object Net.Mail.SmtpClient($smtpServer)
  11.  
  12. $statusReport = "Password expiration notices sent today:`n`n"
  13.  
  14. $users = Get-ADUser -Filter * -Properties emailaddress,'msDS-UserPasswordExpiryTimeComputed',canonicalname | where-object {$_.canonicalname -like $adSearchContainer} | Select-Object name,samaccountname,emailaddress,@{Name='ExpiresOn';Expression={[datetime]::FromFileTime($_.'msDS-UserPasswordExpiryTimeComputed')}},@{Name='DaysLeft';Expression={([datetime]::FromFileTime($_.'msDS-UserPasswordExpiryTimeComputed')-(Get-Date)).Days}} | where-object {$_.DaysLeft -and $_.DaysLeft -ge 0}
  15.  
  16. foreach ($u in $users)
  17. {
  18.     $body = "";
  19.     $subject = "";
  20.     if ($u.DaysLeft -eq 0)
  21.     {
  22.         $subject = 'Your network password will expire in less than 24 hours'
  23.         $body = @"
  24. From the Helpdesk-
  25.  
  26. Your password will expire in less than 24 hours on $($u.ExpiresOn). Please change your password immediately.
  27. "@
  28.     }
  29.     elseif ($u.DaysLeft -le 1)
  30.     {
  31.         $subject = 'Your network password will expire in less than 48 hours'
  32.         $body = @"
  33. From the Helpdesk-
  34.  
  35. Your password will expire on $($u.ExpiresOn). Please change your password before it expires.
  36. "@
  37.     }
  38.     elseif ($u.DaysLeft -le 6)
  39.     {
  40.         $subject = "Your network password will expire in less than $($u.DaysLeft) days"
  41.         $body = @"
  42. From the Helpdesk-
  43.  
  44. Your password will expire on $($u.ExpiresOn). Please change your password before it expires.
  45. "@
  46.     }
  47.     elseif ($u.DaysLeft -eq 14)
  48.     {
  49.         $subject = 'Your network password will expire in approximately 14 days'
  50.         $body = @"
  51. From the Helpdesk-
  52.  
  53. Your password will expire on $($u.ExpiresOn). If you will be out of the office between now and then, please change your password before you leave to avoid having your password expire while you are out.
  54. "@
  55.     }
  56.    
  57.     if ($subject -ne "")
  58.     {
  59.         $body += @"
  60.  
  61.  
  62. To change your password while you are on a computer in the office, press Control Alt Delete and click the Change Password option.
  63.  
  64. To change your password while you are logged in to Remote Desktop, open the Start Menu and pick Windows Security from the lower right corner of the menu (right above the Log Off button). Then click Change Password.
  65.  
  66. Remember: if you get your email on your phone or tablet, or you use Remote Desktop on an iPad or a Mac, you will need to tell your device your new network password.
  67.  
  68. If you need help, please call the Helpdesk at x800.
  69. "@
  70.         $smtp.Send($emailFrom, $u.emailaddress, $subject, $body)
  71.         $statusReport += "$($u.emailaddress) expires on $($u.ExpiresOn) in $($u.DaysLeft) days`n"
  72.         Write-Output "$($u.emailaddress) expires on $($u.ExpiresOn) in $($u.DaysLeft) days"
  73.     }
  74. }
  75.  
  76. $statusReport += "`n`nThis report was generated on "
  77. $statusReport += gc env:computername
  78. $smtp.Send($emailFrom, $emailFrom, 'Password Expiration Status Report', $statusReport)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement