Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- .SYNOPSIS
- Get-MailboxReport.ps1 - Mailbox report generation script.
- .DESCRIPTION
- Generates a report of useful information for
- the specified server, database, mailbox or list of mailboxes.
- Use only one parameter at a time depending on the scope of
- your mailbox report.
- .OUTPUTS
- Single mailbox reports are output to the console, while all other
- reports are output to a CSV file.
- .PARAMETER all
- Generates a report for all mailboxes in the organization.
- .PARAMETER server
- Generates a report for all mailboxes on the specified server.
- .PARAMETER database
- Generates a report for all mailboxes on the specified database.
- .PARAMETER file
- Generates a report for mailbox names listed in the specified text file.
- .PARAMETER mailbox
- Generates a report only for the specified mailbox.
- .EXAMPLE
- .\Get-MailboxReport.ps1 -database HO-MB-01
- Returns a report with the mailbox statistics for all mailbox users in
- database HO-MB-01
- .EXAMPLE
- .\Get-MailboxReport.ps1 -file .\users.txt
- Returns a report with the mailbox statistics for all mailbox users in
- the file users.txt. Text file should contain names in a format that
- will work for Get-Mailbox, such as the display name, alias, or primary
- SMTP address.
- .EXAMPLE
- .\Get-MailboxReport.ps1 -server ex2010-mb1
- Generates a report with the mailbox statisitcs for all mailbox users
- on ex2010-mb1
- .LINK
- For more script details, a video demo, or to give feedback please go to:
- http://exchangeserverpro.com/powershell-script-create-mailbox-size-report-exchange-server-2010
- .NOTES
- Written By: Paul Cunningham
- Website: http://exchangeserverpro.com
- Twitter: http://twitter.com/exchservpro
- Change Log
- V1.0, 2/2/2012 - Initial version
- #>
- param(
- [Parameter(ParameterSetName='database')] [string]$database,
- [Parameter(ParameterSetName='file')] [string]$file,
- [Parameter(ParameterSetName='server')] [string]$server,
- [Parameter(ParameterSetName='mailbox')] [string]$mailbox,
- [Parameter(ParameterSetName='all')] [switch]$all
- )
- #...................................
- # Variables
- #...................................
- $ErrorActionPreference = "SilentlyContinue"
- $WarningPreference = "SilentlyContinue"
- $report = @()
- $timestamp = Get-Date -UFormat %d%m%y
- $reportfile = "C:\PSScripts\Copy\MailboxReport-$timestamp.csv"
- #...................................
- # Script
- #...................................
- #Add dependencies
- Import-Module ActiveDirectory
- # Load the Exchange Tools Function & Get the mailbox list
- Load_Exchange_Tools
- Write-Progress -Activity "Preparing" -Status "Retrieving mailbox list" -PercentComplete 0
- if($all) { $mailboxes = @(Get-Mailbox -resultsize unlimited -IgnoreDefaultScope) }
- if($server) { $mailboxes = @(Get-Mailbox -server $server -resultsize unlimited -IgnoreDefaultScope) }
- if($database){ $mailboxes = @(Get-Mailbox -database $database -resultsize unlimited -IgnoreDefaultScope) }
- if($file) { $mailboxes = @(Get-Content $file | Get-Mailbox -resultsize unlimited -IgnoreDefaultScope) }
- if($mailbox) { $mailboxes = @(Get-Mailbox $mailbox) }
- #Get the report
- Write-Progress -Activity "Preparing" -Status "Collecting report data" -PercentComplete 0
- $mailboxcount = $mailboxes.count
- $i = 0
- #Loop through mailbox list and find the aged mailboxes
- foreach ($mb in $mailboxes)
- {
- $i = $i + 1
- $pct = $i/$mailboxcount * 100
- Write-Progress -Activity "Collecting mailbox details" -Status "Processing mailbox $i of $mailboxcount - $mb" -PercentComplete $pct
- $stats = $mb | Get-MailboxStatistics | Select-Object TotalItemSize,ItemCount,LastLogonTime
- $lastlogon = $stats.LastLogonTime
- #This is an aged mailbox, so we want some extra details about the account
- $user = Get-User $mb
- $aduser = Get-ADUser $mb.samaccountname -Properties Enabled,AccountExpirationDate
- #Create a custom PS object to aggregate the data we're interested in
- $userObj = New-Object PSObject
- $userObj | Add-Member NoteProperty -Name "DisplayName" -Value $mb.DisplayName
- $userObj | Add-Member NoteProperty -Name "Department" -Value $user.Department
- $userObj | Add-Member NoteProperty -Name "Size (Mb)" -Value $stats.TotalItemSize.Value.ToMB()
- $userObj | Add-Member NoteProperty -Name "Date" -Value $timestamp
- #Add the object to the report
- $report = $report += $userObj
- }
- #Output single mailbox report to console, otherwise output to CSV file
- if ($mailbox)
- {
- $report | Format-List
- }
- else
- {
- $report | Export-csv -Path $reportfile -NoTypeInformation
- Write-Host -ForegroundColor White "Report written to $reportfile in current path."
- Get-Item $reportfile
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement