简体   繁体   中英

Powershell trying to copy users from one AD group to another

I am trying to copy from one ad group to another. However I am having trouble with piping the data and feeding the add-group command with array items please help

    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 }

You break it by using Format-Table. Formatting is for display, you should not format anything you expect to work with in the shell. By all means format it at the end once you're done.

Anyway, you should be able to simplify it.

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

Chris

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM