Saturday, May 31, 2008

Version 5 of the Mailbox Size Gui Script for Exchange 2007

***** If your running Exchange 2010 please see http://gsexdev.blogspot.com/2010/03/mailbox-size-gui-exchange-2010-remote.html ********

Okay time for a new version of the mailbox size gui the last version had a number of large bugs and the quota code looked like it had been written by a drunk squirrel. So this version firstly fixes the bugs in the quota code and the bugs with Deleted items sizes and adds some new functionality.

The first addition is a drop down box that allows selection of which quota value you want to report on Eg exchange offers 3 quota levels Warning, Prohibit Send and Prohibit Send and Receive so the script will now allow reporting on which ever Quota usage you want to look at. This section still needs a little work to get better performance as it re-queries the server for mailbox sizes when you change the quota dropdown box instead of just reusing the current result.

The second and most exciting feature is the mailbox size growth feature this is the biggest change and requires some explanation.

Firstly when the script runs now it will create a folder on the c drive called mbsizehistory, it will then record the results of each mailbox size query you make to a csv file in this directory with a serial date as the file name along with the servername . It will only create one file per day and per server you run it against.

So basically every time you run this script it will create this file so if you run it once a week or one a day or every three days you will start collecting adhoc history data about your mailbox size growth. Convention wisdom and what I’ve done in the past is record this type of data to a database every day and then build table, view and graph report on this data. While this is possible with the script the amount of complexity of setup and other backend you need starts getting beyond the technology that might be generally available for a simple script like this. But this where the cool and exciting part comes in to cater for this and allow aggregation of random history files using a few simple date algorithms, hash table and loops I’ve come up with a serial date, random file aggregation datatable. This part is highly experimental as it was only written last week so I haven’t tested it with real data only mock data and haven’t tested it with multiple servers so going on past experience the squirrel may make another appearance. But with the limited testing I’ve done it does work and I think its pretty cool and useful. Basically it aggregates the previous result from the files and tries to show the mailbox growth over 1 day,7days,1 month and 1 year. Because the dates of the file may be intermediate the script handles getting the file that is as closest to the growth date's as possible.

I’ve put a downloadable of the new version here the growth section looks like

function ShowGrowth(){


$gtTable.clear()
$datetime = get-date
$arArrayList = New-Object System.Collections.ArrayList
dir c:\mbsizehistory\*.csv | foreach-object{
$fname = $_.name
$nmArray = $_.name.split("-")
if ($nmArray[1].Replace(".csv","") -eq $snServerNameDrop.SelectedItem.ToString()) {
[VOID]$arArrayList.Add($nmArray[0])
}
}
$arArrayList.Sort()
$spoint = $arArrayList[$arArrayList.Count-1]
$oneday = $spoint
$sevenday = $spoint
$onemonth = $spoint
$oneyear = $spoint
foreach ($file in $arArrayList){
if ($file -gt ($datetime.Adddays(-2).ToString("yyyyMMdd")) -band $file -lt $oneday) {$oneday = $file}
if ($file -gt ($datetime.Adddays(-7).ToString("yyyyMMdd")) -band $file -lt $sevenday) {$sevenday = $file}
if ($file -gt ($datetime.Adddays(-31).ToString("yyyyMMdd")) -band $file -lt $onemonth) {$onemonth = $file}
if ($file -gt ($datetime.Adddays(-256).ToString("yyyyMMdd")) -band $file -lt $oneyear) {$oneyear = $file}
}
write-host $oneday
write-host $sevenday
write-host $onemonth
write-host $oneyear

$onedaystats = @{ }
$sevendaystats = @{ }
$onemonthsdaystats = @{ }
$oneyearstats = @{ }

Import-Csv ("c:\mbsizehistory\" + $oneday + "-" + $snServerNameDrop.SelectedItem.ToString() + ".csv") | %{
$onedaystats.add($_.DisplayName,$_.TotalItemSize)
}
Import-Csv ("c:\mbsizehistory\" + $sevenday + "-" + $snServerNameDrop.SelectedItem.ToString() + ".csv") | %{
$sevendaystats.add($_.DisplayName,$_.TotalItemSize)
}
Import-Csv ("c:\mbsizehistory\" + $onemonth + "-" + $snServerNameDrop.SelectedItem.ToString() + ".csv") | %{
$onemonthsdaystats.add($_.DisplayName,$_.TotalItemSize)
}
Import-Csv ("c:\mbsizehistory\" + $oneyear + "-" + $snServerNameDrop.SelectedItem.ToString() + ".csv") | %{
$oneyearstats.add($_.DisplayName,$_.TotalItemSize)
}

foreach($row in $msTable.Rows){
if ($onedaystats.ContainsKey($row[0].ToString())){
$ondaysizegrowth = $row[2] - $onedaystats[$row[0].ToString()]
}
else{$ondaysizegrowth = 0}
if ($sevendaystats.ContainsKey($row[0].ToString())){
$sevendaysizegrowth = $row[2] - $sevendaystats[$row[0].ToString()]}
else{$sevendaysizegrowth = 0}
if ($onemonthsdaystats.ContainsKey($row[0].ToString())){
$onemonthsizegrowth = $row[2] - $onemonthsdaystats[$row[0].ToString()]}
else{$onemonthsizegrowth = 0}
if ($oneyearstats.ContainsKey($row[0].ToString())){
$oneyearsizegrowth = $row[2] - $oneyearstats[$row[0].ToString()]}
else{$oneyearsizegrowth = 0}
$gtTable.rows.add($row[0].ToString(),$row[2],$ondaysizegrowth,$sevendaysizegrowth,$onemonthsizegrowth,$oneyearsizegrowth)

}
$dgDataGrid.DataSource = $gtTable

}