Using Powershell to Find Free Space in Exchange 2003 databases
One regular task for Exchange admins can be reclaiming free space within Exchange mailbox stores either after a large amount of data has been removed or just a significant amount has built up over time for various reasons. For those of you who don’t know, to reclaim the space the mailbox store has to be taken offline and the database defragged to get the space back on the disk.
Finding good candidates for defragging especially in a large environment with multiple databases and Exchange servers can be a pretty tedious task. Exchange records an entry in the Application Event Log (Event ID 1221) after online maintenance has taken place which tells you how much free space is in the database - this means trawling through the event logs on each server and recording which databases you think are worth defragging. So naturally I wrote a Powershell script to save wasting time on this task!
Using WMI the script queries the Application log on each of the Exchange servers you choose, looking for 1221 events in the last day. It sucks out the name of the mailbox store and the amount of free space in the database from the message field of the event and if the free space is greater than a particular figure (in the example below 3GB) adds the info to a csv file.
You could obviously change the figure to meet your needs and also if you remove the if statement you could get it to report on every database so you have a report of free space across all databases.
Tip: If you have clustered mailbox servers then you only need to point the script at one of the servers in the cluster since it will contain all of the event log entries for each server in the cluster.
#Check to see if csv exists and if so remove it If (Test-Path “FreeSpaceGreaterThan1GB.csv”) { Remove-Item “FreeSpaceGreaterThan1GB.csv” } Else { }
#Set the columns for the csv file $rows = “Servername,” + “Mailbox Store,” + “Free Space (MB),” Add-Content FreeSpaceGreaterThan1GB.csv $rows
$ExchServer = ‘server1’,‘server2’ foreach ($Server in $ExchServer){
#Get the time 1 day ago in the right format for WMI query $WmidtQueryDT = [System.Management.ManagementDateTimeConverter]::ToDmtfDateTime([DateTime]::Now.AddDays(-1)) #Perform WMI query of Event 1221 in Application log in the last day $1221 = Get-WmiObject -computer $ExchServer -query (“Select * from Win32_NTLogEvent Where Logfile=‘Application’ and Eventcode = ‘1221’ and TimeWritten >=’” + $WmidtQueryDT + “’”)
foreach ($event in $1221){
#Get the name of the Mailbox Store $MBXStoreLocationStart =< span style=“color:rgb(0,0,0);"> $event.Message.IndexOf(“Storage Group”) + 16 $MBXStoreLocationFinish = $event.Message.IndexOf(“has”) - 2 $MBXStoreLocation = $event.Message.SubString($MBXStoreLocationStart, $MBXStoreLocationFinish - $MBXStoreLocationStart)
#Get the free space figure and convert it to an integer $MBLocationStart = $event.Message.IndexOf(“has”) + 4 $MBLocationFinish = $event.Message.IndexOf(“megabytes”) - 1 $MBLocation = $event.Message.SubString($MBLocationStart, $MBLocationFinish - $MBLocationStart) $result = [int]$MBLocation
$ComputerName = $event.ComputerName
#If free space > 3GB, add the details to the csv file if ($result -ge 3072){
$rowline = “$ComputerName,” + “$MBXStoreLocation,” + “$MBLocation,” Add-Content FreeSpaceGreaterThan1GB.csv $rowline } else { }
} }