#script begin " " write-host "***********************************************" write-host " Security & Compliance Center PowerShell " -foregroundColor yellow -backgroundcolor darkgreen write-host " Core eDiscovery cases - Add users to a hold " -foregroundColor yellow -backgroundcolor darkgreen write-host "***********************************************" " " # Connect to SCC PowerShell using modern authentication if (!$SccSession) { Import-Module ExchangeOnlineManagement Connect-IPPSSession } # Get other required information do{ $casename = Read-Host "Enter the name of the case" $caseexists = (get-compliancecase -identity "$casename" -erroraction SilentlyContinue).isvalid if($caseexists -ne 'True') {"" write-host "A case named '$casename' doesn't exist. Please specify the name of an existing case, or create a new case and then re-run the script." -foregroundColor Yellow ""} }While($caseexists -ne 'True') "" do{ $holdName = Read-Host "Enter the name of the new hold" $holdexists=(get-caseholdpolicy -identity "$holdname" -case "$casename" -erroraction SilentlyContinue).isvalid if($holdexists -eq 'True') {"" write-host "A hold named '$holdname' already exists. Please specify a new hold name." -foregroundColor Yellow ""} }While($holdexists -eq 'True') "" $holdQuery = Read-Host "Enter a search query to create a query-based hold, or press Enter to hold all content" "" $holdstatus = read-host "Do you want the hold enabled after it's created? (Yes/No)" do{ "" $inputfile = read-host "Enter the name of the text file that contains the email addresses of the users to add to the hold" "" $fileexists = test-path -path $inputfile if($fileexists -ne 'True'){write-host "$inputfile doesn't exist. Please enter a valid file name." -foregroundcolor Yellow} }while($fileexists -ne 'True') #Import the list of addresses from the txt file. Trim any excess spaces and make sure all addresses #in the list are unique. [array]$emailAddresses = Get-Content $inputfile -ErrorAction SilentlyContinue | where {$_.trim() -ne ""} | foreach{ $_.Trim() } [int]$dupl = $emailAddresses.count [array]$emailAddresses = $emailAddresses | select-object -unique $dupl -= $emailAddresses.count #Validate email addresses so the hold creation does not run in to an error. if($emailaddresses.count -gt 0){ write-host ($emailAddresses).count "addresses were found in the text file. There were $dupl duplicate entries in the file." -foregroundColor Yellow "" Write-host "Validating the email addresses. Please wait..." -foregroundColor Yellow "" $finallist =@() foreach($emailAddress in $emailAddresses) { if((get-recipient $emailaddress -erroraction SilentlyContinue).isvalid -eq 'True') {$finallist += $emailaddress} else {"Unable to find the user $emailaddress" [array]$excludedlist += $emailaddress} } "" if($finallist.count -gt 0) { "" Write-Host "Creating the hold named $holdname. Please wait..." -foregroundColor Yellow if(($holdstatus -eq "Y") -or ($holdstatus -eq "y") -or ($holdstatus -eq "yes") -or ($holdstatus -eq "YES")){ New-CaseHoldPolicy -Name "$holdName" -Case "$casename" -ExchangeLocation $finallist -Enabled $True | out-null New-CaseHoldRule -Name "$holdName" -Policy "$holdname" -ContentMatchQuery $holdQuery | out-null } else{ New-CaseHoldPolicy -Name "$holdName" -Case "$casename" -ExchangeLocation $finallist -Enabled $false | out-null New-CaseHoldRule -Name "$holdName" -Policy "$holdname" -ContentMatchQuery $holdQuery -disabled $false | out-null } "" } #write log files (if needed) $newhold=Get-CaseHoldPolicy -Identity "$holdname" -Case "$casename" -erroraction SilentlyContinue $newholdrule=Get-CaseHoldRule -Identity "$holdName" -erroraction SilentlyContinue if(($finallist.count -gt 0) -or ($excludedlist.count -gt 0) -or ($newhold.isvalid -eq 'True') -or ($newholdrule.isvalid -eq 'True')) { Write-Host "Generating output files..." -foregroundColor Yellow if($finallist.count -gt 0){ " " | add-content .\LocationsOnHold.txt "Exchange Locations" | add-content .\LocationsOnHold.txt "==================" | add-content .\LocationsOnHold.txt $newhold.ExchangeLocation.name | add-content .\LocationsOnHold.txt} if($excludedlist.count -gt 0){ " "| add-content .\LocationsNotOnHold.txt "Mailboxes not added to the hold" | add-content .\LocationsNotOnHold.txt "===============================" | add-content .\LocationsNotOnHold.txt $excludedlist | add-content .\LocationsNotOnHold.txt} $FormatEnumerationLimit=-1 if($newhold.isvalid -eq 'True'){$newhold|fl >.\GetCaseHoldPolicy.txt} if($newholdrule.isvalid -eq 'True'){$newholdrule|Fl >.\GetCaseHoldRule.txt} } } else {"The hold wasn't created because no valid entries were found in the text file."} "" #Disconnect from SCC PowerShell and PnPOnline Write-host "Disconnecting from SCC PowerShell and PnP Online" -foregroundColor Yellow Get-PSSession | Remove-PSSession Write-host "Script complete!" -foregroundColor Yellow "" #script end