I want to exclude disabled user from this script but can't seem to find how i try the -exclude with no luck.
if you have a better way to do it in open to suggestion too
import-module ActiveDirectory;
$maxPasswordAgeTimeSpan = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge
Get-ADUser -filter * -properties PasswordLastSet, PasswordExpired, PasswordNeverExpires, EmailAddress, GivenName | foreach {
$today=get-date
$UserName=$_.GivenName
$Email=$_.EmailAddress
if (!$_.PasswordExpired -and !$_.PasswordNeverExpires) {
$ExpiryDate=$_.PasswordLastSet + $maxPasswordAgeTimeSpan
$DaysLeft=($ExpiryDate-$today).days
if ($DaysLeft -lt 10 -and $DaysLeft -gt 0){
$WarnMsg = "
<p style='font-family:arial'>Bonjour $UserName,</p>
<p style='font-family:arial'>Votre mot de passe va expirer dans $DaysLeft jours, S.V.P. changer votre mot de passe.</p>
<p style='font-family:arial'>Merci.</p>"
$enc = New-Object System.Text.utf8encoding
ForEach ($email in $_.EmailAddress) {
send-mailmessage -to test@test -from [email protected] -Subject "Votre mot de passe va expirer dans $DaysLeft jours" -body $WarnMsg -smtpserver x.x.x.x -BodyAsHtml -Encoding $enc }
}
}
}
Just change your filter at Get-AdUser from * to 'enabled -eq "true"'
Get-ADUser -filter 'enabled -eq "true"' -properties ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With