【问题标题】:Powershell: Find users with the same attribute value in Active DirectoryPowershell:在 Active Directory 中查找具有相同属性值的用户
【发布时间】:2020-06-10 22:47:42
【问题描述】:
我一直在从事一个涉及 Active Directory 的身份管理项目,并遇到了一个我无法弄清楚 Powershell 的案例。本质上,我们正在查看employeeID 属性,我们希望在整个域中找到该属性中具有相同值的所有用户。用户不应具有相同的employeeID,因此如果有两个或更多具有相同的employeeID。它们需要清理干净。
我知道 Powershell 可以为我执行此操作,但我不确定我需要哪些命令。我一直在寻找 Get-ADUser ,但没有什么可以让我开始。我本质上只是想要一份与其他用户具有相同员工 ID 的所有用户的报告,以便清理它们。
【问题讨论】:
标签:
powershell
active-directory
【解决方案1】:
你可以:
- 枚举所有具有
employeeID 值的帐户
- 使用
Group-Object根据值对它们进行比较和分组
# Fetch all user accounts with an employeeID
$employeeAccounts = Get-ADUser -Filter 'employeeID -like "*"' -Properties employeeID
# Group them by value of employeeID attribute, keep any group with more than 1 account
$accountsByEmployeeID = $employeeAccounts |Group-Object employeeID |Where-Object Count -gt 1
foreach($ID in $accountsByEmployeeID){
# $accounts will contain a list of accounts with the same employeeID
# you could send an email, file a ticket, or disable one or more of the accounts here
$accounts = $ID.Group
}