Try/Catch block not handling ExchangeOnline Write-ErrorMessage

rektbyazure 20 Reputation points
2023-07-07T11:54:54.9033333+00:00

Hello,

Sometimes when a domain is added to a tenant, Exchange takes a while to pick it up. If you try to work on that domain with PowerShell you might get this error:

Write-ErrorMessage : ExD48BF8|Microsoft.Exchange.Configuration.ObjectModel.NotAcceptedDomainException|You can't use the domain (Your-Domain) because it's not an accepted domain for your organization.

In this case, I'm trying to match an existing SMTP address with the new domain, like this:

Set-Mailbox -Identity $mailbox.Identity -EmailAddresses SMTP:admin@$domain

My snippet block looks like this:

$retryCount = 0
$maxRetries = 6
$domainCreated = $false

do {
    # Try to create a new shared mailbox
    try {
        $mailbox = New-Mailbox -Shared -Name "$domain administration" -ErrorAction Stop
        # Set the primary email address to the desired domain
        Set-Mailbox -Identity $mailbox.Identity -EmailAddresses SMTP:reports.$domain@$domain
        # Line separator for aesthetic purposes
        Write-Host "Domain confirmed!" -ForegroundColor Blue
        Write-Host "Successfully created mailbox: $domain administration" -ForegroundColor Green
        $domainCreated = $true
    }
    catch {
        # If the output contains the error message indicating a NotAcceptedDomainException
        if ($_.Exception.Message -like "*You can't use the domain* because it's not an accepted domain for your organization.*") {
            $retryCount++
            if ($retryCount -ge $maxRetries) {
                Write-Host "Domain is not accepted by organization, please check it exists and try again" -ForegroundColor Yellow
                exit
            } else {
                Write-Host "Domain not yet accepted by tenant, waiting one hour before trying again ($retryCount/$maxRetries attempts)" -ForegroundColor Yellow
                Start-Sleep -Seconds 3600
            }
        }
        # If the output contains the error message indicating a NameNotAvailableException
        elseif ($_.Exception.Message -like "*NameNotAvailableException*") {
            # The name is already in use, try to set the primary SMTP address for the existing mailbox
            try {
                Set-Mailbox -Identity "$domain administration" -EmailAddresses SMTP:reports.$domain@$domain
                Write-Host "$domain administration detected, updated SMTP address, moving on." -ForegroundColor Yellow
                $domainCreated = $true
            }
            catch {
                Write-Host "Failed to update SMTP address for $domain administration: $_" -ForegroundColor Red
                break
            }
        }
        else {
            Write-Host "An error occurred: $_" -ForegroundColor Red
            break
        }
    }
} while (-not $domainCreated)

I still get the error above and I'm not sure how can I catch it. The goal is to catch the error and retry the steps after 3600 seconds but the Write-ErrorMessage thing keeps slipping past the block.

Appreciate your help.

Exchange Online
Exchange Online
A Microsoft email and calendaring hosted service.
6,171 questions
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2023-07-07T15:33:48.1566667+00:00

    You need -ErrorAction STOP on the Set-Mailbox cmdlet, too/


  2. Rich Matheisen 47,901 Reputation points
    2023-07-09T14:42:26.94+00:00

    I think a better way to approach the problem (a slow replication) is to simply check the results from the Get-AcceptedDomain cmdlet before attempting to assign an email address that, potentially, is unknown to your tenent.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.