Thursday, July 5, 2012

Exchange: Mailbox and Database Storage Statistics PowerShell Export

Bear in mind this is version 1 so there aren't any variables to pass into yet. I'd like to get this a little more modular so you would adjust the command line instead of digging through the script for things like the mail server and such. Still, it does what I need it to. I'll highlight what you would need to update for your environment.

One tip for something I've done in the past. If you have SharePoint you can mail enable a library and email these to it. Then you capture the change in space over time so you know how your storage is changing. Plus it gets this valuable data out of your inbox.

This is particularly helpful when trying to right size your DBs and manage your various drives (typically it's best to put each DB on it's own drive letter).


#Gets mailbox statistics from specified mailbox server. Returns User, Size, Item Count, and which database the mailbox is in.


$MailboxStatsFileName="MailboxStats-" + $(get-date).tostring("MMddyyyy") + ".csv"
$MailboxStatsDeleteFileName="MailboxStats-" + $(get-date).adddays(-35).tostring("MMddyyyy") + ".csv"

Write-Output "Creating mailbox stats: $($MailboxStatsFileName)"
Get-MailboxStatistics -server ServerName | Select-Object DisplayName,@{label=”User”;expression={$_.LastLoggedOnUserAccount}},@{label=”Total Size(MB)”;expression={$_.TotalItemSize.Value.ToMB()}},ItemCount,Database | Export-CSV "C:\Reports\$($MailboxStatsFileName)"

#Pause to allow time for export
Start-Sleep 20

function Get-DatabaseStatistics {
    $Databases = Get-MailboxDatabase -Status
    foreach($Database in $Databases) {
        $DBSize = $Database.DatabaseSize
        $MBCount = @(Get-MailboxStatistics -Database $Database.Name).Count
$MBAvg = Get-MailboxStatistics -Database $Database.Name |
          %{$_.TotalItemSize.value.ToMb()} |
            Measure-Object -Average           
New-Object PSObject -Property @{
            Server = $Database.Server.Name
            DatabaseName = $Database.Name
            LastFullBackup = $Database.LastFullBackup
            MailboxCount = $MBCount
            "DatabaseSize (GB)" = $DBSize.ToGB()
            "AverageMailboxSize (MB)" = $MBAvg.Average
            "WhiteSpace (MB)" = $Database.AvailableNewMailboxSpace.ToMb()
        }
    }
}


#Runs function to gather server/database stats including whitespace
$DBFileName="DBStats-" + $(get-date).tostring("MMddyyyy") + ".csv"
$DBDeleteFileName="DBStats-" + $(get-date).adddays(-35).tostring("MMddyyyy") + ".csv"

Write-Output "Creating DB stats: $($DBFileName)"
Get-DatabaseStatistics | Export-Csv "C:\Reports\$($DBFileName)" -Force -NoType

#Pause to allow time for export
Start-Sleep 15

Write-Output "Emailing: $($MailboxStatsFileName) and $($DBFileName) to sys admins"
#Send email to admins with report info for mailboxes and databases.
Send-MailMessage -To email@company.com -From Exchange@Company.com -Subject "Email Statistics for $((get-date).ToShortDateString())" -SmtpServer mail.company.com -Attachments "C:\Reports\$($MailboxStatsFileName)", "C:\Reports\$($DBFileName)"


if (test-path -Path "C:\Reports\$($MailboxStatsDeleteFileName)")
{
  Write-Output "Deleting Old Mailbox Stats"
  Remove-Item -Path "C:\Reports\$($MailboxStatsDeleteFileName)" -ErrorAction SilentlyContinue
}

if (test-path -Path "C:\Reports\$($DBDeleteFileName)")
{
  Write-Output "Deleting Old DB Stats"
  Remove-Item -Path "C:\Reports\$($DBDeleteFileName)" -ErrorAction SilentlyContinue
}

Exchange: Mailbox Database Whitespace


Get-MailboxDatabase -status | select-object name,availablenewmailboxspace

I may have to look at combining this with another post for exporting mailbox and DB storage stats. Either as another value on that dump or another csv to email out.

Exchange: Bulk Mailbox Database Migrations


AUTO SUSPEND

$targetDB="DATABASENAME"

$users = Import-Csv C:\Mailbox\Move.csv
foreach ($user in $users)
{
Write-Output "Processing User: $($user.UserName) at $(get-date)"
Get-Mailbox -Identity $user.UserName| New-MoveRequest -BatchName 'MyBatchName' -SuspendWhenReadyToComplete -TargetDatab5ase "$($targetDB)" -BadItemLimit 5
Write-Output "Finished Processing User: $($user.UserName) at $(get-date)"
}


NO SUSPEND
$targetDB="DATABASENAME"


$users = Import-Csv C:\Mailbox\Move.csv
foreach ($user in $users)
{
Write-Output "Processing User: $($user.UserName) at $(get-date)"
Get-Mailbox -Identity $user.UserName | New-MoveRequest -BatchName 'MyBatchName' -TargetDatabase "$($targetDB)" -BadItemLimit 5
Write-Output "Finished Processing User: $($user.UserName) at $(get-date)"
}


Some other helpful one liners to go with the previous scripts.

Get-MoveRequest -MoveStatus 'InProgress' -BatchName 'MyBatchName'
Get-MoveRequest -MoveStatus 'AutoSuspended' -BatchName 'MyBatchName'
Get-MoveRequest -MoveStatus 'CompletionInProgress' -BatchName 'MyBatchName'


Get-MoveRequest -MoveStatus 'AutoSuspended' -BatchName 'MyBatchName' | Resume-MoveRequest

Exchange: Message from an Address for Time Period

Just a quick one liner

Get-MessageTrackingLog -ResultSize unlimited –Sender “address@company.com” -Start "6/21/2012 12:00AM" -End "6/22/2012 7:50AM" | Export-CSV C:\commadelimitedoutput.csv