Modifying nested security group Powershell to show group name

Hi People,

Can anyone please assist me in modifying the script below so that it shows the nested group name before it indent and list all of the users ?

###################################
# Get-MyGroupMembersRecursive.ps1 #
# Created by Hugo Peeters         #
# http://www.peetersonline.nl     #
###################################

param($ParentGroupNames)

$Global:myCol = @()

function Indent	{
	param([Int]$Level)
	$Global:Indent = $null
	For ($x = 1 ; $x -le $Level ; $x++)	{
		$Global:Indent += "`t"
	}
}

function Get-MySubGroupMembersRecursive	{
	param($DNs)
	ForEach ($DN in $DNs) {
		$Object = Get-QADObject $DN
		If ($Object.Type -eq "Group") {
			$i++
			Indent $i
			Write-Host ("{0}{1}" -f $Indent,$Object.DisplayName) -ForegroundColor "yellow"
			$Group = Get-QADGroup $DN
			If ($Group.Members.Length -ge 1) {
				Get-MySubGroupMembersRecursive $Group.Members
			}
			$i--
			Indent $i
			Clear-Variable Group -ErrorAction SilentlyContinue
		}
		Else {
			$userfound = Get-QADUser $DN | Select Name, Email
			Write-Host ("{0} {1}" -f $Indent,$userfound.Name)
			$Global:myCol += $userfound
			Clear-Variable userfound -ErrorAction SilentlyContinue
		}
	}
}

ForEach ($ParentGroupName in $ParentGroupNames)	{
	$Global:Indent = $null
	$ParentGroup = Get-QADGroup -Name $ParentGroupName
	Write-Host "====================="
	Write-Host " TREE VIEW PER GROUP"
	Write-Host "====================="
	Write-Host ("{0}" -f $ParentGroup.DisplayName) -ForegroundColor "yellow"
	If ($ParentGroup -eq $null) {
		Write-Warning "Group $ParentGroupName not found."
		break
	}
	Else {
		Write-Host "$ParentGroupName"
		$FirstMembers = $ParentGroup.Members
		ForEach ($member in $firstmembers) {
			#Write-Host "`n $member ... group"
			Get-MySubGroupMembersRecursive $member
			Write-Host "-----------------------"
		}
	}
}
Write-Host ""
Write-Host "====================="	
Write-Host " All Unique Members: "
Write-Host "====================="	
$myCol | Sort Name | Select Name, Email -Unique

Open in new window


Thanks in advance.
LVL 9
Senior IT System EngineerIT ProfessionalAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris DentPowerShell DeveloperCommented:
It needs to use Name instead of DisplayName (for the group).
###################################
# Get-MyGroupMembersRecursive.ps1 #
# Created by Hugo Peeters         #
# http://www.peetersonline.nl     #
###################################

param($ParentGroupNames)

$Global:myCol = @()

function Indent	{
	param([Int]$Level)
	$Global:Indent = $null
	For ($x = 1 ; $x -le $Level ; $x++)	{
		$Global:Indent += "`t"
	}
}

function Get-MySubGroupMembersRecursive	{
	param($DNs)
	ForEach ($DN in $DNs) {
		$Object = Get-QADObject $DN
		If ($Object.Type -eq "Group") {
			$i++
			Indent $i
			Write-Host ("{0}{1}" -f $Indent,$Object.Name) -ForegroundColor "yellow"
			$Group = Get-QADGroup $DN
			If ($Group.Members.Length -ge 1) {
				Get-MySubGroupMembersRecursive $Group.Members
			}
			$i--
			Indent $i
			Clear-Variable Group -ErrorAction SilentlyContinue
		}
		Else {
			$userfound = Get-QADUser $DN | Select Name, Email
			Write-Host ("{0} {1}" -f $Indent,$userfound.Name)
			$Global:myCol += $userfound
			Clear-Variable userfound -ErrorAction SilentlyContinue
		}
	}
}

ForEach ($ParentGroupName in $ParentGroupNames)	{
	$Global:Indent = $null
	$ParentGroup = Get-QADGroup -Name $ParentGroupName
	Write-Host "====================="
	Write-Host " TREE VIEW PER GROUP"
	Write-Host "====================="
	Write-Host ("{0}" -f $ParentGroup.Name) -ForegroundColor "yellow"
	If ($ParentGroup -eq $null) {
		Write-Warning "Group $ParentGroupName not found."
		break
	}
	Else {
		Write-Host "$ParentGroupName"
		$FirstMembers = $ParentGroup.Members
		ForEach ($member in $firstmembers) {
			#Write-Host "`n $member ... group"
			Get-MySubGroupMembersRecursive $member
			Write-Host "-----------------------"
		}
	}
}
Write-Host ""
Write-Host "====================="	
Write-Host " All Unique Members: "
Write-Host "====================="	
$myCol | Sort Name | Select Name, Email -Unique

Open in new window


Cheers,

Chris
0
Chris DentPowerShell DeveloperCommented:
Such a lot of code troubles me a bit. It could be shortened to this:
Function Get-GroupMemberTree {
  Param(
    $Identity,
    $IndentLevel = 0,
    $IndentCharacter = "`t"
  )

  Write-Host "$($IndentCharacter * $IndentLevel)$($Identity -replace '^CN=|,(CN|OU|DC)=.+$')" -ForegroundColor "yellow"

  $IndentLevel++

  Get-QADGroupMember $Identity | ForEach-Object {
    if ($_.Type -eq 'group') {
      Get-GroupMemberTree $_.DN $IndentLevel $IndentCharacter
    } else {
      Write-Host "$($IndentCharacter * $IndentLevel)$($_.Name)"
    }
  }
}

Get-GroupMemberTree "Some Parent Group"

Open in new window

That makes me happier :)

Chris
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Senior IT System EngineerIT ProfessionalAuthor Commented:
Thank you Chris !
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.