Powershell 脚本用于从邮箱中删除超过 7 个月或指定日期的电子邮件

Powershell 脚本用于从邮箱中删除超过 7 个月或指定日期的电子邮件

我在将Search-Mailbox命令转换为另一个命令时遇到了问题。如果你们都知道,将来它就会Search-Mailbox被淘汰。所以我需要为这个命令使用另一个 cmdlet。

我现在的工作命令是Search-Mailbox

Search-Mailbox -Identity [email protected] -SearchQuery "(Received:01/12/2017..$((get-date).AddMonths(-7).ToString("MM/dd/yyy")))" -deletecontet

我读了很多遍https://docs.microsoft.com/en-us/exchange/policy-and-compliance/ediscovery/delete-messages?view=exchserver-2019#step-2-delete-the-message并尝试一步一步操作并获取此代码。

New-ComplianceSearch -Name "Remove older than 7 month messages" -ExchangeLocation [email protected] -ContentMatchQuery "(Received:01/12/2017..$((get-date).AddMonths(-7).ToString("MM/dd/yyy")))"

Start-ComplianceSearch -Identity "Remove older than 7 month messages"

New-ComplianceSearchAction -SearchName "Remove older than 7 month messages" -Purge -PurgeType SoftDelete

但它对我来说不起作用。出现错误

Unable to execute the task. Reason: The search "Remove older than 8 month messages" 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.

添加我的完整 PowerShell 脚本

Start-Transcript


$smtpServer="smtp.office365.com" # Office 365 official smtp server 
$from = "IT Support <[email protected]>" # email from  
$logging = "Enabled" # Set to Disabled to Disable Logging 
$testing = "Disabled" # Set to Disabled to Email Users 
$testRecipient = "[email protected]"  
$date = Get-Date -format ddMMyyyy 

$Username = "[email protected]"
$Password = "test-" | ConvertTo-SecureString -AsPlainText -Force
$UserCredential = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$Password

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri  https://eur04b.ps.compliance.protection.outlook.com/powershell-liveid?PSVersion=5.1.17763.1007 -Credential $UserCredential -Authentication Basic –AllowRedirection

 Import-PSSession $Session

Get-ComplianceSearchAction
New-ComplianceSearch -Name "Remove older than 7 month messages" -ExchangeLocation [email protected]  -ContentMatchQuery "(Received:01/12/2017..$((get-date).AddMonths(-7).ToString("MM/dd/yyy")))"
Start-ComplianceSearch -Identity "Remove older than 7 month messages"
New-ComplianceSearchAction -SearchName "Remove older than 7 month messages" -Purge -PurgeType SoftDelete 

答案1

尝试:

New-ComplianceSearch -Name "Remove older than 7 month messages" -ExchangeLocation [email protected] -ContentMatchQuery "(Received >= 01/12/2017 -and Received <= $((get-date).AddMonths(-7).ToString("MM/dd/yyy")))"

答案2

我在实验室环境中测试了合规性搜索,发现搜索操作(合规性搜索操作)无法分配给搜索(合规性搜索) 才能完成合规性搜索。

以下快照是我的测试结果(合规性搜索的状态从“开始“ 到 ”完全的",可以将搜索动作分配给搜索): 在此处输入图片描述

因此,如果您创建合规性搜索并立即为其分配搜索操作(使用脚本同时运行这些命令),则“新合规搜索操作“命令可能不起作用。

相关内容