Introduction
Email trace retrieval is an essential task for IT administrators to monitor and troubleshoot email flow within an organization. By automating this process with PowerShell, we can save time and reduce the risk of human error. This script reads message IDs from a text file, retrieves email details for the last 10 days, and exports the information to a CSV file.
Scenario
During a recent Microsoft outage, Microsoft provided a list of message IDs for the failed messages. Due to their security and compliance policies, they did not provide much detail except for the message IDs. Using these message IDs, we developed an enhanced message trace script to identify the sender, recipient, date, subject, and status of each message.
The Script
Let's dive into the script and understand how it works:
$messageIdsFile = "D:\ramki\Inputs\MID01_04.txt"
$outputCsvFile = "D:\ramki\Inputs\report_01_phase4_1000.csv"
# Read message IDs from the text file
$messageIds = Get-Content -Path $messageIdsFile
# Initialize an array to store email details
$emailDetails = @()
# Define the date range for the message trace search (last 10 days)
$startDate = (Get-Date).AddDays(-10)
$endDate = Get-Date
# Initialize progress bar
$totalMessageIds = $messageIds.Count
$progress = 0
# Loop through each message ID and retrieve email details
foreach ($messageId in $messageIds) {
try {
Write-Progress -Activity "Processing Message IDs" -Status "Processing $progress of $totalMessageIds" -PercentComplete (($progress / $totalMessageIds) * 100)
$progress++
Write-Output "Processing Message ID: $messageId"
$emailTrace = Get-MessageTrace -MessageId $messageId -StartDate $startDate -EndDate $endDate
if ($emailTrace) {
foreach ($trace in $emailTrace) {
$sender = $trace.SenderAddress
$recipient = $trace.RecipientAddress
$subject = $trace.Subject
$date = $trace.Received
$status = $trace.Status
$emailDetails += [PSCustomObject]@{
MessageId = $messageId
Date = $date
Sender = $sender
Recipient = $recipient
Subject = $subject
Status = $status
}
}
Write-Output "Retrieved details for Message ID: $messageId"
} else {
Write-Output "No details found for Message ID: $messageId"
}
} catch {
Write-Output "Error processing Message ID: $messageId - $_"
}
}
# Export email details to a CSV file
$emailDetails | Export-Csv -Path $outputCsvFile -NoTypeInformation
Write-Output "Email details have been exported to $outputCsvFile"
Script Breakdown
- Reading Message IDs: The script starts by reading message IDs from a specified text file.
- Initializing Variables: It initializes an array to store email details and defines the date range for the message trace search (last 10 days).
- Progress Bar: A progress bar is initialized to provide visual feedback during the script execution.
- Retrieving Email Details: The script loops through each message ID, retrieves email details using the
Get-MessageTrace
command, and stores the information in the array. - Error Handling: Error handling is implemented to manage potential issues during the retrieval process.
- Exporting to CSV: Finally, the script exports the collected email details to a CSV file.
Conclusion
Automating email trace retrieval with PowerShell can significantly streamline the process for IT administrators. This script not only saves time but also ensures accuracy and consistency in retrieving email details. Feel free to customize the script to suit your specific requirements and enhance your email management tasks.
I hope this helps! Let me know if you need any further adjustments or additional information for our blog post.
Comments
Post a Comment