I am trying to create a try catch for a certain exception, for New-ComplianceSearchAction, but till now no success.
Any one know how can I deal with this error from that command?
This is the error that I want to handle:
Unable to execute the task. Reason: The search "asd" is still running or it didn't return any results. Please wait until the search finishes or edit the query and run the search again. + CategoryInfo : WriteError: (:) [New-ComplianceSearchAction], ComplianceJobTaskException + FullyQualifiedErrorId : [Server=VI1EUR03WS021,RequestId=e0c617b6-2906-4d24-8da8-93b49aba6405,TimeStamp=4/24/2023 3:24:56 PM] [FailureCategory=Cmdlet-ComplianceJobTaskException] 5B1778A4,Microsoft.Office.ComplianceJob.Tasks.NewComplianceSearchAction + PSComputerName : eur03b.ps.compliance.protection.outlook.com Unable to execute the task. Reason: The search "asd" is still running or it didn't return any results. Please wait until the search finishes or edit the query and run the search again. + CategoryInfo : WriteError: (:) [New-ComplianceSearchAction], ComplianceJobTaskException + FullyQualifiedErrorId : [Server=VI1EUR03WS021,RequestId=e0c617b6-2906-4d24-8da8-93b49aba6405,TimeStamp=4/24/2023 3:24:56 PM] [FailureCategory=Cmdlet-ComplianceJobTaskException] 5B1778A4,Microsoft.Office.ComplianceJob.Tasks.New
ComplianceSearchAction + PSComputerName : eur03b.ps.compliance.protection.outlook.com
And this is the code that I am using, to catch this error, or even any:
### CONTENT SEARCH - EXPORT
Write-Output "Content Search - Export: started"
$search_name = 'asd'
# Exports and create the Search results key and url
try {
$ExportDetails = New-ComplianceSearchAction -SearchName "$search_name" -Export -ExchangeArchiveFormat PerUserPst -Format FXStream -IncludeCredential -ErrorAction STOP
}
catch [ComplianceJobTaskException] {
Write-Host "An error occurred while running the command."
Write-Error $_.Exception.Message -ErrorAction STOP
exit
}