Skip to content

Instantly share code, notes, and snippets.

  • Save killahquam/04795621858ca06b33fc6b34c6d19d4e to your computer and use it in GitHub Desktop.
Save killahquam/04795621858ca06b33fc6b34c6d19d4e to your computer and use it in GitHub Desktop.
# Requires ActiveRoles CmdLets from Quest Software (They're free and awesome) - http://www.quest.com/powershell/activeroles-server.aspx
Get-QADUser -SizeLimit 0 | Select-Object samAccountName,mail,PasswordStatus |
Where-Object {$_.PasswordStatus -ne "Password never expires" -and $_.PasswordStatus -ne "Expired" -and $_.PasswordStatus -ne "User must change password at next logon." -and $_.mail -ne $null} |
ForEach-Object {
$today = Get-Date
$logdate = Get-Date -format yyyyMMdd
$samaccountname = $_.samAccountName
$mail = $_.mail
$passwordstatus = $_.PasswordStatus
$passwordexpiry = $passwordstatus.Replace("Expires at: ","")
$passwordexpirydate = Get-Date $passwordexpiry
$daystoexpiry = ($passwordexpirydate - $today).Days
if ($daystoexpiry -lt 14) {
$emailFrom = "PasswordManagement@yourdomain.com"
$emailTo = "$mail"
$subject = "Your password will expire in $daystoexpiry days"
$body = "Please change your password to prevent loss of access to your corporate systems`n`n"
$body += "If you are unable to change your password, please contact the help desk"
$attachment = "C:\batch\docs\PasswordChange.doc" # Attach a HowTo document for changing passwords
$smtpserver = "smtp.yourdomain.com"
Send-MailMessage -To $emailTo -From $emailFrom -Subject $subject -Body $body -Attachments $attachment -SmtpServer $smtpserver
Write-Host "Email was sent to $mail on $today"
Add-Content c:\batch\logs\maillog$logdate.txt "Email was sent to $mail on $today"
}
}
Send-MailMessage -To "administrator@yourdomain.com" -From "PasswordManagementProcess@yourdomain.com" -Subject "Password change log for $today" -Body "This is the log from $today" -Attachments "d:\batch\logs\maillog$logdate.txt" -SmtpServer $smtpserver
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment