Search This Blog

Friday, September 24, 2010

Script to make users member of a new group

Senario:
GroupTestA has 2000 members which needs to be added to another new group GroupTestB. There is no group nesting.

*The script will work only when there are no members present in new group otherwise script fails. I haven't added thelogic where it checks for member present in both groups and skip them.

$root = [adsi]""
$rootdn = $root.distinguishedName
#Bind to the First Group DN

$groupTA = [adsi]("ldap://CN=GroupTestA, OU=Testing OU," + $rootdn)
$GroupMembers = $groupTA.member

#Bind to Second Group DN
$groupTB = [adsi]("ldap://CN=GroupTestB, OU=Testing OU," + $rootdn)
foreach($dn in $groupMembers)
{
$groupTB.member.add($dn)
}
$groupTB.Setinfo()

************************
Just created one with logic. It works well and tested ok but it throws exception.....
# Logic to skip the common members and add only unqiue members

foreach($dnA in $groupMembersA)
{
 foreach($dnB in $groupMembersB)
{
 if($dnA -ne $dnB) {
 write-host $dnA -ForegroundColor GREEN
$groupTB.member.add($dnA)
 $groupTB.Setinfo()
}
else {
write-host $dnB -foregroundcolor RED
}
 }
 }
************************************************

2 comments:

  1. -Disclaimer (code66)September 27, 2010 at 3:56 PM

    Why not just add the whole GroupTestA as a member of the new group GroupTestB?

    ReplyDelete
  2. Well, I suggested them this however due to requirement issues they want it that way.

    ReplyDelete