Exchange 2007 Whitespace

I found this webblog

that would find the freespace and white space for an exchange 2007 server. The powershell script as written was giving me minor errors which when finally corrected yeilded a very nice report for exchange administrators.

one of the errors to note :
Exception calling “Add” with “2” argument(s): “Key cannot be null. Parameter name: key”
At C:\Scripts\whitespacescript.ps1:29 char:45
+ $freespace | ForEach-Object {$whiteSpace.Add( < <<< $_.DB,$_.FreeMB)}

When reading the Application log and getting all the 1221 events, you may have more than one 1221 event per storage group. If the specified key already exists in the Hashtable, setting the Item property overwrites the old value. In contrast, the Add method does not modify existing elements. Which is why the exception is generated.

The other problem I encountered, after the values were entered into the hashtable, once it exited the function, the key would still be in the hashtable, but the value associated with key was empty. Once I reorginized the script so there was not function, it ran without problems.

$date = ( get-date ).ToString(‘MM-dd-yyyy’)
$AllServers = @()
foreach ($server in Get-MailboxServer)

# Convert Dates to WMI CIM dates
$tc = [System.Management.ManagementDateTimeconverter]
$Start =$tc::ToDmtfDateTime( (Get-Date).AddDays(-1).Date )
$End =$tc::ToDmtfDateTime( (Get-Date).Date)

#Create a hash Table to hold the freespace information
$hashWS = @{}
#Clear the hash Table just in case
# Create two claculated properties for InsertionStrings values
$DB = @{Name=”DB”;Expression={$_.InsertionStrings[1]}}
$FreeMB = @{Name=”FreeMB”;Expression={[int]$_.InsertionStrings[0]}}

$whitespace = Get-WMIObject Win32_NTLogEvent -ComputerName $server -Filter “LogFile=’Application’ AND EventCode=1221 AND TimeWritten>=’$Start’ AND TimeWritten< =’$End'” | Select-Object $DB,$FreeMB
foreach ($objectItem in $whitespace)

foreach ($objItem in Get-MailboxDatabase -server $server)
$edbfilepath = $objItem.edbfilepath
$path = “`\`\” + $server + “`\” + $objItem.EdbFilePath.DriveName.Remove(1).ToString() + “$”+ $objItem.EdbFilePath.PathName.Remove(0,2)
$dbsize = Get-ChildItem $path
$dbpath = $EdbFilePath.PathName.Remove(0,2).remove($EdbFilePath.PathName.length-6)
$mailboxpath = “$server$dbpath”
$size = get-wmiobject -computername $server win32_logicaldisk |where-object {$_.deviceid -eq $objItem.EdbFilePath.DriveName} |select-object deviceID, Freespace, Size
$freespace = ($size.freespace / 1GB)
$total = ($size.size / 1GB)
$PercentFree = “{0:n2}” -f ($freespace / $total *100)
$mailboxcount = Get-MailboxStatistics -database “$mailboxpath” |Where {$_.DisconnectDate -eq $null -and $_.ObjectClass -eq ‘Mailbox’} |measure-object
$disconnectedmailboxcount = Get-MailboxStatistics -database “$mailboxpath” |Where {$_.DisconnectDate -ne $null} |measure-object

$ReturnedObj = New-Object PSObject
$ReturnedObj | Add-Member -MemberType NoteProperty -Name “Server\StorageGroup\Database” -Value $objItem.Identity

$ReturnedObj | Add-Member -MemberType NoteProperty -Name “Total Size (GB)” -Value (“{0:n2}” -f ($total))
$ReturnedObj | Add-Member -MemberType NoteProperty -Name “Used Space (GB)” -Value (“{0:n2}” -f ($dbsize.Length/1024MB))
$ReturnedObj | Add-Member -MemberType NoteProperty -Name “Free Space (GB)” -Value (“{0:n2}” -f ($freespace))
$ReturnedObj | Add-Member -MemberType NoteProperty -Name “Percent Disk Free” -Value $percentfree
$ReturnedObj | Add-Member -MemberType NoteProperty -Name “User Mailbox Count” -Value $mailboxcount.count

$dbasename = $ +”\” + $
$ReturnedObj | Add-Member -MemberType NoteProperty -Name “White Space (GB)” -Value (“{0:n2}” -f ($hashWS[$dbasename]/1024))

$ReturnedObj | Add-Member -MemberType NoteProperty -Name “Total Free (GB)” -Value (“{0:n2}” -f ($freespace + $hashWS[$dbasename]/1024))
$TotalPercent = ($freespace + $hashWS[$dbasename]/1024) / $total *100
$ReturnedObj | Add-Member -MemberType NoteProperty -Name “Total Percent Free” -Value (“{0:n2}” -f ($TotalPercent))
$AllServers += $ReturnedObj
#$AllServers |Export-Csv E:\MailboxDatabaseSizeReport.csv -NoTypeInformation
$body = “Mailbox Whitespace report for $date ”
$bodydetail = $allservers |sort-object “Server\StorageGroup\Database” |convertto-html
$body = $body + $bodydetail
Send-MailMessage -To “” -Body $body -Subject “Mailbox Database Size Report” -SmtpServer “” -BodyAsHtml:$true -From “”

This entry was posted in Powershell. Bookmark the permalink.

Leave a Reply

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