-1

I am new to powershell scripting and I am trying to create a script that I can tweak as needed to delete phishing emails from exchange.
So far I have this:

Import-Module ExchangeOnlineManagement
Connect-IPPSSession -UserPrincipalName xxxxxxxxxx@xxxx.com
        
New-ComplianceSearch -Name Test1 `
-ExchangeLocation All `
-ContentMatchQuery 'subject:"Informational-severity alert: eDiscovery search started or exported" AND sent:08/31/2021'
        
Start-ComplianceSearch -Identity Test1
        
Get-ComplianceSearch -Identity Test1
        
New-ComplianceSearchAction -Searchname Test1 -Purge -PurgeType Softdelete

The script works up until the purge line due to the fact that the compliance search is still in progress. So how can I adjust the script to have it wait until the compliance search is complete before running the purge on the emails? Again, I am very much a beginner at powershell, so take that into consideration when writing an answer, please!

starball
  • 20,030
  • 7
  • 43
  • 238
retrac89
  • 9
  • 1
  • 1
    Don't you want to register the purge action _before_ starting the search? So that the search results are, you know, purged :-) – Mathias R. Jessen Aug 31 '21 at 14:49
  • Good call! So that works, but I started the search after the purge, and then do the: Get-ComplianceSearch -Identity Test3 | Format List * and I get the error that the search is still running. So is there a way to make it wait until the search is complete to get the results, and hopefully have a way to export them to a .csv file – retrac89 Aug 31 '21 at 15:12

1 Answers1

0

Use the Start-Job command to start a task, and then run the Wait-Job command to wait for the job to finish before continuing to the next command in your script.

From the PowerShell documentation Start-Job https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/start-job?view=powershell-7.1
Wait-Job https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/wait-job?view=powershell-7.1

Shaqil Ismail
  • 1,794
  • 1
  • 4
  • 5