此脚本现在可以正常运行,但问题是当找到搜索库中的多个组时,该脚本会将所有组中的所有用户添加到跨林目标组。
例如:
ForestAGroup1 =包含2个用户
ForestAGroup2 =包含2个用户
::运行脚本::
现在...
ForestBGroup1 =包含4个用户
ForestBGroup2 =包含4个用户
ForestBGroup1 / 2需要包含与ForestAGroup1 / 2相同的用户。
以下是供参考的脚本:
$creds = Get-Credential
$Groups = Get-ADGroup -Properties * -Filter * -SearchBase "OU=TEST,OU=Shop Print Groups,OU=User,OU=domain Groups,DC=domainA,DC=com" | export-csv c:\temp\test.csv
$Groups = Get-ADGroup -Properties * -Filter * -SearchBase "OU=TEST,OU=Shop Print Groups,OU=User,OU=domain Groups,DC=domainA,DC=com"
Foreach($G In $Groups)
{
#Display group members and group name
Write-Host $G.Name
Write-Host "-------------"
$G.Members
#Add members to domainB group
$domainGMembers = import-csv C:\temp\test.csv | ForEach-Object -Process {Get-ADGroupMember -Identity $_.CN} | Select-Object samaccountname | export-csv c:\temp\gmembers.csv
$domainDNUser = import-csv C:\temp\gmembers.csv | ForEach-Object -Process {Get-ADUser $_.samaccountname -Server "domainA.com" -properties:Distinguishedname}
import-csv C:\temp\gmembers.csv | ForEach-Object -Process {Add-ADGroupMember -Server "domainB.com" -Identity $G.Name -Members $domainDNUser -Credential $creds -Verbose}
}
答案 0 :(得分:0)
你在做什么?
TEST
- OU中所有群组的所有成员添加到domainB
SamAccountName
时,您搜索用户对象以获得DN
。然后,您使用SamAccountName
来查找DN
。试试这个(未经测试):
$creds = Get-Credential
$Groups = Get-ADGroup -Properties Members -Filter * -SearchBase "OU=TEST,OU=Shop Print Groups,OU=User,OU=domain Groups,DC=domain,DC=com"
Foreach($G In $Groups)
{
#Display group members and group name
Write-Host $G.Name
Write-Host "-------------"
$G.Members
#Add members to domainB group
$G.Members |
Get-ADUser -Server fairfieldmfg.com |
ForEach-Object { Add-ADGroupMember -Server "domainB.com" -Identity $G.Name -Members $_ -Credential $creds -Verbose }
}
我使用了一个foreach循环来运行Add-ADGroupMember
,因为它通常在一组成员中间失败,如果它在已经是成员的情况下发现,但是如果我们一次添加一个成员在那附近(或者你可以进行搜索并排除已经在组中的那些)。
您可能希望将-ErrorAction SilentlyContinue
添加到Add-ADGroupMember
,以便在您知道脚本正常工作时忽略这些错误。