forked from 12Knocksinna/Office365itpros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPurgeMessagesWithContentSearch.PS1
60 lines (55 loc) · 3.02 KB
/
PurgeMessagesWithContentSearch.PS1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# PurgeMessagesWithContentSearch.PS1
# https://github.com/12Knocksinna/Office365itpros/blob/master/PurgeMessagesWithContentSearch.PS1
#
# A script to purge messages from Exchange Online using an Office 365 content search and a purge action applied to
# the results of that search.
# V1.0 March 2020
# ---------------------------------------
# Some information to identify the messages we want to purge
$Sender = "[email protected]"
$Subject = "Special Offer for you"
# Date range for the search - make this as precise as possible
$StartDate = "10-Mar-2020"
$EndDate = "13-Mar-2020"
$Start = (Get-Date $StartDate).ToString('yyyy-MM-dd')
$End = (Get-Date $EndDate).ToString('yyyy-MM-dd')
$ContentQuery = '(c:c)(received=' + $Start + '..' + $End +')(senderauthor=' + $Sender + ')(subjecttitle="' + $Subject + '")'
CLS
If (Get-ComplianceSearch -Identity "Remove Offensive Information") {
Write-Host "Cleaning up old search"
Try {
$Status = Remove-ComplianceSearch -Identity "Remove Offensive Information" -Confirm:$False }
Catch {
Write-Host "We can't clean up the old search" ; break }}
New-ComplianceSearch -Name "Remove Offensive Information" -ContentMatchQuery $ContentQuery -ExchangeLocation All -AllowNotFoundExchangeLocationsEnabled $True | Out-Null
Write-Host "Starting Search..."
Start-ComplianceSearch -Identity "Remove Offensive Information" | Out-Null
$Seconds = 0
While ((Get-ComplianceSearch -Identity "Remove Offensive Information").Status -ne "Completed") {
$Seconds++
Write-Host "Still searching... (" $Seconds ")"
Sleep -Seconds 1 }
$ItemsFound = (Get-ComplianceSearch -Identity "Remove Offensive Information").Items
If ($ItemsFound -gt 0) {
$Stats = Get-ComplianceSearch -Identity "Remove Offensive Information" | Select -Expand SearchStatistics | Convertfrom-JSON
$Data = $Stats.ExchangeBinding.Sources |?{$_.ContentItems -gt 0}
Write-Host ""
Write-Host "Total Items found matching query:" $ItemsFound
Write-Host ""
Write-Host "Items found in the following mailboxes"
Write-Host "--------------------------------------"
Foreach ($D in $Data) {Write-Host $D.Name "has" $D.ContentItems "items of size" $D.ContentSize }
Write-Host " "
$Iterations = 0; $ItemsProcessed = 0
While ($ItemsProcessed -lt $ItemsFound) {
$Iterations++
Write-Host "Deleting items... (" $Iterations ")"
New-ComplianceSearchAction -SearchName "Remove Offensive Information" -Purge -PurgeType HardDelete -Confirm:$False | Out-Null
While ((Get-ComplianceSearchAction -Identity "Remove Offensive Information_Purge").Status -ne "Completed") { # Let the search action complete
Sleep -Seconds 2 }
$ItemsProcessed = $ItemsProcessed + 10 # Can remove a maximum of 10 items per mailbox
# Remove the search action so we can recreate it
Remove-ComplianceSearchAction -Identity "Remove Offensive Information_Purge" -Confirm:$False }}
Else {
Write-Host "No items found" }
Write-Host "All done!"