Get Number of Mailboxes Per Exchange server

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

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <blockquote> <center> <hr> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <drupal6>, <html>, <java>, <javascript>, <php>, <posh>.

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.