Advertisement
Journeym

Untitled

Jul 16th, 2020
1,810
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $exceptions = "mail1@contoso.com","mail2@contoso.com"
  2. $Sender = "sender@contoso.com" # Used in Search query
  3. $Name = "ComplianceSearchNameHere" # Used in Search query
  4. $date = (get-date).AddDays(-7).tostring("MM/dd/yyyy") # Format can vary, this works fine for me when running on Exchange Server locally
  5. $msgTreshold = 10 # Hardcoded treshold for New-ComplianceSearchAction -Purge https://docs.microsoft.com/en-us/microsoft-365/compliance/search-for-and-delete-messages-in-your-organization?view=o365-worldwide
  6. $logpath = "E:\Scripts\logs\"
  7. $debug = $true # Enables Transcript to $($logpath)test.txt
  8.  
  9. filter timestamp {"$(Get-Date -Format G): $_"}
  10. if ($debug) {Start-Transcript -path  "$($logpath)test.txt" -append}
  11.  
  12. "Starting Remove-SpamNotificationMessages" | timestamp | Out-File  -filepath "$($logpath)purge-log.txt" -Append -noClobber
  13.  
  14. # Start with ComplianceSearch to soft clean up to 10 messages per mailbox that match the cryteria.
  15. Get-ComplianceSearch $Name
  16. if (-not $?) {
  17.     Write-Output "ComplianceSearch with name :$($Name) not found, creating new..." | timestamp
  18.     "ComplianceSearch with name :$($Name) not found, creating new..." | timestamp | Out-File  -filepath "$($logpath)purge-log.txt" -Append -noClobber
  19.     New-ComplianceSearch -Name $Name -ContentMatchQuery "Received<$date AND from:$Sender"
  20. }
  21. else {
  22.     Write-Output "Modifying ComplianceSearch $($Name) settings..." | timestamp
  23.     "Modifying ComplianceSearch $($Name) settings..." | timestamp | Out-File  -filepath "$($logpath)purge-log.txt" -Append -noClobber
  24.     Set-ComplianceSearch -Identity $Name -ContentMatchQuery "Received<$date AND from:$Sender"
  25. }
  26.  
  27. # Waiting for ComplianceSearch to complete
  28. Start-ComplianceSearch -Identity $name
  29. $state = "Running"
  30. do {
  31.     sleep -s 30
  32.     $state = (Get-ComplianceSearch $Name).Status
  33.     Write-Output "Waiting for ComplianceSearch $($Name) to finish" | timestamp
  34.     "Waiting for ComplianceSearch $($Name) to finish" | timestamp | Out-File  -filepath "$($logpath)purge-log.txt" -Append -noClobber
  35. } while ($state -ne "Completed")
  36. Write-Output "ComplianceSearch $($Name) completed, processing results..." | timestamp
  37. "ComplianceSearch $($Name) completed, processing results..." | timestamp | Out-File  -filepath "$($logpath)purge-log.txt" -Append -noClobber
  38. $search = Get-ComplianceSearch $Name
  39.  
  40. # Processing ComplianceSearch results
  41. $results = $search.SuccessResults;
  42. $results | Out-File  -filepath "$($logpath)lastComplianceSearchResults.txt" -Force;
  43. if (($search.Items -le 0) -or ([string]::IsNullOrWhiteSpace($results))) {
  44.     "The compliance search " + $SearchName + " didn't return any useful results." | Out-File  -filepath "$($logpath)purge-log.txt" -Append -noClobber;
  45.     break;
  46. }
  47. $mailboxes = @();
  48. $lines = $results -split '[\r\n]+';
  49. foreach ($line in $lines) {
  50.     if ($line -match 'Location: (\S+),.+Item count: (\d+)' -and [int]$matches[2] -gt $msgTreshold) {
  51.         $mailboxes += $matches[1];
  52.     }
  53. }
  54. "Number of mailboxes that have search hits greater then $($msgTreshold): " + $mailboxes.Count | Out-File  -filepath "$($logpath)purge-log.txt" -Append -noClobber
  55. New-ComplianceSearchAction -SearchName $Name -Purge -PurgeType SoftDelete -Confirm:$false
  56.  
  57.  
  58. # Processing list of mailboxes that has matched item count greater than ComplianceSearchAction purge limit $msgTreshold
  59. foreach ($mailbox in $mailboxes) {
  60.     if (-not $exceptions.Contains($mailbox)) {
  61.         Write-Output "Delete content process for mailbox $($mailbox) started..." | timestamp
  62.         "Delete content process for mailbox $($mailbox) started..." | timestamp | Out-File  -filepath "$($logpath)purge-log.txt" -Append -noClobber
  63.         $result = Search-Mailbox -Identity $mailbox -SearchQuery "Received<$date AND from:$Sender" -SearchDumpster -DeleteContent  -Confirm:$false -Force
  64.         if (-not $?) {
  65.             Write-Output "Delete content process for mailbox $($mailbox) FAILED..." | timestamp
  66.             "Delete content process for mailbox $($mailbox) FAILED..." | timestamp | Out-File  -filepath "$($logpath)purge-log.txt" -Append -noClobber
  67.         }
  68.         else {
  69.             Write-Output "Delete content process for mailbox $($mailbox) finished. Deleted $($result.ResultItemsCount) items" | timestamp
  70.             "Delete content process for mailbox $($mailbox) finished. Deleted $($result.ResultItemsCount) items" | timestamp | Out-File  -filepath "$($logpath)purge-log.txt" -Append -noClobber
  71.         }
  72.     }
  73. }
  74. if ($debug) {Stop-Transcript}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement