Simple fun with the Exchange Management Shell – Part 2 – Databases and Mailboxes Stats…

Ok, in the last part I had a little fun messing around with getting mailbox information based upon the location of the AD account within an OU.

The examples given were simple (but then again that is the point) – but they are designed to show that with just a little effort with the Exchange Management Shell you can accomplish quite a bit – which in Exchange 2003 and lower versions, you had to make use of independent API’s (CDO, WEBDAV et al) and scripting languages such as VBSCRIPT.

In this part I would like to do a little experimentation with some small furry animals, erm no sorry that is my weekend work, with Mailbox Database and Mailbox Statistics gathering. Like the last part – there will be no great detail to the scripts (well initially – this part will build to a larger script which might be considered for production use) – they are for the most part designed to be “primers” to give people an idea of what they can do should they wish.

Ok so lets start of simply – lets get all of our Exchange Databases:

Get-MailboxDatabase

This will produce a list of all the databases on all the Exchange Servers within your immediate environment:

image

Now, using some the concepts that we learned in the first part by filtering the database results by a specific server within the environment:

Get-MailboxDatabase | where {$_.Server -eq 'LAB-EXCCREV-01'}

As your can see by adding in a “where” clause to filter results based upon the “server” attribute you can return the databases which are on a specific server.

image

Now lets get a little more ambitious and change the code so that you can choose the mailbox servers where you would like to list the databases from:

$Servers = Get-ExchangeServer | where {$_.IsMailboxServer -eq 'True'}
$Count = 0
Foreach ($svr in $Servers){

    Write-Host $Count "." $svr.Name
    $Count ++

}
$Choice = Read-Host "Please choose a server to list databases from"

Get-MailboxDatabase | where {$_.Server -eq $Servers[$Choice].Name}

What we have done in the script above is get all of the Exchange Servers in the Organisation which have the mailbox server role installed upon them into a variable called $Servers. From there we list all of the servers using a foreach loop to get their names on the the screen – and we then finish off by allowing the user to select which server to query – the output from this script looks like the following:

MBDBXFun001

Now lets get a little more adventurous and modify the script to allow for all mailboxes which are in the databases on the selected server:

$Servers = Get-ExchangeServer | where {$_.IsMailboxServer -eq 'True'}
$Count = 0
Foreach ($svr in $Servers){

    Write-Host $Count "." $svr.Name
    $Count ++

}
$Choice = Read-Host "Please choose a server to list databases from"

$DBS = Get-MailboxDatabase | where {$_.Server -eq $Servers[$Choice].Name}
Write-Host ""

foreach($db in $DBS){
    Write-Host $DB.Name
}

Write-Host ""

$MBXChoice = Read-Host "Would you like to list all of the mailboxes on this Server?"

If($MBXChoice.ToLower() -eq "y"){
    $xCount = 0
    foreach ($dbsx in $DBS){
        Get-Mailbox | Select name,serverName,Database | where {$_.Database -eq $DBS[$xCount].Identity}
        $xCount ++ 
    }

}

What we have done to allow for this is to use the Get-Mailbox command only selecting the name of the mailbox, serverName and Database which is resides, we then pass the name of the Databases on the selected server to the Get-Mailbox command – the output of which looks like the following:

MBDBXFun002

Now that we have a script that can list of all of Databases on a given server, and in turn list of the mailboxes within each database – lets modify it again, this time to get some Mailbox Statistics, in the example below – I will be getting the DisplayName and the Total Item Size to MB from the selected server databases into a text file:

$Servers = Get-ExchangeServer | where {$_.IsMailboxServer -eq 'True'}
$Count = 0
Foreach ($svr in $Servers){

    Write-Host $Count "." $svr.Name
    $Count ++

}
$Choice = Read-Host "Please choose a server to list databases from"

$DBS = Get-MailboxDatabase | where {$_.Server -eq $Servers[$Choice].Name}
Write-Host ""

Foreach($db in $DBS){
    Write-Host $DB.Name
}

Write-Host ""

$MBXChoice = Read-Host "Would you like to list all of the mailboxes on this Server?"

If($MBXChoice.ToLower() -eq "y"){
    $xCount = 0
    foreach ($dbsx in $DBS){
        Get-Mailbox | Select name,serverName,Database | where {$_.Database -eq $DBS[$xCount].Identity}
        $xCount ++ 
    }

}
Write-Host ""
$sChoice = Read-Host "Would you like to list some mailbox stats from the chosen servers Databases?"
Write-Host ""

If($sChoice.ToLower() -eq "y"){
    $fname = "c:\mbx.txt"
    
    foreach ($dbsxs in $DBS){
        $Mbx = Get-Mailbox | where {$_.Database -eq $dbsxs.Identity}
        
        foreach($mbox in $MBX){
            
            Get-MailboxStatistics $mbox.samAccountName | ft @{label="Display Name";expression={$_.DisplayName}},@{label="Total Item Size MB";expression={$_.TotalItemSize.value.tomb()}} | Out-File $fname -Append
        }
        
    }

}
Invoke-Item $fname

Ok, but how can we turn the above concepts into a meaningful script?

The following is a script which is (admittedly) a little more complex than the one that we have constructed above – I wrote for a customer a little while ago.
In essence it dumps relevant Database information into a HTML file (the HTML construction code I borrowed from an excellent article written by Thiyagu here) but it makes use of a lot of the concepts that we have covered above – and again demonstrates what can be accomplished with Powershell.

The script itself should be executed on the local Exchange server – or in the case of a clustered instance the Active Node – the script contains some logic (at the beginning) which determines the difference between clustered and non-clustered instances.

This script is available for download (due to its size) from the following link:

ScriptDBandMBXStatsToHTML.ps1

The HTML output from the script looks like the following:

MBDBXFun003

This is where I would like to close this part, we have covered quite a few concepts within this article, of which I hope that you will find most useful

Sharing is caring!:

One thought to “Simple fun with the Exchange Management Shell – Part 2 – Databases and Mailboxes Stats…”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.