我对PowerShell相对较新,并且我正在尝试为涉及Active Directory的项目学习它。我的任务是比较AD中两个不同安全组的成员(两个组都位于同一个OU中),并将组2中不属于组1的成员复制到组1。
我遇到了this link,它展示了如何比较群组,但是:
答案 0 :(得分:1)
你走了。尝试下面我为您的要求编写的脚本。
#Input Parameters. Change these as per your requirement
$group1 = "Group1Name"
$group2 = "Group2Name"
$membersInGroup1 = Get-ADGroupMember $group1
$membersInGroup2 = Get-ADGroupMember $group2
if($membersInGroup1 -eq $null)
{
Add-ADGroupMember -Identity $group1 -Members $membersInGroup2
}
elseif($membersInGroup2 -ne $null)
{
$separateMembers = diff $membersInGroup1 $membersInGroup2
if($separateMembers -ne $null)
{
foreach($member in $separateMembers)
{
$currentUserToAdd = Get-ADUser -Identity $member.InputObject
Add-ADGroupMember -Identity $group1 -Members $currentUserToAdd
}
}
}
如果您遇到任何问题,请告诉我。
答案 1 :(得分:0)
我找到了另一个非常容易的解决方案,我无法抗拒与大家分享:
Get-ADGroupMember -Identity GROUP-A | Add-ADPrincipalGroupMembership -MemberOf GROUP-B
Get-ADGroupMember
将GROUP-A的所有成员和管道导入Add-ADPrincipalGroupMembership
cmdlet,将传入的成员添加到GROUP-B。
答案 2 :(得分:0)
给出两个安全组,即DestinationGroup(组1),SourceGroup(组2):
Add-ADGroupMember -Identity "DistinguishedName of DestinationGroup" -Members (Get-ADGroupMember -Identity "DistinguishedName of SourceGroup" | Select-Object -ExpandProperty distinguishedName)