Checking a User’s New Email is Active

Often in an onboarding type app, an email will be sent to the new starter with Welcome type info. But this cannot be sent to the new account until the Exchange mailbox is ready, which takes some time depending on which way the wind is blowing at the data centre.

One solution is to stick a one hour pause in the process to ensure it is ready.

But now I’m remembering PowerShell, it all becomes simple ! Following on from my previous post on PowerShell from Power Automate via Azure Automation, more PowerShell to check when the Exchange mailbox is up and running.

Two parts, the PowerShell runbook and a Flow in this case to call it.

This PowerShell accepts one parameter, the email address of the mailbox to check, and simply checks it exists, and if not waits 90 seconds in the below case, until it does, when the job ends.

Param(
    [string]$email
)

$tenantDomain = "somedomain.onmicrosoft.com" # Domain of the tenant the managed identity belongs to 

Connect-ExchangeOnline -ManagedIdentity -Organization $tenantDomain

if ((Get-Mailbox $email).PrimarySmtpAddress -eq $email) {
    "The email used is $email"
    }
else {
    Do {
    Start-Sleep -Seconds 90
    } 
    Until ((Get-Mailbox $email).PrimarySmtpAddress -eq $email)
    Exit
    }

Then the Flow simply waits for the job to complete before continuing to send emails. Using Yes to wait for the job to complete will pause the Flow till it returns success.

Make sure it is used for Exchange mailboxes only, or it will never continue. It would be better to loop for only some 2 hours or so before giving up, and reporting a failure for the Flow to handle.

Leave a comment