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
}