Private/Wait-NSPEntraHardMatchReadBack.ps1

function Wait-NSPEntraHardMatchReadBack {
    <#
    .SYNOPSIS
        Re-reads a user after a write, with growing waits. Returns its execution state and value.
    .DESCRIPTION
        applied: reads as desired. unconfirmed: still reads as before, which may be lag. different:
        reads as something else. unknown: could not be read. The waits come from the lesson on
        Conditional Access policies; user objects have not been measured.
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory)][string]$CloudObjectId,
        [Parameter(Mandatory)][string]$Desired,
        [AllowNull()][AllowEmptyString()][string]$Before,
        [AllowEmptyCollection()][int[]]$DelaySeconds = @(2, 4, 8, 15, 30)
    )

    $attempt = 0
    while ($true) {
        $readable = $false
        $value = $null
        try {
            $user = Read-NSPEntraHardMatchUser -CloudObjectId $CloudObjectId
            if ($null -ne $user) {
                $readable = $true
                $value = [string]$user['onPremisesImmutableId']
            }
        } catch { $readable = $false }

        if ($readable) {
            if ($value -ceq $Desired) { return @{ State = 'applied'; Value = $value } }
            if ($value -cne [string]$Before) { return @{ State = 'different'; Value = $value } }
        }
        if ($attempt -ge $DelaySeconds.Count) {
            if ($readable) { return @{ State = 'unconfirmed'; Value = $value } }
            return @{ State = 'unknown'; Value = $null }
        }
        Start-Sleep -Seconds $DelaySeconds[$attempt]
        $attempt++
    }
}