自今年年初以来使用PowerShell查找过期的AD用户

oxcyiej7  于 12个月前  发布在  Shell
关注(0)|答案(1)|浏览(115)

我正在准备一个审计,并试图通过PowerShell,我是一个菜鸟,以获得我们的数据。我使用Search-ADAccount命令列出我们的过期AD用户,我现在必须显示过期的用户在过去12个月内活跃
我知道我可以使用过滤器开关与Get-ADUser,但似乎不能让它与Search-ADAccount工作。

#Expired users
$Filename = "BJL Expired Users.csv"
Search-ADAccount -UsersOnly  -AccountExpired -filter {WhenChanged -ge '${StartDate} '} –ResultSetSize $null |  Select-Object Name, SamAccountName, DistinguishedName, mail, lastlogondate,Enabled,lockedOut,whenChanged,AccountExpirationDate,UserAccountControl,whenCreated| Export-CSV -path "$($ExportPath)$($Filename)"  –NoTypeInformation|out-gridview -title "Expired Accounts"

字符串
Search-ADAccount中不存在-filter交换机如何使用Search-ADAccount执行此操作
参考https://shellgeek.com/powershell-search-adaccount-cmdlet-examples/How to find users who were disabled specific dates

kx5bkwkv

kx5bkwkv1#

正如你已经说过的,Search-ADAccount没有-Filter参数,在这种情况下,你需要使用Get-ADUser并自己构造过滤器。
此示例使用LDAP筛选器查找whenChanged属性大于或等于1年前且accountExpires属性小于现在(UTC)的用户,这意味着帐户已过期。为了更好地理解LDAP语法,您可以检查Active Directory: LDAP Syntax Filters

$properties = @(
    'Name'
    'SamAccountName'
    'DistinguishedName'
    'mail'
    'lastlogondate'
    'Enabled'
    'lockedOut'
    'whenChanged'
    'AccountExpirationDate'
    'UserAccountControl'
    'whenCreated'
)

$filter = -join @(
    '(&'                                                                            # AND, all conditions must be met
        '(whenChanged>={0:yyyyMMddHHmmss.0Z})' -f [datetime]::UtcNow.AddYears(-1)   # `whenChanged` attribute is greater than or equal to 1 year ago
        '(!accountExpires>={0})' -f [datetime]::UtcNow.ToFileTimeUtc()              # `accountExpires` attribute is lower than now (UTC), meaning, the account is expired
    ')'                                                                             # close AND clause
)

$result = Get-ADUser -LDAPFilter $filter -Properties $properties |
    Select-Object $properties
$result | Export-Csv -Path "$($ExportPath)$($Filename)" -NoTypeInformation
$result | Out-GridView -Title 'Expired Accounts'

字符串

相关问题