使用 Powershell 查找年初以来过期的 AD 用户

问题描述 投票:0回答:1

我正在准备审核并尝试通过 PowerShell 获取我们的数据,但我对这方面是个菜鸟。 我正在使用

Search-ADAccount
命令列出过期的 AD 用户,我现在必须显示过去十二个月内处于活动状态的过期用户

我知道我可以将过滤器开关与

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"

-filter
开关在
Search-ADAccount
中不存在,我该如何使用
Search-ADAccount

执行此操作

参考资料https://shellgeek.com/powershell-search-adaccount-cmdlet-examples/ 如何查找在特定日期被禁用的用户

powershell active-directory
1个回答
0
投票

正如您已经说过的,

Search-ADAccount
没有
-Filter
参数,在这种情况下您需要使用
Get-ADUser
并自己构建过滤器。这应该可以实现您正在寻找的内容。

$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'
© www.soinside.com 2019 - 2024. All rights reserved.