Send Mailbox Usage Figures by Email in Exchange 2007

I need to monitor the mailbox sizes of several group mailboxes in my Exchange 2007 environment, here’s a script that automates the process of sending out the mailbox sizes. Disk space is an issue on this server so the script also shows which mailboxes are taking up space because of the deleted items retention policy.

C:\WINDOWS\system32\WINDOW~2\v1.0\powershell.exe -PSConsoleFile “C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1” -Command “C:\SendStats\sendstats.ps1”

Contents of sendstats.ps1:

###Send mailbox statistics script

###First, the administrator must change the mail message values in this section
$FromAddress = “rfrood@trafficpenaltytribunal.gov.uk”
$ToAddress = “rfrood@trafficpenaltytribunal.gov.uk”
$MessageSubject = “Mailbox Size Report”
$MessageBody = “Attached is the current list of mailbox sizes and the retention dumpster sizes.”
$SendingServer = “mcc-exch01.parking-adjudication.gov.uk”

###Now get the mailbox size stats and store in a text file
Get-MailboxStatistics | Sort-Object TotalItemSize -Descending | ft DisplayName,@{label=”TotalItemSize(MB)”;expression={$_.TotalItemSize.Value.ToMB()}},ItemCount > mailbox_sizes.txt

###Get the Retained Item Sizes (Dumpster) and store in a 2nd text file
Get-MailboxStatistics | Sort-Object TotalDeletedItemSize -Descending | ft DisplayName,DeletedItemCount,@{label=”TotalDeletedItemSize(MB)”;expression={$_.TotalDeletedItemSize.Value.ToMB()}} > mailbox_retention_size.txt

###Create the mail message and add the statistics text file as an attachment
$SMTPMessage = New-Object System.Net.Mail.MailMessage $FromAddress, $ToAddress,
$MessageSubject, $MessageBody
$Attachment1 = New-Object Net.Mail.Attachment(“c:\sendstats\mailbox_sizes.txt”)
$Attachment2 = New-Object Net.Mail.Attachment(“c:\sendstats\mailbox_retention_size.txt”)
$SMTPMessage.Attachments.Add($Attachment1)
$SMTPMessage.Attachments.Add($Attachment2)

###Send the message
$SMTPClient = New-Object System.Net.Mail.SMTPClient $SendingServer
$SMTPClient.Send($SMTPMessage)