Manage Shadow Group Membership with powershell AD Cmdlets
Sometimes, in our Active Directory structure, we need a group to reflect the contents of an OU. One example of this is If you organise you users and computers in location specific OU's and need to use RODC password replication policy.
In this script I use PowerShell v2 AD cmdlets to enumerate group membership and OU membership and then use compare-object to work out the differences. Finally the switch reads if the additional user was found in the group or the OU and either adds or removed the member accordingly.
This script uses get-aduser, but get-adcomputer will work just as well. I am wouking on a more comprehensive solution using get-adobject to deal with multiple object types.
Sometimes, in our Active Directory structure, we need a group to reflect the contents of an OU. One example of this is If you organise you users and computers in location specific OU's and need to use RODC password replication policy.
In this script I use PowerShell v2 AD cmdlets to enumerate group membership and OU membership and then use compare-object to work out the differences. Finally the switch reads if the additional user was found in the group or the OU and either adds or removed the member accordingly.
This script uses get-aduser, but get-adcomputer will work just as well. I am wouking on a more comprehensive solution using get-adobject to deal with multiple object types.
$Group = "shadowgroup"
$OU = "OU=ShadowOU,DC=domain,DC=com"
$users = $(get-aduser -SearchBase $OU -filter "*")
$groupmembers = Get-ADGroupMember -Identity $Group
switch (Compare-Object -ReferenceObject $groupmembers -DifferenceObject $users -property name){
{$_.SideIndicator -eq "=>"} {add-adgroupmember -identity $group -member $_.name}
{$_.SideIndicator -eq "<="} {remove-adgroupmember -identity $group -member $_.name -confirm:$false} }
Comments
Post a Comment