How to send Exchange 2007 mailbox size Report by email

      Comments Off on How to send Exchange 2007 mailbox size Report by email

mailboxsize_liteboxIn the last weeken  i have some time to write a little script to alert me about the Status of the mailbox size on my Microsoft Exchange 2007 SP1 server.

What can do this script:

1. Creating HTML Report  about the Storage of the Servers Storage Status

2. Including the all Mailbox Size on the Servers

3. Converting the size to MB (by default is KB)

4. List the following informations: DisplayName; TotalItemSize(MB); TotalDeletedItemSize(MB); ItemCount; LastLoggedOnUserAccount; LastLogonTime; Database ; StorageLimitStatus

5. Send by Email addig the Report generation Status

Lets see the code

[code lang=”powershell”]
$exchangeservers = Get-ExchangeServer |where-object {$_.admindisplayversion.major -eq 8 -and $_.IsMailboxServer -eq $true }
$AllServers = @()

foreach ($server in $exchangeservers)
{
$db = Get-MailboxDatabase -server $server
foreach ($objItem in $db)
{
$edbfilepath = $objItem.edbfilepath
$path = “\” + $server + “\" + $objItem.EdbFilePath.DriveName.Remove(1).ToString() + "$"+ $objItem.EdbFilePath.PathName.Remove(0,2)
$dbsize = Get-ChildItem $path
$start = $path.LastIndexOf('\')
$dbpath = $path.Substring($start +1).remove($path.Substring($start +1).length -4)
$mailboxpath = "$server\$dbpath"
$mailboxcount = Get-MailboxStatistics -database "$mailboxpath" |measure-object
$ReturnedObj = New-Object PSObject
$ReturnedObj | Add-Member NoteProperty -Name "Server\StorageGroup\Database" -Value $objItem.Identity
$ReturnedObj | Add-Member NoteProperty -Name "Size (MB)" -Value ("{0:n2}" -f ($dbsize.Length/1024KB))
$ReturnedObj | Add-Member NoteProperty -Name "Mailbox Count" -Value $mailboxcount.count
$AllServers += $ReturnedObj
}
$mailbox_size=Get-MailboxStatistics -server $server | Sort-Object TotalItemSize -Descending | Select-Object "DisplayName", @{name='TotalItemSize(MB)';expression={$_.totalitemsize/1MB}}, @{name='TotalDeletedItemSize(MB)';expression={$_.totalDeletedItemSize/1MB}} , "ItemCount", "LastLoggedOnUserAccount", "LastLogonTime" ,"Database", "StorageLimitStatus"
}

$exp_msize= $mailbox_size | ConvertTo-Html -Title "Mailbox Status on Servers" -Body "

Mailbox Status on Servers

" | foreach {$_ -replace "
", " "}
$exp_allsrv = $AllServers | ConvertTo-Html -Title "Server Storage Status" -Body "

Server Storage Status

" -head "

" | foreach {$_ -replace "

", " "}

function sendmail($body, $body1)
{
$today = Get-Date
$SmtpClient = new-object system.net.mail.smtpClient
$mailmessage = New-Object system.net.mail.mailmessage
$SmtpClient.Host = "mail.mydomainname.dom"
$mailmessage.from = "report@mydomainname.dom"
$mailmessage.To.add("veres.levente@mydomainname.dom")
$mailmessage.Subject = “Exchange 2007 Database Sizes”
$mailmessage.IsBodyHtml = $true
$mailmessage.Body = "

Generated on : $today n

” + $body
$mailmessage.Body += “`n” + $body1

$smtpclient.Send($mailmessage)
}

sendmail $exp_allsrv $exp_msize

[/code]

Example
mailboxsize_lite