#Powershell #Exchange2010

I just wanted to share this cmdlet I created today. I had a need to workout from Nested Groups including DDL’s the number of users a DL would reach, so I knocked this baby up.

Its v1, so any feedback is very welcome. Usage is basically

Enumerate-Groups.ps1 –GroupName “Group”

or

Get-Group “Group” | ForEach{.\Enumerate-Groups.ps1 –GroupName $_.DistinguishedName }

Enjoy

PARAM([String]$GroupName=””,[Switch]$ShowUsers=$False, [String]$DomainController=””)

If($GroupName -eq “”){Write-Host “You need to specify a group”;Exit}

##########################################################################################

$AppName = “Enumerate-Groups.ps1”

$AppVer = “v1.0 [19 December2011]”

#v1.0 19 Dec 2011 : A script it born

#

# This script take a groupname as an agrument and then attempts to enumerate the users

# contained in the group by checking all necessary nested groups

#

#Parameters:

#GroupName : Name of the group (top level) that you want to enumerate

#ShowUsers : Displays and exports userlist to CSV

#DomainController : Name of a DC to use

#

#Written By Paul Flaherty

#blogs.flaphead.com

##########################################################################################

#Display script name and version

#########################################################################################

Write-host ” ” $AppName -NoNewLine -foregroundcolor Green

Write-Host “: ” $AppVer -foregroundcolor Green

Write-host “`n Run on $ServerName at $Today by $xUser” -foregroundcolor Yellow

Write-Host “|——————————————————————-|`n”

##########################################################################################

#Load the Exchange 2010 bits & bobs

#########################################################################################

$xPsCheck = Get-PSSnapin | Select Name | Where {$_.Name -Like “*Exchange*”}

If ($xPsCheck -eq $Null) {Add-PsSnapin Microsoft.Exchange.Management.PowerShell.e2010}

Import-Module ActiveDirectory

Function Enumerate-Group($InGroup){

$tmpADo = Get-AdObject $InGroup -Server $script:dc

$tmpADo.ObjectClass

If($tmpADo.ObjectClass -ne “msExchDynamicDistributionList”){

$tmpExGroup = Get-Group $InGroup -resultsize 1 -DomainController $Script:DC

$tmpGroup = Get-ADGroup $tmpExGroup.DistinguishedName -Properties Members -Server $script:dc

Write-Host “-“$tmpGroup.Name”:”$tmpGroup.ObjectClass

Write-host “+- Member Count: ” $tmpGroup.Members.Count

$Members = $tmpGroup.Members | Sort

ForEach($Item in $Members){

$tmpMember = Get-AdObject $Item -Server $script:dc

$tmpName = $tmpMember.Name

If($tmpMember.ObjectClass -ne “user”){

Write-Host “+–” $tmpName”:” $tmpMember.ObjectClass

}

$tmpUsers = “” | Select Name, DDL

If($tmpMember.ObjectClass -eq “user”){

$tmpUsers.Name = $tmpMember.name

$tmpUsers.DDL = $tmpGroup.Name

$Script:Users += $tmpUsers

}#If user

if($tmpMember.ObjectClass -eq “group”){Enumerate-group $tmpMember.Name}

if($tmpMember.ObjectClass -eq “msExchDynamicDistributionList”){

Enumerate-DDL $TmpMember.Name

} #If msExchDynamicDistributionList

} #ForEach

}ELSE{

Write-Host “-“$tmpADo.Name”:”$tmpGroup.ObjectClass

Enumerate-DDL $tmpADo.Name

}#IF

} #Function Enumerate-Group

Function Enumerate-DDL($InDDL){

$tmpDDL = Get-DynamicDistributionGroup $InDDL -DomainController $Script:DC

$mc = Measure-Command {$tmpRecp = Get-Recipient -RecipientPreviewFilter $tmpDDL.RecipientFilter -OrganizationalUnit $tmpDDL.RecipientContainer -ResultSize Unlimited -DomainController $Script:DC}

$tmpCount = 0

ForEach($r in $tmpRecp){$tmpCount ++}

Write-Host “+— User Count: ” -NoNewLine

Write-Host $tmpCount -ForeGroundColor Green -NoNewLine

Write-Host ” in” $mc.TotalSeconds “Seconds`n”

ForEach($item in $tmpRecp){

If ($Item.name -ne “”){

$tmpUsers = “” | Select Name, DDL

$tmpUsers.Name = $Item.name

$tmpUsers.DDL = $tmpDDL.Name

$Script:Users += $tmpUsers

}

}#ForEach

}#Function Enumerate-DDL

$script:DC = $DomainController

$Script:users = @()

Write-Host “Domain Controller:.. $script:dc”

Write-Host “Show Users:……… $ShowUsers”

$g = Get-ADObject $GroupName -Server $script:dc

If($g -eq $Null){Write-host “Problem with the group”;Exit}ELSE{Enumerate-group $GroupName}

$totusers = $script:Users.Count

$tmpusers = $script:users | sort Name -Unique

$totuusers = $tmpusers.count

Write-Host “`nTotal Users: ” -NoNewLine

Write-Host $totusers -Foregroundcolor Green

Write-Host “`nTotal Unique Users: ” -NoNewLine

Write-Host $totuusers -Foregroundcolor Green

If($ShowUsers){

Write-Host “`n`nUser List” -foregroundcolor blue

Write-Host “- Exporting to GroupUserList.csv”

$Script:Users | Export-CSV GroupUserList.csv -NoTypeInformation -Delimiter “|”

$Script:Users | sort DDL, Name

}

#End

http://FLAPHEAD.DNS2GO.COM/?p=62

Sent with MobileRSS for iPhone