正在创建一个删除邮件的powershell脚本



我是powershell脚本的新手,我正在尝试创建一个脚本,我可以根据需要调整从交换中删除网络钓鱼电子邮件。
到目前为止,我有这个:

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

由于遵从性搜索仍在进行中,该脚本一直工作到清除行。那么我该如何调整脚本,让它等到符合性搜索完成后再对电子邮件执行清除呢?再说一次,我是powershell的初学者,所以在写答案时请考虑到这一点!

使用Start-Job命令启动任务,然后运行Wait-Job命令等待任务完成,然后再继续执行脚本中的下一个命令。

来自PowerShell文档Start-Jobhttps://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/start-job?view=powershell-7.1
Wait-Jobhttps://learn.microsoft.com/en - us/powershell/module/microsoft.powershell.core/wait job?view=powershell - 7.1

最新更新