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

Code before:
            }
        } 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
                Write-Verbose "$_"
            }
            if ($fullMessage.Count -eq 0) {
                Write-Host "Message not found for $($failedMessage.MessageId)" -ForegroundColor Yellow
            } else {
                Write-Verbose "Resending message $($failedMessage.MessageId) - Subject: $($fullMessage.Subject)"
                try {
                    Send-MgUserMessage -UserId $failedMessage.SenderAddress -MessageId $fullMessage.Id -ErrorAction Stop
                    Write-Host "Resent Message: $($failedMessage.MessageId) - Subject: $($fullMessage.Subject)"
                    $resendCount++
                } catch {
                    Write-Host "Error resending message $($failedMessage.MessageId) - Subject: $($fullMessage.Subject)" -ForegroundColor Red
                    Write-Verbose "$_"
                }
            }
        }
        Write-Host "Summary"
        Write-Host "Total Successful Resent: $resendCount"
        if ($totalMessages - $resendCount -gt 0) {
            Write-Host "Total Unsuccessful Resent: $($totalMessages-$resendCount)" -ForegroundColor Yellow
        }
    }

Review comment (bug):
We should add a `continue` here, as we failed to get the message. Move onto the next message in the loop. 

Code after:
            $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
                Write-Verbose "$_"
                continue
            }
            if ($fullMessage.Count -eq 0) {
                Write-Host "Message not found for $($failedMessage.MessageId)" -ForegroundColor Yellow
            } else {
                Write-Verbose "Resending message $($failedMessage.MessageId) - Subject: $($fullMessage.Subject)"
                try {
                    Send-MgUserMessage -UserId $failedMessage.SenderAddress -MessageId $fullMessage.Id
                    Write-Host "Resent Message: $($failedMessage.MessageId) - Subject: $($fullMessage.Subject)"
                    $resendCount++
                } catch {
                    Write-Host "Error resending message $($failedMessage.MessageId) - Subject: $($fullMessage.Subject)" -ForegroundColor Red
                    Write-Verbose "$_"
                }
            }
        }
        Write-Host "Summary"
        Write-Host "Total Successful Resent: $resendCount"
        if ($totalMessages - $resendCount -gt 0) {
            Write-Host "Total Unsuccessful Resent: $($totalMessages-$resendCount)" -ForegroundColor Yellow
        }
    }
} else {
    Write-Host "No messages found" -ForegroundColor Yellow
}