Last active
July 28, 2017 06:34
-
-
Save bill-long/cdf0b5979cdec263da50776fd160f168 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
######################################### | |
# Dump-RulesStream.ps1 | |
# | |
# Dumps PR_RW_RULES_STREAM from IPM.RuleOrganizer | |
# | |
# Example: | |
# | |
# .\Export-RulesStream.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: | |
# | |
# .\Export-RulesStream.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" | |
# | |
######################################### | |
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 | |
} | |
$ruleStreamProperty = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x6802, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary) | |
$itemClassProperty = [Microsoft.Exchange.WebServices.Data.ItemSchema]::ItemClass | |
$dateTimeCreatedProperty = [Microsoft.Exchange.WebServices.Data.ItemSchema]::DateTimeCreated | |
$arrayOfPropertiesToLoad = @($ruleStreamProperty, $itemClassProperty, $dateTimeCreatedProperty) | |
$propertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet($arrayOfPropertiesToLoad) | |
function DoFolder($folder, $path) | |
{ | |
" Processing folder: " + $folder.DisplayName | |
$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.RuleOrganizer") | |
{ | |
$ruleStream = $null | |
$item.Load($propertySet) | |
$item.TryGetProperty($ruleStreamProperty, [ref]$ruleStream) | Out-Null | |
$readableRuleStream = "" | |
foreach ($byte in $ruleStream) | |
{ | |
if ($byte -gt 31 -and $byte -lt 127) | |
{ | |
$readableRuleStream += [char]$byte | |
} | |
} | |
" Found item:" | |
" Class: " + $item.ItemClass | |
" Rule Stream (ASCII only): " + $readableRuleStream | |
} | |
} | |
$offset += $folderItems.Items.Count | |
$itemView = new-object Microsoft.Exchange.WebServices.Data.ItemView(100, $offset) | |
} | |
} | |
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