排除禁用用户Get-aduser

时间:2016-11-08 18:22:57

标签: powershell

我想从此脚本中排除已禁用的用户,但似乎无法找到我如何尝试-exclude而没有运气。

如果您有更好的方式在开放式建议中

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 noreply@test.com -Subject "Votre mot de passe va expirer dans $DaysLeft jours" -body $WarnMsg  -smtpserver x.x.x.x -BodyAsHtml -Encoding $enc }

    }

   }
}

1 个答案:

答案 0 :(得分:3)

只需将您的过滤器 Get-AdUser *更改为'enabled -eq "true"'

Get-ADUser -filter 'enabled -eq "true"' -properties ...