Question Locked

Password Expires Notification

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I have a PowerShell script that sends Email notifications to users in the OU when their password expires 15 days out. I have the script but it is sending notifications to every user. Can someone please help with what is causing this.I appreciate any and all suggestions. This script will be running on Windows Server 2003 Standard 64bit. Script as below:

function Send-Mail{

param($smtpServer,$from,$to,$subject,$body)

$smtp = new-object system.net.mail.smtpClient($SmtpServer)
$mail = new-object System.Net.Mail.MailMessage

$mail.from = $from
$mail.to.add($to)
$mail.subject = $subject
$mail.body = $body
#$mail.IsBodyHtml = $true

$smtp.send($mail)
}

# get domain maximumPasswordAge password policy
$maximumPasswordAge = (Get-QADObject (Get-QADRootDSE).defaultNamingContextDN).maximumPasswordAge.value.days

if(!$maximumPasswordAge){
throw "Domain 'MaximumPasswordAge'password policy is not configured (set to 0)."
}

# exclude users that cannot change password
$ldap = "(!userAccountControl:1.2.840.113556.1.4.803:=64)(mail=*)"

# create calculated property to display days until password expire
$daysUntilExpire = @{n="daysUntilExpire";e={$maximumPasswordAge-$_.passwordAge.value.days}}

$expireIn = 15

# get enabled users that meet the above criteria
$expiredUsers = Get-QADUser -enabled -passwordNeverExpires $false -size 0 -ldap $ldap | where {$_.passwordAge.value -gt 0 -AND ($maximumPasswordAge-$_.passwordAge.value.days) -gt $expireIn}

$expiredUsers | foreach {
$subject="TEST ONLY FROM THE IT DEPARTMENT, Your password will expire in $expireIn days"
$body="DISREGARD THIS NOTICE AS IT IS AN INTERNAL TEST ONLY FROM THE IT DEPARTMENT, Your password will expire in $expireIn days"
Send-Mail -smtpServer exServerName -from "anyone@somewhere.com" -to $_.email -subject $subject -body $body
}

3 replies

by john.flynn

$ldap | where {$_.passwordAge.value -gt 0 -AND ($maximumPasswordAge-$_.passwordAge.value.days) -gt $expireIn}

Should read

$ldap | where {$_.passwordAge.value -gt 0 -AND ($maximumPasswordAge-$_.passwordAge.value.days) -lt $expireIn}

Thanks for your post I have enjoyed it. Still working through the complete solution

by dwphilips

Thank you for your help. It's working now. I'm pasting the final code. It warns user every odd day from 15 days out. Feel free to use

function Send-Mail{

param($smtpServer,$from,$to,$subject,$body)

$smtp = new-object system.net.mail.smtpClient($SmtpServer)
$mail = new-object System.Net.Mail.MailMessage

$mail.from = $from
$mail.to.add($to)
$mail.subject = $subject
$mail.body = $body
#$mail.IsBodyHtml = $true

$smtp.send($mail)
}

add-pssnapin "Quest.ActiveRoles.ADManagement"

# get domain maximumPasswordAge password policy
$maximumPasswordAge = (Get-QADObject (Get-QADRootDSE).defaultNamingContextDN).maximumPasswordAge.value.days

if(!$maximumPasswordAge){
throw "Domain 'MaximumPasswordAge'password policy is not configured (set to 0)."
}

# exclude users that cannot change password
$ldap = "(!userAccountControl:1.2.840.113556.1.4.803:=64)(mail=*)"

# create calculated property to display days until password expire
$daysUntilExpire = @{n="daysUntilExpire";e={$maximumPasswordAge-$_.passwordAge.value.days}}

$expireIn = 15

# get enabled users that meet the above criteria
$expiredUsers = Get-QADUser -enabled -passwordNeverExpires $false -size 0 -ldap $ldap | where {($_.passwordAge.value -gt 0) -AND ($_.passwordAge.value.days -lt $maximumPasswordAge) -AND (($maximumPasswordAge-$_.passwordAge.value.days) -lt $expireIn)}

$minLogin = ([datetime]::Now).Date.AddDays(-60)
$newline = [char]13+[char]10

foreach ($user in $expiredUsers) {

if ($user.lastLogonTimestamp.value -eq $null) { continue }
if ($user.lastLogonTimestamp.value -lt $minLogin) { continue }

$remain = $maximumPasswordAge-($user.passwordAge.value.days)

if (($remain % 2) -ne 1) { continue }

$body="The password for your account '"+$user.samAccountName+"' will expire in $remain days."+$newline+$newline+"To change your password, please login to Citrix at https://any.any.com/ or Outlook Web Access at https://any.any.com/"
$body = $body + $newline+$newline + "Citrix users: Please login with your current username and password, select Web Interface Logon, then click on the yellow 'key' icon in the upper right of the Applications window to change your password."
$body = $body + $newline+$newline + "Email users: Please login with your current username and password, select 'Options' in the upper right, then 'Change Password' on the left. Enter your old and new password, then click the 'Save' button at the top above the icon of the keys."

$subject="Your password will expire in $remain days"
$to = $user.email

Send-Mail -smtpServer "your server" -from "anyaddress@something.com" -to $to -subject $subject -body $body
}

This discussion is closed to new replies.