Skip to content

Instantly share code, notes, and snippets.

@bill-long
Last active March 17, 2022 17:11
Show Gist options
  • Save bill-long/aa7055990d902c164f02a050ab691ba2 to your computer and use it in GitHub Desktop.
Save bill-long/aa7055990d902c164f02a050ab691ba2 to your computer and use it in GitHub Desktop.
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]
$Mailbox,
[Parameter(Mandatory = $true)]
[string]
$MID
)
. $exscripts\ManagedStoreDiagnosticFunctions.ps1
$mb = Get-Mailbox $Mailbox
if ($null -eq $mb) {
Write-Host "Get-Mailbox did not find the specified mailbox."
return
}
$mbTableResult = Get-StoreQuery -Database "$($mb.Database.ToString())" -Query "SELECT MailboxNumber FROM Mailbox WHERE MailboxGuid = '$($mb.ExchangeGuid.ToString())'"
Write-Host "Found mailbox $($mb.DisplayName) with mailbox number $($mbTableResult.MailboxNumber)."
$mailboxNumber = $mbTableResult.MailboxNumber
$replid = $mid.Split("-")[0]
$globcnt = $mid.Split("-")[1]
$guidResult = Get-StoreQuery -Database "$($mb.Database.ToString())" -Query "SELECT Guid FROM ReplidGuidMap WHERE MailboxNumber = $mailboxNumber AND Replid = $replid"
$calculatedId = "0x"
$guidResult.Guid.ToByteArray() | ForEach-Object { $calculatedId += $_.ToString("X2") }
$calculatedId += $globcnt.PadLeft(12, "0")
$calculatedId += "0000"
[BitConverter]::GetBytes([int16]::parse($replid)) | ForEach-Object { $calculatedId += $_.ToString("X2") }
$messageTableResult = @(Get-StoreQuery -Database "$($mb.Database.ToString())" -Query "SELECT * FROM Message WHERE MailboxNumber = $mailboxNumber AND MessageId = $calculatedId" -Unlimited)
if ($messageTableResult.Count -lt 1) {
Write-Host "No results were returned."
}
if ([string]::IsNullOrEmpty($messageTableResult[0].MessageId)) {
Write-Host "No message was found with a matching MID."
return
}
Write-Host "Found $($messageTableResult.Length) message table results with matching MID." # Should only ever be one, but just in case.
foreach ($message in $messageTableResult) {
$folderTableResults = Get-StoreQuery -Database "$($mb.Database.ToString())" -Query "SELECT FolderId,ParentFolderId,DisplayName FROM Folder WHERE MailboxNumber = $mailboxNumber" -Unlimited
$folderPath = ""
$currentId = $message.FolderId
$matchingFolder = $null
do {
$matchingFolder = @($folderTableResults | Where-Object { $_.FolderId -eq $currentId })
if ($matchingFolder.Length -gt 0) {
$folderPath = $matchingFolder.DisplayName + "\" + $folderPath
$currentId = $matchingFolder.ParentFolderId
}
} while ($matchingFolder.Length)
$props = Get-StoreQuery -Database "$($mb.Database.ToString())" -Query "SELECT * FROM ParsePropertyBlob($($message.PropertyBlob))"
$propsOutput = [PSCustomObject]@{
Folder = $folderPath
DateReceived = $message.DateReceived.ToString()
Subject = $props | Where-Object PropertyTag -EQ "0E1D001F" | Select-Object -ExpandProperty PropertyValue
}
$propsOutput
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment