powershell - 删除 PowerShell 中超过 7 个月且未使用 Search-Mailbox 的所有电子邮件
问题描述
我在将Search-Mailbox
命令转换为另一个命令时遇到问题。如果你们都知道在未来作为Search-Mailbox
退休。所以我需要为这个命令使用另一个 cmdlet。
这是我的Search-Mailbox
命令:
Search-Mailbox -Identity test@test.comm -SearchQuery "(Received:01/12/2017..$((get-date).AddMonths(-7).ToString("MM/dd/yyy")))" -deletecontent
我尝试做的事情:
New-ComplianceSearch -Name "Remove older than 7 month messages" -ExchangeLocation test@test.com -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 7 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.
> + CategoryInfo : WriteError: (:) [New-ComplianceSearchAction], ComplianceJobTaskException
这是我正在尝试的一件事。请帮我解决这个问题,我现在很困在这里。
如果我只是运行这个:
New-ComplianceSearch -Name "Remove older than 8 month messages" -ExchangeLocation research.shared@covaliscapital.com -ContentMatchQuery "(Received:01/12/2017..$((get-date).AddMonths(-8).ToString("MM/dd/yyy")))"
Start-ComplianceSearch -Identity "Remove older than 8 month messages"
我得到这个
RunspaceId : sagasgasgasgagsa
Language :
StatusMailRecipients : {}
LogLevel : Suppressed
IncludeUnindexedItems : True
ContentMatchQuery : (Received:01/12/2017..12/04/2019)
SearchType : EstimateSearch
HoldNames : {}
SearchNames : {}
RefinerNames : {}
Region :
Refiners :
Items : 0
Size : 0
UnindexedItems : 0
UnindexedSize : 0
SuccessResults : {}
SearchStatistics :
Errors :
ErrorTags : {}
NumFailedSources : 0
JobId : gasgsagasgasgsagasg
Name : Remove older than TEST month messages
CreatedTime : 8/4/2020 10:49:45 AM
LastModifiedTime : 8/4/2020 10:49:45 AM
JobStartTime :
JobEndTime :
Description :
CreatedBy : IT Support
RunBy :
TenantId : asgasgagasgsa
NumBindings : 0
Status : NotStarted
ExchangeLocation : {TEST@TEST.com}
PublicFolderLocation :
SharePointLocation :
OneDriveLocation :
ExchangeLocationExclusion :
PublicFolderLocationExclusion :
SharePointLocationExclusion :
OneDriveLocationExclusion :
JobRunId : asgasgag
Retry : False
AllowNotFoundExchangeLocationsEnabled : False
JobOptions : 0
JobProgress : 0
CaseId :
CaseName :
PagingState :
Identity : dsaasgfasgsagga
ContentURL :
ResultInEOP : False
AzureBatchFrameworkEnabled : False
IsValid : True
ObjectState : New
我的完整 powershell 脚本
Start-Transcript
$smtpServer="smtp.office365.com" # Office 365 official smtp server
$from = "IT Support <test@test.com>" # email from
$logging = "Enabled" # Set to Disabled to Disable Logging
$testing = "Disabled" # Set to Disabled to Email Users
$testRecipient = "test@test.com"
$date = Get-Date -format ddMMyyyy
$Username = "test@test.com"
$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 test@test.com -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
解决方案
添加后Start-ComplianceSearch -Identity "Remove older than 7 month messages"
:
Get-ComplianceSearch -Identity "Remove older than 7 month messages"
Do {
$Get = Get-ComplianceSearch -Identity "Remove older than 7 month messages"
Write-Host "-- CS $($Get.Status), $($Get.Items)"
}
While ($Get.Status -ne 'Completed')
所以New-ComplianceSearchAction -SearchName "Remove older than 7 month messages" -Purge -PurgeType SoftDelete
在 CS 完成后运行。
推荐阅读
- oracle - 从 Superset 连接 Oracle DB 时出现错误
- exchangewebservices - EWS API - 以与会者身份更新会议
- microsoft-graph-api - 如何使用图形 api 在 Onedrive 上获取文件 ID?
- firefox - Firefox 强大的跟踪保护阻止链接点击
- api - 如何处理服务到服务通信中的安全 API
- node.js - 如何将 gitlab 合并请求用作 npm 模块
- python - Django查询按最新版本的多对多关系上的字段排序
- html - React js材料ui核心表点击从行获取数据
- html - 文本在屏幕的一部分中移动。(而不是从一端到另一端)
- visual-studio-code - 如何阻止 VS Code 打开浏览器