In order to report on our current load-balancing of our mailbox servers, regularly I run a report on how many mailboxes are on each mailbox server. Typically, I'd simply run a simple one-liner like the following:
get
-mailboxserver
| %{$_.servername;
(get
-mailbox
-server
$_.servername
-resultsize unlimited
).count
}
This code would take 3 to 5 HOURS to query all 20 Exchange clusters and 80k mailboxes.
In comes .NET directory searchers. I've used these before to return new mailboxes, this code searches based on the Exchange Server Distinguished name field to return a count for that server.
function Get-MailboxServerCount ([string]$DN) {
$homeMTAValue = "CN=Microsoft MTA,"+$dn
$Searcher = New-Object System.DirectoryServices.DirectorySearcher
$Searcher.SearchRoot = New-Object System.DirectoryServices.DirectoryEntry ("LDAP://$(([system.directoryservices.activedirectory.domain]::GetCurrentDomain()).Name)")
$Searcher.Filter = "(&(objectClass=user)(HomeMTA=$homeMTAValue))"
$Searcher.PageSize = 10000
$Searcher.SearchScope = "Subtree"
($Searcher.FindAll()).Count
}
$Count = 1
$mbxServers = Get-MailboxServer
$MbxInfo = @()
foreach ($srvr in $mbxServers) {
Write-Progress -Activity "Gathering info..." -Status "Current Server: $($srvr.Name)" -Id 1 -PercentComplete (($Count / $mbxServers.Count) * 100)
$obj = New-Object -TypeName psobject
$obj | Add-Member -Name "Server" -Value $srvr.Name -MemberType NoteProperty
$obj | Add-Member -Name "Count" -Value (Get-MailboxServerCount -DN $srvr.Distinguishedname) -MemberType NoteProperty
$MbxInfo += $obj
$Count ++
}
#Export to a CSV-file
$MbxInfo | sort Server | Export-Csv -Path c:\bin\csv\MBServerUserCount.csv -NoTypeInformation
This code, I just timed it, took 13 minutes.
Comments
Post new comment