Powershell试图将用户从一个AD组复制到另一个AD组

时间:2016-08-05 14:01:23

标签: powershell active-directory

我正在尝试从一个广告组复制到另一个广告组。但是我在管道数据和使用数组项添加add-group命令时遇到问题请帮助

    Write-Host "You have selected the following group: $SourceGroup"
$SourceGroup = Read-Host

Write-Host "Enter the name of the new group you want the membership of $SourceGroup copied to"
$DestinationGroup = Read-Host



$P1 = Get-ADGroup $SourceGroup -Property member | Select member | Format-Table -HideTableHeaders
$P2 = Foreach ($Member in $P1) { Get-ADUser $Member -Property SamAccountName | Select SamAccountName }


Foreach ($Member in $P1) { Add-ADGroupMember -Identity $DestinationGroup -Member $Member }

1 个答案:

答案 0 :(得分:4)

使用Format-Table打破它。格式化是为了显示,您不应该格式化您希望在shell中使用的任何内容。一旦完成,请务必将其格式化。

无论如何,你应该能够简化它。

Get-ADGroup $SourceGroup | Get-ADGroupMember | ForEach-Object {
    Add-ADGroupMember $DestinationGroup -Member $_.DistinguishedName
}

克里斯