R4maz
R4maz

Reputation: 11

Powershell Expiration Date calculating wrong days

good morning all,

I am stuck with an issue with the script created by Bradley Wyatt, it should be notifing users that their password is about to expire, but it has skipped a few users, as in these examples:

>C:\Users\> net user USER /domain
>Password expires             7/9/2021 8:20:25 AM

but in the script:

07/19/2021 08:00:02 - INFO: Password for USERNAME not expiring for 19 days

this is the mojo of the script:

$users = Get-Aduser -properties Name, PasswordNeverExpires, PasswordExpired, PasswordLastSet, EmailAddress -filter { (Enabled -eq 'True') -and (PasswordNeverExpires -eq 'False') } | Where-Object { $_.PasswordExpired -eq $False }

$maxPasswordAge = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge

 #Get Password last set date
$passwordSetDate = (Get-ADUser $user -properties * | ForEach-Object { $_.PasswordLastSet })
#Check for Fine Grained Passwords
$PasswordPol = (Get-ADUserResultantPasswordPolicy $user)
if (($PasswordPol) -ne $null) {
    $maxPasswordAge = ($PasswordPol).MaxPasswordAge
}

$expireson = $passwordsetdate + $maxPasswordAge
$today = (get-date)
#Gets the count on how many days until the password expires and stores it in the $daystoexpire var
$daystoexpire = (New-TimeSpan -Start $today -End $Expireson).Days


If (($daystoexpire -ge "0") -and ($daystoexpire -lt $expireindays)) {
    "$Date - INFO: Sending expiry notice email to $Name" | Out-File ($DirPath + "\" + "Log.txt") -Append
    Write-Host "Sending Password expiry email to $name" -ForegroundColor Yellow

    $SmtpClient = new-object system.net.mail.smtpClient
    $MailMessage = New-Object system.net.mail.mailmessage

can you guys get what am I missing?

Upvotes: 0

Views: 380

Answers (1)

R4maz
R4maz

Reputation: 11

Sorry for the trouble team, but one of my colleagues that has a quick hand changed the max password age in the DC without reporting me. thanks

Enforce password history 5 passwords remembered Maximum password age 60 days Minimum password age 5 days Minimum password length 14 characters Password must meet complexity requirements Enabled Store passwords using reversible encryption Disabled

Upvotes: 1

Related Questions