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

Code before:
    } else {
        try {
            $tempAddress = Get-EXORecipient $failedMessage.SenderAddress -ErrorAction Stop
            if ($null -eq $tempAddress) {
                Write-Host "Sender $($failedMessage.RecipientAddress) is not a recipient in this tenant." -ForegroundColor Yellow
                Write-Host "Discarded $($failedMessage.MessageId) - Subject: $($failedMessage.Subject)" -ForegroundColor Yellow
            } else {
                Write-Verbose "Added to cache Recipient $($failedMessage.SenderAddress) with Id $($failedMessage.SenderAddress)"
                $recipientCache[$failedMessage.SenderAddress] = $failedMessage.SenderAddress
                Write-Verbose "Verified $($failedMessage.SenderAddress)"
                $verifiedAcceptedSenderMessages += $failedMessage
            }
        } catch {
            Write-Host "Error getting Sender $($failedMessage.SenderAddress)" -ForegroundColor Yellow
            Write-Host "Discarded $($failedMessage.MessageId) - Subject: $($failedMessage.Subject)" -ForegroundColor Yellow
            Write-Verbose "$_"
        }
    }
}

if ( $verifiedAcceptedSenderMessages ) {
    if ($verifiedAcceptedSenderMessages.Count) {
        $totalMessages = $verifiedAcceptedSenderMessages.Count
    } else {
        $totalMessages = 1
    }

    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
        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-MgUserMail -UserId $failedMessage.SenderAddress -Message $fullMessage -ErrorAction Stop

Review comment (suggestion):
This shouldn't be needed. If you don't have a count on `$verifiedAcceptedSenderMessages` we shouldn't be entering the `foreach` loop where you could possibly divide by zero. 

Code after:
            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 "$_"
                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