D0cEvil

O365 - Mailbox Holds Report

Sep 23rd, 2022
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 10.88 KB | Cybersecurity | 0 0
  1. <#
  2. =============================================================================================
  3. Name:           Export Office 365 Mailbox Holds Report
  4. Description:    This script exports hold enabled mailboxes to CSV
  5. Version:        1.0
  6. website:        o365reports.com
  7. Script by:      O365Reports Team
  8. For detailed Script execution: http://o365reports.com/2021/06/29/export-office-365-mailbox-holds-report-using-powershell
  9. ============================================================================================
  10. #>
  11.  
  12. param (
  13.     [string] $UserName = $null,
  14.     [string] $Password = $null,
  15.     [Switch] $LitigationHoldsOnly,
  16.     [Switch] $InPlaceHoldsOnly,
  17.     [Switch] $RetentionHoldsOnly
  18. )
  19.  
  20. Function GetBasicData {
  21.     $global:ExportedMailbox = $global:ExportedMailbox + 1
  22.     $global:MailboxName = $_.Name
  23.     $global:RecipientTypeDetails = $_.RecipientTypeDetails
  24.     $global:UPN = $_.UserPrincipalName
  25. }
  26. Function RetrieveAllHolds {
  27.     if ($LitigationHoldsOnly.IsPresent) {
  28.         $global:ExportCSVFileName = "LitigationHoldsReport" + ((Get-Date -format "MMM-dd hh-mm-ss tt").ToString()) + ".csv"
  29.             Get-mailbox -IncludeInactiveMailbox -ResultSize Unlimited | Where-Object { $_.LitigationHoldEnabled -eq $True } | foreach-object {
  30.             $CurrLitigationHold = $_
  31.             GetLitigationHoldsReport
  32.         }
  33.     }
  34.     elseif ($InPlaceHoldsOnly.IsPresent) {
  35.         $global:ExportCSVFileName = "InPlaceHoldsReport" + ((Get-Date -format "MMM-dd hh-mm-ss tt").ToString()) + ".csv"
  36.         Get-mailbox -IncludeInactiveMailbox -ResultSize Unlimited | Where-Object { $_.InPlaceHolds -ne $Empty } | foreach-object {
  37.             $CurrInPlaceHold = $_
  38.             GetInPlaceHoldsReport
  39.         }
  40.     }
  41.     elseif ($RetentionHoldsOnly.IsPresent) {
  42.         $global:ExportCSVFileName = "RetentionHoldsReport" + ((Get-Date -format "MMM-dd hh-mm-ss tt").ToString()) + ".csv"
  43.         Get-mailbox -IncludeInactiveMailbox -ResultSize Unlimited | Where-Object { $_.RetentionHoldEnabled -eq $True } | foreach-object {
  44.             $CurrRetentionHold = $_
  45.             GetRetentionHoldsReport
  46.         }
  47.     }
  48.     else {
  49.         $global:ExportCSVFileName = "AllActiveHoldsReport" + ((Get-Date -format "MMM-dd hh-mm-ss tt").ToString()) + ".csv"
  50.         Get-mailbox -IncludeInactiveMailbox -ResultSize Unlimited | Where-Object { $_.LitigationHoldEnabled -eq $True -or $_.RetentionHoldEnabled -eq $True -or $_.InPlaceHolds -ne $Empty -or $_.ComplianceTagHoldApplied -eq $True } | foreach-object {
  51.             $CurrMailbox = $_
  52.             GetDefaultReport
  53.         }
  54.     }
  55. }
  56.  
  57. Function GetInPlaceHoldType($HoldGuidList) {
  58.     $HoldTypes = @()
  59.     $InPlaceHoldCount = 0
  60.     $HoldGuidList | ForEach-Object {
  61.         $InPlaceHoldCount = $InPlaceHoldCount + 1
  62.         if ($_ -match "UniH") {
  63.             $HoldTypes += "eDiscovery Case"
  64.         }
  65.         elseif ($_ -match "^mbx") {
  66.             $HoldTypes += "Specific Location Retention Policy"
  67.         }
  68.         elseif ($_ -match "^\-mbx") {
  69.             $HoldTypes += "Mailbox Excluded Retention Policy"
  70.         }
  71.         elseif ($_ -match "skp") {
  72.             $HoldTypes += "Retention Policy on Skype"
  73.         }
  74.         else {
  75.             $HoldTypes += "In-Place Hold"
  76.         }
  77.     }
  78.    
  79.     return ($HoldTypes -join ", "), $InPlaceHoldCount
  80. }
  81.  
  82. Function GetLitigationHoldsReport {
  83.     GetBasicData
  84.     $LitigationOwner = $CurrLitigationHold.LitigationHoldOwner
  85.     if ($null -ne $CurrLitigationHold.LitigationHoldDate) {
  86.         $LitigationHoldDate = ($CurrLitigationHold.LitigationHoldDate).ToString().Split(" ") | Select-Object -Index 0
  87.     }
  88.     $LitigationDuration = $CurrLitigationHold.LitigationHoldDuration
  89.     if ($LitigationDuration -ne "Unlimited") {
  90.         $LitigationDuration = ($LitigationDuration).split(".") | Select-Object -Index 0
  91.     }
  92.  
  93.     Write-Progress "Retrieving the Litigation Hold Information for the User: $global:MailboxName" "Processed Mailboxes Count: $global:ExportedMailbox"
  94.  
  95.     #ExportResult
  96.     $ExportResult = @{'Mailbox Name' = $global:MailboxName; 'Mailbox Type' = $global:RecipientTypeDetails; 'UPN' = $global:UPN; 'Litigation Owner' = $LitigationOwner; 'Litigation Duration' = $LitigationDuration; 'Litigation Hold Date' = $LitigationHoldDate }
  97.     $ExportResults = New-Object PSObject -Property $ExportResult
  98.     $ExportResults | Select-object 'Mailbox Name', 'UPN', 'Mailbox Type',  'Litigation Owner', 'Litigation Duration', 'Litigation Hold Date' | Export-csv -path $global:ExportCSVFileName -NoType -Append -Force  
  99. }
  100.  
  101. Function GetInPlaceHoldsReport {
  102.     GetBasicData
  103.     $InPlaceHoldInfo = GetInPlaceHoldType ($CurrInPlaceHold.InPlaceHolds)
  104.     $InPlaceHoldType = $InPlaceHoldInfo[0]
  105.     $NumberOfHolds = $InPlaceHoldInfo[1]
  106.  
  107.     Write-Progress "Retrieving the In-Place Hold Information for the User: $global:MailboxName" "Processed Mailboxes Count: $global:ExportedMailbox"
  108.  
  109.     #Export Results
  110.     $ExportResult = @{'Mailbox Name' = $global:MailboxName; 'Mailbox Type' = $global:RecipientTypeDetails; 'UPN' = $global:UPN; 'Configured InPlace Hold Count' = $NumberOfHolds; 'InPlace Hold Type' = $InPlaceHoldType }
  111.     $ExportResults = New-Object PSObject -Property $ExportResult
  112.     $ExportResults | Select-object 'Mailbox Name', 'UPN', 'Mailbox Type',  'Configured InPlace Hold Count', 'InPlace Hold Type' | Export-csv -path $global:ExportCSVFileName -NoType -Append -Force
  113. }
  114.  
  115. Function GetRetentionHoldsReport {
  116.     GetBasicData
  117.     $RetentionPolicy = $CurrRetentionHold.RetentionPolicy
  118.     $RetentionPolicyTag = ((Get-RetentionPolicy -Identity $RetentionPolicy).RetentionPolicyTagLinks) -join ","
  119.  
  120.     if (($CurrRetentionHold.StartDateForRetentionHold) -ne $Empy) {
  121.         $StartDateForRetentionHold = ($CurrRetentionHold.StartDateForRetentionHold).ToString().split(" ") | Select-Object -Index 0
  122.     }
  123.     else {
  124.         $StartDateForRetentionHold = "-"
  125.     }
  126.     if (($CurrRetentionHold.EndDateForRetentionHold) -ne $Empy) {
  127.         $EndDateForRetentionHold = ($CurrRetentionHold.EndDateForRetentionHold).ToString().split(" ") | Select-Object -Index 0
  128.     }
  129.     else {
  130.         $EndDateForRetentionHold = "-"  
  131.     }
  132.  
  133.     Write-Progress "Retrieving the Retention Hold Information for the User: $global:MailboxName" "Processed Mailboxes Count: $global:ExportedMailbox"
  134.  
  135.     #ExportResult
  136.     $ExportResult = @{'Mailbox Name' = $global:MailboxName; 'Mailbox Type' = $global:RecipientTypeDetails; 'UPN' = $global:UPN; 'Retention Policy Name' = $RetentionPolicy; 'Start Date for Retention Hold' = $StartDateForRetentionHold; 'End Date for Retention Hold' = $EndDateForRetentionHold; 'Retention Policy Tag' = $RetentionPolicyTag }
  137.     $ExportResults = New-Object PSObject -Property $ExportResult
  138.     $ExportResults | Select-object 'Mailbox Name', 'UPN', 'Mailbox Type',  'Retention Policy Name', 'Start Date for Retention Hold', 'End Date for Retention Hold', 'Retention Policy Tag' | Export-csv -path $global:ExportCSVFileName -NoType -Append -Force
  139. }
  140.  
  141. Function GetDefaultReport {
  142.     GetBasicData
  143.     $LitigationHold = $CurrMailbox.LitigationHoldEnabled
  144.     $ComplianceTag = $CurrMailbox.ComplianceTagHoldApplied
  145.     $RetentionHold = $CurrMailbox.RetentionHoldEnabled
  146.     $ArchiveStatus = $CurrMailbox.ArchiveStatus
  147.     $RetentionPolicy = $CurrMailbox.RetentionPolicy
  148.    
  149.     $LitigationDuration = $CurrMailbox.LitigationHoldDuration
  150.     if ($LitigationDuration -ne "Unlimited") {
  151.         $LitigationDuration = ($LitigationDuration).split(".") | Select-Object -Index 0
  152.     }
  153.     $InPlaceHold = $CurrMailbox.InPlaceHolds
  154.     if ($InPlaceHold -ne $Empty) {
  155.         $InPlaceHold = "Enabled"
  156.     }
  157.     else {
  158.         $InPlaceHold = "Disabled"
  159.     }
  160.  
  161.     Write-Progress "Retrieving All Active Holds Applied on the User: $global:MailboxName" "Processed Mailboxes Count: $global:ExportedMailbox"
  162.            
  163.     #ExportResult
  164.     $ExportResult = @{'Mailbox Name' = $global:MailboxName; 'Mailbox Type' = $global:RecipientTypeDetails; 'UPN' = $global:UPN; 'Archive Status' = $ArchiveStatus; 'Litigation Hold Enabled' = $LitigationHold; 'Compliance Tag Hold Enabled' = $ComplianceTag; 'Retention Hold Enabled' = $RetentionHold; 'Litigation Duration' = $LitigationDuration; 'In-Place Hold Status' = $InPlaceHold; 'Retention Policy Name' = $RetentionPolicy }
  165.     $ExportResults = New-Object PSObject -Property $ExportResult
  166.     $ExportResults | Select-object 'Mailbox Name', 'UPN', 'Mailbox Type',  'Archive Status', 'Litigation Hold Enabled', 'In-Place Hold Status', 'Retention Hold Enabled', 'Compliance Tag Hold Enabled', 'Litigation Duration', 'Retention Policy Name' | Export-csv -path $global:ExportCSVFileName -NoType -Append -Force
  167.  
  168. }
  169. Function ConnectToExchange {
  170.     $Exchange = (get-module ExchangeOnlineManagement -ListAvailable).Name
  171.     if ($Exchange -eq $null) {
  172.         Write-host "Important: ExchangeOnline PowerShell module is unavailable. It is mandatory to have this module installed in the system to run the script successfully."
  173.         $confirm = Read-Host Are you sure you want to install module? [Y] Yes [N] No  
  174.         if ($confirm -match "[yY]") {
  175.             Write-host "Installing ExchangeOnlineManagement"
  176.             Install-Module ExchangeOnlineManagement -Repository PSGallery -AllowClobber -Force
  177.             Write-host "ExchangeOnline PowerShell module is installed in the machine successfully."
  178.         }
  179.         elseif ($confirm -cnotmatch "[yY]" ) {
  180.             Write-host "Exiting. `nNote: ExchangeOnline PowerShell module must be available in your system to run the script."
  181.             Exit
  182.         }
  183.     }
  184.     #Storing credential in script for scheduling purpose/Passing credential as parameter
  185.     if (($UserName -ne "") -and ($Password -ne "")) {  
  186.         $SecuredPassword = ConvertTo-SecureString -AsPlainText $Password -Force  
  187.         $Credential = New-Object System.Management.Automation.PSCredential $UserName, $SecuredPassword
  188.         Connect-ExchangeOnline -Credential $Credential -ShowProgress $false | Out-Null
  189.     }
  190.     else {
  191.         Connect-ExchangeOnline | Out-Null
  192.     }
  193.     Write-Host "ExchangeOnline PowerShell module is connected successfully"
  194.     #End of Connecting Exchange Online
  195. }
  196.  
  197. ConnectToExchange
  198. $global:ExportedMailbox = 0
  199. RetrieveAllHolds
  200.  
  201. if ((Test-Path -Path $global:ExportCSVFileName) -eq "True") {    
  202.     Write-Host "The output file available in $global:ExportCSVFileName" -ForegroundColor Green
  203.     write-host "There are $global:ExportedMailbox mailboxes records in the output file"
  204.     $prompt = New-Object -ComObject wscript.shell    
  205.     $userInput = $prompt.popup("Do you want to open output file?", 0, "Open Output File", 4)    
  206.     If ($userInput -eq 6) {    
  207.         Invoke-Item "$global:ExportCSVFileName"
  208.     }  
  209. }
  210. else {
  211.     Write-Host "No data found with the specified criteria"
  212. }
  213. Disconnect-ExchangeOnline -Confirm:$false -InformationAction Ignore -ErrorAction SilentlyContinue
  214. Write-Host "Disconnected active ExchangeOnline session"
  215.  
Add Comment
Please, Sign In to add comment