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
{
}
}
}

Users feedback ( 2 )

  1. Mike Abbaticchio said:

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

  2. Pedro said:

    if ($result -ge 3072) ???
    is it more than 1 gb? It will not be 1024 ?

    Thanks

Any thoughts?