Combining Get-MessageTrace and Get-MessageTraceDetail into single CSV report PowerShell

Viewed 18

I'm trying to combine output of Get-MessageTrace and Get-MessageTraceDetail into single CSV report. The goal is to get list of sent emails from SMTP@domain.com, with status "delivered" and with status "failed", and include output from Get-MessageTraceDetail, to have detailed information for failed deliveries. For emails with delivery status failed, grab MessageTraceId (cb53e3cd-bs2e-45c7-444c-08sa7bfnetee) of failed email, and -RecipientAddress (user@domain.com) and pipe it to CSV file. So the output in the report will provide detailed information on why specifically delivery failed.

#Get list of delivered and failed emails:
Connect-ExchangeOnline

$date = Get-Date -Format "dd/MMM/yyyy"
$dateMinusTenDays = ((Get-Date $date).AddDays(-1)).ToString("dd/MMM/yyyy")

Get-MessageTrace -SenderAddress SMTP@domain.com -StartDate $dateMinusTenDays -EndDate $date | Select-Object * | Export-Csv -Path "C:\temp\deliveryReport.csv" -Append -Force

#Get detailed information on emails with delivery status failed.
$recipients = Get-MessageTrace -SenderAddress SMTP@domain.com -StartDate $dateMinusTenDays -EndDate $date | Select-Object *

foreach($recipient in $recipients){if($recipient.Status -eq "Failed"){Get-MessageTraceDetail -MessageTraceId $recipient.MessageTraceId -RecipientAddress $recipient.RecipientAddress Write-Host $recipient.RecipientAddress $recipient.MessageTraceId #Get-MessageTraceDetail -MessageTraceId cb53e3cd-bs2e-45c7-444c-08sa7bfnetee -RecipientAddress rrobert@domain.com}}
0 Answers
Related