Repository: microsoft/CSS-Exchange
File: M365/MDO/ResendFailedMail.ps1 (PowerShell)

Code before:
$totalMessages = $failedMessages.Count
foreach ($failedMessage in $failedMessages) {
    $count++
    Write-Progress -Activity "Checking Progress" -Status "$count of $totalMessages" -PercentComplete ($count / $totalMessages * 100) -CurrentOperation "Checking message $($failedMessage.MessageId) - Subject: $($failedMessage.Subject)"
    Write-Verbose "Checking $($failedMessage.SenderAddress)"
    $tempAddress = $null
    if ($recipientCache.ContainsKey($failedMessage.SenderAddress)) {
        Write-Verbose "RecipientAddress  $($failedMessage.SenderAddress) found in cache"
        if ($recipientCache[$failedMessage.SenderAddress]) {
            $verifiedAcceptedSenderMessages += $failedMessage
        } else {
            Write-Verbose "Sender $($failedMessage.SenderAddress) is not a recipient in this tenant."
            Write-Verbose "Discarded $($failedMessage.MessageId) - Subject: $($failedMessage.Subject)"
        }
    } else {
        try {
            $tempAddress = Get-EXOMailbox $failedMessage.SenderAddress -ErrorAction Stop
            if ($null -eq $tempAddress) {
                Write-Verbose "Sender $($failedMessage.SenderAddress) is not a recipient in this tenant."
                Write-Verbose "Discarded $($failedMessage.MessageId) - Subject: $($failedMessage.Subject)"
                $recipientCache[$failedMessage.SenderAddress] = $false
            } else {
                Write-Verbose "Added to cache Recipient $($failedMessage.SenderAddress) with Id $($failedMessage.SenderAddress)"
                $recipientCache[$failedMessage.SenderAddress] = $true
                Write-Verbose "Verified $($failedMessage.SenderAddress)"
                $verifiedAcceptedSenderMessages += $failedMessage
            }
        } catch {
            Write-Verbose "Error getting Sender Address $($failedMessage.SenderAddress)"
            Write-Verbose "Discarded $($failedMessage.MessageId) - Subject: $($failedMessage.Subject)"
            $recipientCache[$failedMessage.SenderAddress] = $false
            Write-Verbose "$_"
        }
    }
}

$totalMessages = $verifiedAcceptedSenderMessages.Count
if ($totalMessages -gt 0) {
    if (-not $Force) {
        Write-Host "`nWe are going to resend the following messages:"
        Write-Host ($verifiedAcceptedSenderMessages | Format-Table -AutoSize Received, MessageId, SenderAddress, RecipientAddress, Subject | Out-String)
        Write-Host "Total number of messages: $totalMessages`n"
    }

    if ($Force -or $PSCmdlet.ShouldContinue("Are you sure you want to do it?", "Resend messages")) {
        $count = 0
        $resendCount = 0
        foreach ( $failedMessage in $verifiedAcceptedSenderMessages ) {
            $count++
            Write-Progress -Activity "Resending Progress" -Status "$count of $totalMessages" -PercentComplete ($count / $totalMessages * 100) -CurrentOperation "Resending message $($failedMessage.MessageId) - Subject: $($failedMessage.Subject)"
            try {

Review comment (bug):
```suggestion
                $verifiedAcceptedSenderMessages.Add($failedMessage)
```

Code after:
    Write-Verbose "Checking $($failedMessage.SenderAddress)"
    $tempAddress = $null
    if ($recipientCache.ContainsKey($failedMessage.SenderAddress)) {
        Write-Verbose "RecipientAddress  $($failedMessage.SenderAddress) found in cache"
        if ($recipientCache[$failedMessage.SenderAddress]) {
            $verifiedAcceptedSenderMessages.Add($failedMessage)
        } else {
            Write-Verbose "Sender $($failedMessage.SenderAddress) is not a recipient in this tenant."
            Write-Verbose "Discarded $($failedMessage.MessageId) - Subject: $($failedMessage.Subject)"
        }
    } else {
        try {
            $tempAddress = Get-EXOMailbox $failedMessage.SenderAddress -ErrorAction Stop
            if ($null -eq $tempAddress) {
                Write-Verbose "Sender $($failedMessage.SenderAddress) is not a recipient in this tenant."
                Write-Verbose "Discarded $($failedMessage.MessageId) - Subject: $($failedMessage.Subject)"
                $recipientCache[$failedMessage.SenderAddress] = $false
            } else {
                Write-Verbose "Added to cache Recipient $($failedMessage.SenderAddress) with Id $($failedMessage.SenderAddress)"
                $recipientCache[$failedMessage.SenderAddress] = $true
                Write-Verbose "Verified $($failedMessage.SenderAddress)"
                $verifiedAcceptedSenderMessages.Add($failedMessage)
            }
        } catch {
            Write-Verbose "Error getting Sender Address $($failedMessage.SenderAddress)"
            Write-Verbose "Discarded $($failedMessage.MessageId) - Subject: $($failedMessage.Subject)"
            $recipientCache[$failedMessage.SenderAddress] = $false
            Write-Verbose "$_"
        }
    }
}

$totalMessages = $verifiedAcceptedSenderMessages.Count
if ($totalMessages -gt 0) {
    if (-not $Force) {
        Write-Host "`nWe are going to resend the following messages:"
        Write-Host ($verifiedAcceptedSenderMessages | Format-Table -AutoSize Received, MessageId, SenderAddress, RecipientAddress, Subject | Out-String)
        Write-Host "Total number of messages: $totalMessages`n"
    }

    if ($Force -or $PSCmdlet.ShouldContinue("Are you sure you want to do it?", "Resend messages")) {
        $count = 0
        $resendCount = 0
        foreach ( $failedMessage in $verifiedAcceptedSenderMessages ) {
            $count++
            Write-Progress -Activity "Resending Progress" -Status "$count of $totalMessages" -PercentComplete ($count / $totalMessages * 100) -CurrentOperation "Resending message $($failedMessage.MessageId) - Subject: $($failedMessage.Subject)"
            try {
                $fullMessage = $null
                $fullMessage = Get-MgUserMessage -UserId $failedMessage.SenderAddress -Filter "InternetMessageId eq '$($failedMessage.MessageId)'" -ExpandProperty Attachments -ErrorAction Stop | Sort-Object ReceivedDateTime | Select-Object -First 1
            } catch {
                Write-Host "Error getting message $($failedMessage.MessageId) - Subject: $($failedMessage.Subject)" -ForegroundColor Red