Skip to content

Instantly share code, notes, and snippets.

@D4R4
Last active August 8, 2021 22:23
Show Gist options
  • Save D4R4/08c1542a9c317d9555931deb60b8f3fb to your computer and use it in GitHub Desktop.
Save D4R4/08c1542a9c317d9555931deb60b8f3fb to your computer and use it in GitHub Desktop.
POWERSHELL Send an email to admin regarding users with old passwords
Import-Module ActiveDirectory
$today = Get-Date
$checkDate = $today.Date.AddDays(-350)
Try
{
$results = Get-ADUser -Filter {Memberof -eq "CN=company,OU=Groups,DC=company,DC=net"} -properties passwordlastset -SearchBase "CN=Users,DC=company,DC=net" | Where-Object {$_.passwordlastset -lt $checkDate}
$results_string = $results | Sort-Object -property passwordlastset | Format-Table Name, passwordlastset -HideTableHeaders | Out-String
$Subject = "*DOMAIN CONTROLLER MONITORING* Old User Passwords"
$Body = @"
The following user passwords are older than 10 months:
$($results_string)
"@
Send-MailMessage -From "[email protected]" -To "[email protected]" -Subject $Subject -Body $Body -SmtpServer "smtp.company"
# Sending Email to the users
$email_addresses = $results
$email_addresses | ForEach-Object{
$email="[email protected]"
#$email=$_.UserPrincipalName
$name=$_.GivenName
$template = Get-Content -Path './password_age_email_template.html' | Out-String
$template = $template -replace "###NAME###", $name
$Subject = "COMPANY Domain Controller: Expired account password"
Send-MailMessage -BodyAsHtml -From "[email protected]" -To "$email" -Subject $Subject -Body $template -SmtpServer "smtp.server.com"
}
}
Catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]
{
# Do stuff if not found
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment