This script could be used to create report for average mailbox size and item count per database in Microsoft Exchange 2010. Also you can use this script to measure all the properties which can be measured.
In a real world, IT Administrators may want to know the detail information about Mailbox Database, some of them even require the average mailbox size of each database, and they need these data to manage the exchange server better.
This script contains one advanced function, Get-OSCEXMailboxDBReport. You can use this script in following ways:
Method 1:
Method 2:
Function Get-OSCEXMailboxDBReport
{
[CmdletBinding()]
Param
(
#Define parameters
[Parameter(Mandatory=$true,Position=1,ValueFromPipeline=$true)]
[string]$Database,
[Parameter(Mandatory=$false,Position=2)]
[string[]]$DataBaseProperty,
[Parameter(Mandatory=$true,Position=3)]
[string[]]$MailboxStatisticsProperty,
[Parameter(Mandatory=$false,Position=4)]
[switch]$IncludeDisconnectedMailbox
)
Process
{
$reports = @()
$mailboxDBProperties = @{}
#Get Mailbox Database
$mailboxDB = Get-MailboxDatabase -Identity $Database -Status -Verbose:$false
#If specific database could be found, keep on processing
if ($mailboxDB -ne $null) {
#Save the property names to a variable, it will be used to validate the property names which are provided by administrators
$mailboxDB | Get-Member -MemberType Property | %{$mailboxDBProperties.Add($_.Name,"")}
#Prepare the report
$report = New-Object PSObject
$report | Add-Member -MemberType NoteProperty -Name "ServerName" -Value $($mailboxDB.ServerName)
$report | Add-Member -MemberType NoteProperty -Name "DatabaseName" -Value $($mailboxDB.Name)
$report | Add-Member -MemberType NoteProperty -Name "DatabaseSize" -Value $($mailboxDB.DatabaseSize)
$report | Add-Member -MemberType NoteProperty -Name "AvailableNewMailboxSpace" -Value $($mailboxDB.AvailableNewMailboxSpace)
#Handle additional properties
if ($DataBaseProperty -ne $null) {
foreach ($dbProperty in $DataBaseProperty) {
if ($mailboxDBProperties.ContainsKey($dbProperty)) {
$report | Add-Member -MemberType NoteProperty -Name $dbProperty -Value $($mailboxDB.$dbProperty)
} else {
$warningMsg = $Messages.PropertyNameIsNotValid
$warningMsg = $warningMsg -replace "Placeholder01",$dbProperty
$pscmdlet.WriteWarning($warningMsg)
}
}
}
#Get mailbox statistics
$mailboxDBStatProperties = @{}
if ($IncludeDisconnectedMailbox) {
$mailboxStat = Get-MailboxStatistics -Database $Database -Verbose:$false
} else {
$mailboxStat = Get-MailboxStatistics -Database $Database -Verbose:$false | Where-Object {$_.DisconnectDate -ne $null}
}
if ($mailboxStat -is [array]) {
$mailboxStat | Get-Member -MemberType Property | %{$mailboxDBStatProperties.Add($_.Name,"")}
$report | Add-Member -MemberType NoteProperty -Name "Mailbox(Count)" -Value $($mailboxStat.Count)
foreach ($mbxStatProperty in $MailboxStatisticsProperty) {
#Use regular expression to capture measure method
$measureMethod = [regex]::Match($mbxStatProperty,"(?<=\()\w+")
$measuredProperty = [regex]::Replace($mbxStatProperty,"\($measureMethod\)","")
if (($mailboxDBStatProperties.ContainsKey($measuredProperty)) -and ($measuredProperty -match "Count|Size") -and ($measuredProperty -notmatch "LastLoggedOnUserAccount")) {
Switch ($measureMethod) {
"Average" {
if ($measuredProperty -match "Size") {
$measuredValue = $mailboxStat | Select *,@{Name="$measuredProperty";Expression={$_.$measuredProperty.Value.ToBytes()}} -ExcludeProperty $measuredProperty | Measure-Object -Property $measuredProperty -Average
} else {
$measuredValue = $mailboxStat | Measure-Object -Property $measuredProperty -Average
}
$report | Add-Member -MemberType NoteProperty -Name "$measuredProperty(Average)" -Value $measuredValue.Average
}
"Sum" {
if ($measuredProperty -match "Size") {
$measuredValue = $mailboxStat | Select *,@{Name="$measuredProperty";Expression={$_.$measuredProperty.Value.ToBytes()}} -ExcludeProperty $measuredProperty | Measure-Object -Property $measuredProperty -Sum
} else {
$measuredValue = $mailboxStat | Measure-Object -Property $measuredProperty -Sum
}
$report | Add-Member -MemberType NoteProperty -Name "$measuredProperty(Sum)" -Value $measuredValue.Sum
}
"Maximum" {
if ($measuredProperty -match "Size") {
$measuredValue = $mailboxStat | Select *,@{Name="$measuredProperty";Expression={$_.$measuredProperty.Value.ToBytes()}} -ExcludeProperty $measuredProperty | Measure-Object -Property $measuredProperty -Maximum
} else {
$measuredValue = $mailboxStat | Measure-Object -Property $measuredProperty -Maximum
}
$report | Add-Member -MemberType NoteProperty -Name "$measuredProperty(Maximum)" -Value $measuredValue.Maximum
}
"Minimum" {
if ($measuredProperty -match "Size") {
$measuredValue = $mailboxStat | Select *,@{Name="$measuredProperty";Expression={$_.$measuredProperty.Value.ToBytes()}} -ExcludeProperty $measuredProperty | Measure-Object -Property $measuredProperty -Minimum
} else {
$measuredValue = $mailboxStat | Measure-Object -Property $measuredProperty -Minimum
}
$report | Add-Member -MemberType NoteProperty -Name "$measuredProperty(Minimum)" -Value $measuredValue.Minimum
}
Default {
$warningMsg = $Messages.MeasureMethodIsNotValid
$warningMsg = $warningMsg -replace "Placeholder01",$measureMethod
$pscmdlet.WriteWarning($warningMsg)
}
}
} else {
$warningMsg = $Messages.PropertyNameIsNotValidForMeasuring
$warningMsg = $warningMsg -replace "Placeholder01",$measuredProperty
$pscmdlet.WriteWarning($warningMsg)
}
$measuredValue = $null
}
} else {
#If the mailbox database does not contain any user mailboxes,SystemMailbox will be excluded from the result.
$warningMsg = $Messages.CannotFindAnyMailboxStat
$warningMsg = $warningMsg -replace "Placeholder01",$Database
$pscmdlet.WriteWarning($warningMsg)
}
}
return $report
}
}
Function Get-OSCEXMailboxDBReport { [CmdletBinding()] Param ( #Define parameters [Parameter(Mandatory=$true,Position=1,ValueFromPipeline=$true)] [string]$Database, [Parameter(Mandatory=$false,Position=2)] [string[]]$DataBaseProperty, [Parameter(Mandatory=$true,Position=3)] [string[]]$MailboxStatisticsProperty, [Parameter(Mandatory=$false,Position=4)] [switch]$IncludeDisconnectedMailbox ) Process { $reports = @() $mailboxDBProperties = @{} #Get Mailbox Database $mailboxDB = Get-MailboxDatabase -Identity $Database -Status -Verbose:$false #If specific database could be found, keep on processing if ($mailboxDB -ne $null) { #Save the property names to a variable, it will be used to validate the property names which are provided by administrators $mailboxDB | Get-Member -MemberType Property | %{$mailboxDBProperties.Add($_.Name,"")} #Prepare the report $report = New-Object PSObject $report | Add-Member -MemberType NoteProperty -Name "ServerName" -Value $($mailboxDB.ServerName) $report | Add-Member -MemberType NoteProperty -Name "DatabaseName" -Value $($mailboxDB.Name) $report | Add-Member -MemberType NoteProperty -Name "DatabaseSize" -Value $($mailboxDB.DatabaseSize) $report | Add-Member -MemberType NoteProperty -Name "AvailableNewMailboxSpace" -Value $($mailboxDB.AvailableNewMailboxSpace) #Handle additional properties if ($DataBaseProperty -ne $null) { foreach ($dbProperty in $DataBaseProperty) { if ($mailboxDBProperties.ContainsKey($dbProperty)) { $report | Add-Member -MemberType NoteProperty -Name $dbProperty -Value $($mailboxDB.$dbProperty) } else { $warningMsg = $Messages.PropertyNameIsNotValid $warningMsg = $warningMsg -replace "Placeholder01",$dbProperty $pscmdlet.WriteWarning($warningMsg) } } } #Get mailbox statistics $mailboxDBStatProperties = @{} if ($IncludeDisconnectedMailbox) { $mailboxStat = Get-MailboxStatistics -Database $Database -Verbose:$false } else { $mailboxStat = Get-MailboxStatistics -Database $Database -Verbose:$false | Where-Object {$_.DisconnectDate -ne $null} } if ($mailboxStat -is [array]) { $mailboxStat | Get-Member -MemberType Property | %{$mailboxDBStatProperties.Add($_.Name,"")} $report | Add-Member -MemberType NoteProperty -Name "Mailbox(Count)" -Value $($mailboxStat.Count) foreach ($mbxStatProperty in $MailboxStatisticsProperty) { #Use regular expression to capture measure method $measureMethod = [regex]::Match($mbxStatProperty,"(?<=\()\w+") $measuredProperty = [regex]::Replace($mbxStatProperty,"\($measureMethod\)","") if (($mailboxDBStatProperties.ContainsKey($measuredProperty)) -and ($measuredProperty -match "Count|Size") -and ($measuredProperty -notmatch "LastLoggedOnUserAccount")) { Switch ($measureMethod) { "Average" { if ($measuredProperty -match "Size") { $measuredValue = $mailboxStat | Select *,@{Name="$measuredProperty";Expression={$_.$measuredProperty.Value.ToBytes()}} -ExcludeProperty $measuredProperty | Measure-Object -Property $measuredProperty -Average } else { $measuredValue = $mailboxStat | Measure-Object -Property $measuredProperty -Average } $report | Add-Member -MemberType NoteProperty -Name "$measuredProperty(Average)" -Value $measuredValue.Average } "Sum" { if ($measuredProperty -match "Size") { $measuredValue = $mailboxStat | Select *,@{Name="$measuredProperty";Expression={$_.$measuredProperty.Value.ToBytes()}} -ExcludeProperty $measuredProperty | Measure-Object -Property $measuredProperty -Sum } else { $measuredValue = $mailboxStat | Measure-Object -Property $measuredProperty -Sum } $report | Add-Member -MemberType NoteProperty -Name "$measuredProperty(Sum)" -Value $measuredValue.Sum } "Maximum" { if ($measuredProperty -match "Size") { $measuredValue = $mailboxStat | Select *,@{Name="$measuredProperty";Expression={$_.$measuredProperty.Value.ToBytes()}} -ExcludeProperty $measuredProperty | Measure-Object -Property $measuredProperty -Maximum } else { $measuredValue = $mailboxStat | Measure-Object -Property $measuredProperty -Maximum } $report | Add-Member -MemberType NoteProperty -Name "$measuredProperty(Maximum)" -Value $measuredValue.Maximum } "Minimum" { if ($measuredProperty -match "Size") { $measuredValue = $mailboxStat | Select *,@{Name="$measuredProperty";Expression={$_.$measuredProperty.Value.ToBytes()}} -ExcludeProperty $measuredProperty | Measure-Object -Property $measuredProperty -Minimum } else { $measuredValue = $mailboxStat | Measure-Object -Property $measuredProperty -Minimum } $report | Add-Member -MemberType NoteProperty -Name "$measuredProperty(Minimum)" -Value $measuredValue.Minimum } Default { $warningMsg = $Messages.MeasureMethodIsNotValid $warningMsg = $warningMsg -replace "Placeholder01",$measureMethod $pscmdlet.WriteWarning($warningMsg) } } } else { $warningMsg = $Messages.PropertyNameIsNotValidForMeasuring $warningMsg = $warningMsg -replace "Placeholder01",$measuredProperty $pscmdlet.WriteWarning($warningMsg) } $measuredValue = $null } } else { #If the mailbox database does not contain any user mailboxes,SystemMailbox will be excluded from the result. $warningMsg = $Messages.CannotFindAnyMailboxStat $warningMsg = $warningMsg -replace "Placeholder01",$Database $pscmdlet.WriteWarning($warningMsg) } } return $report } }
Example 01: Displays help about Get-OSCEXMailboxDBReport
Command: Get-Help Get-OSCEXMailboxDBReport -Full
Screenshot:
Example 02: Calculate average value and sum value of TotalItemSize property for specific mailbox database.
Command:
Get-OSCEXMailboxDBReport -Database "MailboxDB" -MailboxStatisticsProperty "TotalItemSize(Average)","TotalItemSize(Sum)"
Screenshot:
Note: The values of these properties should be supplied in the following form "Property(MeasureMethod)". For example, "TotalItemSize(Average)","ItemCount(Sum)". Valid measure methods are Average, Sum, Maximum and Minimum.
Example 03: Calculate average value and sum value of TotalItemSize property for specific mailbox database. (Include Disconnected Mailbox)
Command:
Get-OSCEXMailboxDBReport -Database "MailboxDB" -MailboxStatisticsProperty "TotalItemSize(Average)","TotalItemSize(Sum)" -IncludeDisconnectedMailbox
Screenshot:
Note: Mailbox(Count) can reflect the number of disconnected mailboxes.
Example 04: Calculate average value and sum value of TotalItemSize property for specific mailbox database. And use "Calculated Property" to convert TotalItemSize from bytes to megabytes.
Command:
Get-OSCEXMailboxDBReport -Database "MailboxDB" -MailboxStatisticsProperty "ItemCount(Average)","TotalItemSize(Sum)" | Select *,@{Name="TotalItemSizeInMB(Sum)";Expression={$_."TotalItemSize(Sum)" / 1MB}} -ExcludeProperty "TotalItemSize(Sum)"
Screenshot:
Example 05: Calculate average value of ItemCount property and sum value of TotalItemSize property for specific mailbox database. IssueWarningQuota and ProhibitSendQuota which are the properties of mailbox database will be included in the
report.
Command:
Get-OSCEXMailboxDBReport -Database "MailboxDB" -DataBaseProperty "IssueWarningQuota","ProhibitSendQuota" -MailboxStatisticsProperty "ItemCount(Average)","TotalItemSize(Sum)"
Screenshot:
Example 06: Calculate average value of ItemCount property and sum value of TotalItemSize property for all mailbox databases. Then use "Calculated Property" to convert TotalItemSize from bytes to megabytes.
Command:
$dbs = Get-MailboxDatabase
$dbs | %{Get-OSCEXMailboxDBReport -Database $_.Name -MailboxStatisticsProperty "TotalItemSize(Sum)","ItemCount(Average)" -Verbose | Select *,@{Name="TotalItemSizeInMB(Sum)";Expression={$_."TotalItemSize(Sum)" / 1MB}} -ExcludeProperty "TotalItemSize(Sum)"}
Screenshot:
Technical Resources:
Windows PowerShell Advanced Function
http://technet.microsoft.com/en-us/library/dd315326.aspx
Get-MailboxDatabase
http://technet.microsoft.com/en-us/library/bb124924.aspx
Get-MailboxStatistics
http://technet.microsoft.com/en-us/library/bb124612.aspx
Windows PowerShell Tip: Using Calculated Properties
http://technet.microsoft.com/en-us/library/ff730948.aspx
Forum Threads:
Get average mailbox size -possible?
http://social.technet.microsoft.com/Forums/eu/exchange2010/thread/da15743f-6200-45e7-b7be-e93a62a8fdb2