##############################################################################
# Script Name: AD_Get_All_Nested_Members_Of_Group.ps1
# Version: 1.0
# Author: Dean Bunn
# Last Edited: 01/30/2012
# Description: Get All Nested Members of an AD Group
##############################################################################
#Function for Group Members Check
function checkMember([string]$objCN)
{
#Var for LDAP Path
$ldapPath = "LDAP://" + $objCN
#Retrieve AD Object
$adObj = [ADSI]$ldapPath
#Null Check on Class
if($adObj.Class)
{
#Switch Statement for Object Type
Switch($adObj.Class.ToString().ToLower())
{
#Group Objects
"group"
{
#Loop Through Group Members and Perform Recursive Member Check
foreach($member in $adObj.member)
{
#Check to See If Member Has Been Checked Already
if(!$htUniqueMembers.ContainsKey($member))
{
#Add Member to Unique HashTable
$htUniqueMembers.add($member,"1")
#Run checkMember on This Group Member
checkMember $member.ToString()
}
}
}#End of Group
#User Objects
"user"
{
#Check to See If Member Has Been Added to Unique Member. If Not Then Add
if(!$htUniqueMembers.ContainsKey($objCN))
{
$htUniqueMembers.add($objCN,"1")
}
}#End of User
#Add Other Classes Here If Necessary
}#End of Class Switch Statement
}#End Class Null Check
}#End of checkMember Function
#HashTable for All Unique Members
$htUniqueMembers = @{}
#Run checkMember on Top Group
checkMember "CN=STATS-ALL-MEMBERS,OU=DEPARTMENTS,DC=MYCOLLEGE,DC=EDU"
#Loop Through Each Unique Group Member
foreach($uCN in $htUniqueMembers.Keys)
{
Write-Host $uCN
}