简体   繁体   中英

Add ALL users of a OU to a security group also the ones in a security group

I want to add all users of my OU into a specific security group. I used the command below, which showed me all Users of the OU, but not the ones in a security group.

So how can I fix my command to show me ALL Users in the OU?

GET-ADUser -SearchBase 'OU=OU, DC=DC' -Filter 'enabled -eq $true | % {Add-ADGroupMember 'Security-Group' -Members $_}

That is because you query all users not groups . If the users inside the security groups are located in a different OU you should also check that OU. Or do something like:

$users = Get-ADUser -SearchBase 'OU=OU, DC=DC' -Filter {(enabled -eq $true)}
foreach($user in $users){
    Add-ADGroupMember 'security-group' -Members $user
}
$groups = Get-ADGroup -Filter * -SearchBase 'OU=OU, DC=DC'
foreach($group in $groups){
    $members = Get-ADGroupMember $group
    foreach($member in $members){
        Add-ADGroupMember 'security-group' -Members $member
    }
}

With the above script, everybody will be added to the security group including users not in that OU but inside a group in that OU.

Hope this helps!

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