您可以使用PowerShell编写脚本来实现此操作。以下是一个示例脚本:
# 设置要比较的两个AD组的名称
$group1 = "GroupName1"
$group2 = "GroupName2"
# 获取这两个组的成员,并将它们存储在变量中
$members1 = Get-ADGroupMember -Identity $group1
$members2 = Get-ADGroupMember -Identity $group2
# 创建一个空数组,用于存储存在于这两个组中的用户
$usersInBoth = @()
# 检查在两个组中都存在的用户,并将它们添加到$usersInBoth数组中
foreach($user1 in $members1) {
foreach($user2 in $members2) {
if($user1.DistinguishedName -eq $user2.DistinguishedName) {
$usersInBoth += $user1
}
}
}
# 如果$usersInBoth数组不为空,则将其转换为Excel电子表格并导出
if($usersInBoth.Length -ne 0) {
$exportPath = "C:\Users\John\Documents\UsersInBothGroups.xlsx"
$usersInBoth | Select-Object SamAccountName, Name | Export-Excel -Path $exportPath -AutoSize
} else {
Write-Host "No users found in both groups."
}
这个脚本首先获取指定的两个AD组的成员,然后使用嵌套foreach循环比较这两个成员列表中的每个用户。如果用户在两个组中都存在,则将其添加到一个名为$usersInBoth的数组中。最后,如果数组中有用户,则将其转换为Excel电子表格并保存到指定的路径中。
上一篇:比较对象并导出差异
下一篇:比较对象并提取深层差异