Exchange powershell script to collect free space from event 1221 and dump to csv if greater and 1GB

#Script will poll all exchange servers and pull out data from event 1221 and place in a .csv file if greater than 1GB

#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 = ServerHostName,’ServerHostName’
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 = $event.Message.IndexOf(“Storage Group”) + 10
$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 > 1GB, add the details to the csv file
if ($result -ge 3072){

$rowline = “$ComputerName,” + “$MBXStoreLocation,” + “$MBLocation,”
Add-Content FreeSpaceGreaterThan1GB.csv $rowline
}
else
{
}
}
}

Advertisements
This entry was posted in Exchange 2007 Powershell commands and scripts. Bookmark the permalink.

One Response to Exchange powershell script to collect free space from event 1221 and dump to csv if greater and 1GB

  1. Mike Abbaticchio says:

    Powershell seems to choke on:
    $ExchServer = ServerHostName,’ServerHostName’

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s