Last active
July 12, 2017 21:06
-
-
Save bill-long/e5335414a8bed0a45fd1494b6b069308 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
######################################### | |
# Delete-RulesAfterDate.ps1 | |
# | |
# Deletes all inbox rules that were created after a certain date. | |
# The date must be configured below. Also, the script won't actually | |
# delete anything until you flip $makChanges to $true. | |
# | |
# Example: | |
# | |
# .\Delete-RulesAfterDate.ps1 -HostName e16srv1.contoso.com -UserName admin@contoso.com -Mailbox someuser@contoso.com | |
# | |
# Instead of specifying -Mailbox, you can also pass it a file that contains a list of SMTP addresses: | |
# | |
# .\Delete-RulesAfterDate.ps1 -HostName e16srv1.contoso.com -UserName admin@contoso.com -InputFile $home\Desktop\somefile.txt | |
param([string]$HostName, [string]$UserName, [string]$Mailbox, [string]$InputFile) | |
######################################### | |
# Update the path below to match the actual path to the EWS managed API DLL. | |
# | |
Import-Module -Name "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll" | |
# | |
######################################### | |
######################################### | |
# Update this value to contain the date after which rules are deleted, and update the expected | |
# number of rules to delete. If we find more rules than this, we just output them and skip the | |
# mailbox so the admin can sanity-check the result. | |
# | |
$deleteRulesAfterDate = [DateTime]::Parse("July 1, 2017") | |
$maxRulesToDelete = 1 | |
# | |
######################################### | |
######################################### | |
# The script doesn't delete anything unless you set this to true | |
# | |
$makeChanges = $false | |
# | |
######################################### | |
if ($InputFile -eq "" -and $Mailbox -eq "") | |
{ | |
$Mailbox = Read-Host "Mailbox to process" | |
} | |
elseif ($InputFile -ne "" -and $Mailbox -ne "") | |
{ | |
"-Mailbox and -InputFile are mutually exclusive. Only one may be specified." | |
return | |
} | |
$mailboxList = new-object 'System.Collections.Generic.List[string]' | |
if ($InputFile -ne "") | |
{ | |
$inputReader = new-object System.IO.StreamReader($InputFile) | |
while ($null -ne ($buffer = $inputReader.ReadLine())) | |
{ | |
if ($buffer.Length -gt 0) | |
{ | |
$mailboxList.Add($buffer) | |
} | |
} | |
} | |
else | |
{ | |
$mailboxList.Add($Mailbox) | |
} | |
("Number of mailboxes to process: " + $mailboxList.Count.ToString()) | |
if ($mailboxList.Count -lt 1) | |
{ | |
return | |
} | |
if ($HostName -eq "") | |
{ | |
$HostName = Read-Host "Hostname for EWS endpoint (leave blank to attempt Autodiscover)" | |
} | |
if ($UserName -eq "") | |
{ | |
$UserName = Read-Host "User (UPN format)" | |
} | |
$password = $host.ui.PromptForCredential("Credentials", "Please enter your password to authenticate to EWS.", $UserName, "").GetNetworkCredential().Password | |
# If a URL was specified we'll use that; otherwise we'll use Autodiscover | |
$exchService = new-object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010) | |
$exchService.Credentials = new-object System.Net.NetworkCredential($UserName, $password, "") | |
if ($HostName -ne "") | |
{ | |
("Using EWS URL " + "https://" + $HostName + "/EWS/Exchange.asmx") | |
$exchService.Url = new-object System.Uri(("https://" + $HostName + "/EWS/Exchange.asmx")) | |
} | |
else | |
{ | |
("Autodiscovering " + $mailboxList[0] + "...") | |
$exchService.AutoDiscoverUrl($mailboxList[0], {$true}) | |
} | |
if ($exchService.Url -eq $null) | |
{ | |
return | |
} | |
$ruleNameProperty = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x65EC, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String) | |
$itemClassProperty = [Microsoft.Exchange.WebServices.Data.ItemSchema]::ItemClass | |
$dateTimeCreatedProperty = [Microsoft.Exchange.WebServices.Data.ItemSchema]::DateTimeCreated | |
$arrayOfPropertiesToLoad = @($ruleNameProperty, $itemClassProperty, $dateTimeCreatedProperty) | |
$propertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet($arrayOfPropertiesToLoad) | |
function DoFolder($folder, $path) | |
{ | |
" Processing folder: " + $folder.DisplayName | |
$rulesToDelete = @() | |
$itemView = new-object Microsoft.Exchange.WebServices.Data.ItemView(1000) | |
$itemView.PropertySet = $propertySet | |
$itemView.Traversal = [Microsoft.Exchange.WebServices.Data.ItemTraversal]::Associated | |
while (($folderItems = $folder.FindItems($itemView)).Items.Count -gt 0) | |
{ | |
foreach ($item in $folderItems) | |
{ | |
if ($item.ItemClass -eq "IPM.Rule.Version2.Message" -and $item.DateTimeCreated -gt $deleteRulesAfterDate) | |
{ | |
$ruleName = $null | |
$item.TryGetProperty($ruleNameProperty, [ref]$ruleName) | Out-Null | |
" Found item:" | |
" Class: " + $item.ItemClass | |
" Created: " + $item.DateTimeCreated.ToString() | |
" Name: " + $ruleName | |
$rulesToDelete += $item | |
} | |
} | |
$offset += $folderItems.Items.Count | |
$itemView = new-object Microsoft.Exchange.WebServices.Data.ItemView(100, $offset) | |
} | |
if ($rulesToDelete.Length -gt $maxRulesToDelete) | |
{ | |
" I found " + $rulesToDelete.Length.ToString() + ' rules to delete. Because this is greater than $maxRulesToDelete, no action will be taken on this mailbox.' | |
} | |
elseif ($makeChanges -ne $true) | |
{ | |
" I found " + $rulesToDelete.Length.ToString() + ' rules to delete, which is below the $maxRulesToDelete, but because $makeChanges is $false, no action will be taken.' | |
} | |
else | |
{ | |
" Deleting " + $rulesToDelete.Length.ToString() + " rules." | |
foreach ($rule in $rulesToDelete) | |
{ | |
$rule.Delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete) | |
} | |
} | |
} | |
function DoMailbox($thisMailbox) | |
{ | |
$inboxFolderName = [Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox | |
$inboxId = new-object Microsoft.Exchange.WebServices.Data.FolderId($inboxFolderName, $mbx) | |
$inboxFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($exchService, $inboxId) | |
if ($inboxFolder -eq $null) | |
{ | |
("Error. Could not open mailbox: " + $emailAddress) | |
return | |
} | |
"Opened mailbox: " + $emailAddress | |
DoFolder $inboxFolder | |
} | |
foreach ($emailAddress in $mailboxList) | |
{ | |
$mbx = new-object Microsoft.Exchange.WebServices.Data.Mailbox($emailAddress) | |
DoMailbox($mbx) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment