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

Code before:
    exit
}

if ($failedMessages.count -ge 1000) {
    Write-Host "We get more than 1000 messages, please limit your search." -ForegroundColor Red
    exit
}

if (-not $IncludeDuplicates) {
    $uniqueMessages = New-Object System.Collections.Generic.List[object]
    $uniqueMessages += $failedMessages | Sort-Object MessageId -Unique
    $failedMessages = $uniqueMessages
}

$verifiedAcceptedSenderMessages = New-Object System.Collections.Generic.List[object]
$count = 0
$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 "$_"
        }
    }
}

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

Code after:

if ($failedMessages.count -ge 1000) {
    Write-Host "We get more than 1000 messages, please limit your search." -ForegroundColor Red
    exit
}

if (-not $IncludeDuplicates) {
    [array]$failedMessages = $failedMessages | Sort-Object MessageId -Unique
}

$verifiedAcceptedSenderMessages = New-Object System.Collections.Generic.List[object]
$count = 0
$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.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) {