SHARE
TWEET

Untitled

a guest Jul 19th, 2019 285 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Find accounts that are enabled and have expiring passwords
  2. $users = Get-ADUser -filter {Enabled -eq $True -and PasswordNeverExpires -eq $False -and PasswordLastSet -gt 0 } `
  3. -Properties "Name", "EmailAddress", "msDS-UserPasswordExpiryTimeComputed" | Select-Object -Property "Name", "EmailAddress", `
  4. @{Name = "PasswordExpiry"; Expression = {[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed").tolongdatestring() }}
  5.  
  6. foreach ($user in $users) {
  7. $exp = $user.PasswordExpiry
  8. $days = @()
  9. foreach ($d in 7,3,1){$days += (get-date).adddays($d).ToLongDateString()}
  10.     if($exp -in $days){
  11.         $count = (New-TimeSpan -Start (Get-Date) -End $exp).Days
  12.         $body = "This is an automated message. This is to inform you that the password for $($user.name)"
  13.         $body += " will expire in $count days on $exp. Please contact the helpdesk if "
  14.         $body += "you need assistance changing your password. PLEASE DO NOT REPLY TO THIS EMAIL."
  15.         Send-MailMessage -To $user.EmailAddress`
  16.                          -From "Password Expiration Warning<email>"`
  17.                          -SmtpServer "XXXXXXX"`
  18.                          -Subject "PASSWORD EXPIRATION WARNING - Your account password will expire soon."`
  19.                          -Body $body
  20.      }
  21. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top