Advertisement
Spagman

ExchangeServerPro - modified script

May 30th, 2012
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .SYNOPSIS
  3. Get-MailboxReport.ps1 - Mailbox report generation script.
  4.  
  5. .DESCRIPTION
  6. Generates a report of useful information for
  7. the specified server, database, mailbox or list of mailboxes.
  8. Use only one parameter at a time depending on the scope of
  9. your mailbox report.
  10.  
  11. .OUTPUTS
  12. Single mailbox reports are output to the console, while all other
  13. reports are output to a CSV file.
  14.  
  15. .PARAMETER all
  16. Generates a report for all mailboxes in the organization.
  17.  
  18. .PARAMETER server
  19. Generates a report for all mailboxes on the specified server.
  20.  
  21. .PARAMETER database
  22. Generates a report for all mailboxes on the specified database.
  23.  
  24. .PARAMETER file
  25. Generates a report for mailbox names listed in the specified text file.
  26.  
  27. .PARAMETER mailbox
  28. Generates a report only for the specified mailbox.
  29.  
  30. .EXAMPLE
  31. .\Get-MailboxReport.ps1 -database HO-MB-01
  32. Returns a report with the mailbox statistics for all mailbox users in
  33. database HO-MB-01
  34.  
  35. .EXAMPLE
  36. .\Get-MailboxReport.ps1 -file .\users.txt
  37. Returns a report with the mailbox statistics for all mailbox users in
  38. the file users.txt. Text file should contain names in a format that
  39. will work for Get-Mailbox, such as the display name, alias, or primary
  40. SMTP address.
  41.  
  42. .EXAMPLE
  43. .\Get-MailboxReport.ps1 -server ex2010-mb1
  44. Generates a report with the mailbox statisitcs for all mailbox users
  45. on ex2010-mb1
  46.  
  47. .LINK
  48. For more script details, a video demo, or to give feedback please go to:
  49. http://exchangeserverpro.com/powershell-script-create-mailbox-size-report-exchange-server-2010
  50.  
  51. .NOTES
  52. Written By: Paul Cunningham
  53. Website:    http://exchangeserverpro.com
  54. Twitter:    http://twitter.com/exchservpro
  55.  
  56. Change Log
  57. V1.0, 2/2/2012 - Initial version
  58. #>
  59.  
  60.  
  61. param(
  62.     [Parameter(ParameterSetName='database')] [string]$database,
  63.     [Parameter(ParameterSetName='file')] [string]$file,
  64.     [Parameter(ParameterSetName='server')] [string]$server,
  65.     [Parameter(ParameterSetName='mailbox')] [string]$mailbox,
  66.     [Parameter(ParameterSetName='all')] [switch]$all
  67. )
  68.  
  69. #...................................
  70. # Variables
  71. #...................................
  72.  
  73. $ErrorActionPreference = "SilentlyContinue"
  74. $WarningPreference = "SilentlyContinue"
  75. $report = @()
  76.  
  77. $timestamp = Get-Date -UFormat %d%m%y
  78. $reportfile = "C:\PSScripts\Copy\MailboxReport-$timestamp.csv"
  79.  
  80.  
  81. #...................................
  82. # Script
  83. #...................................
  84.  
  85. #Add dependencies
  86.  
  87. Import-Module ActiveDirectory
  88.  
  89. # Load the Exchange Tools Function & Get the mailbox list
  90.  
  91. Load_Exchange_Tools
  92.  
  93. Write-Progress -Activity "Preparing" -Status "Retrieving mailbox list" -PercentComplete 0
  94.  
  95. if($all) { $mailboxes = @(Get-Mailbox -resultsize unlimited -IgnoreDefaultScope) }
  96.  
  97. if($server) { $mailboxes = @(Get-Mailbox -server $server -resultsize unlimited -IgnoreDefaultScope) }
  98.  
  99. if($database){ $mailboxes = @(Get-Mailbox -database $database -resultsize unlimited -IgnoreDefaultScope) }
  100.  
  101. if($file) { $mailboxes = @(Get-Content $file | Get-Mailbox -resultsize unlimited -IgnoreDefaultScope) }
  102.  
  103. if($mailbox) { $mailboxes = @(Get-Mailbox $mailbox) }
  104.  
  105. #Get the report
  106.  
  107. Write-Progress -Activity "Preparing" -Status "Collecting report data" -PercentComplete 0
  108.  
  109. $mailboxcount = $mailboxes.count
  110. $i = 0
  111.  
  112. #Loop through mailbox list and find the aged mailboxes
  113. foreach ($mb in $mailboxes)
  114. {
  115.     $i = $i + 1
  116.     $pct = $i/$mailboxcount * 100
  117.     Write-Progress -Activity "Collecting mailbox details" -Status "Processing mailbox $i of $mailboxcount - $mb" -PercentComplete $pct
  118.  
  119.     $stats = $mb | Get-MailboxStatistics | Select-Object TotalItemSize,ItemCount,LastLogonTime
  120.     $lastlogon = $stats.LastLogonTime
  121.  
  122.     #This is an aged mailbox, so we want some extra details about the account
  123.    
  124.     $user = Get-User $mb
  125.     $aduser = Get-ADUser $mb.samaccountname -Properties Enabled,AccountExpirationDate
  126.  
  127.     #Create a custom PS object to aggregate the data we're interested in
  128.    
  129.     $userObj = New-Object PSObject
  130.     $userObj | Add-Member NoteProperty -Name "DisplayName" -Value $mb.DisplayName
  131.     $userObj | Add-Member NoteProperty -Name "Department" -Value $user.Department
  132.     $userObj | Add-Member NoteProperty -Name "Size (Mb)" -Value $stats.TotalItemSize.Value.ToMB()
  133.     $userObj | Add-Member NoteProperty -Name "Date" -Value $timestamp
  134.  
  135.    
  136.     #Add the object to the report
  137.     $report = $report += $userObj
  138. }
  139.  
  140. #Output single mailbox report to console, otherwise output to CSV file
  141. if ($mailbox)
  142. {
  143.     $report | Format-List
  144. }
  145. else
  146. {
  147.     $report | Export-csv -Path $reportfile -NoTypeInformation
  148.     Write-Host -ForegroundColor White "Report written to $reportfile in current path."
  149.     Get-Item $reportfile
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement