Private/Compare-MMSyncedUser.ps1

function Compare-MMSyncedUser {

    [CmdletBinding()]
    param(
        [switch]$FixUPN,           # No Matching UPN
        [switch]$FixTargetAddress, # No RemoteMailbox Object / Type
                                   # Shared Mailbox cloud only converted
                                   # HealthMailbox synced
        [string] $CountVariable
    )

    $exoUser = Get-MMCloudUser -SyncedUser ; $exoUser.Count # 19
    #$exoUser = Get-MMCloudUser -SyncedUser -LicensedUser; $exoUser.Count # 3
    #$exoUser | select * | Out-GridView
    #$exoUser | select * | ft
    #$exoUser | select UserPrincipalName, WindowsLiveID, MicrosoftOnlineServicesID, ExternalDirectoryObjectId, Guid, ExchangeObjectId, DisplayName, WindowsEmailAddress, RecipientType, RecipientTypeDetails

    $exoUserMailbox = $exoUser | Where-Object {$_.RecipientType -eq "UserMailbox"}

        # UserPrincipalName : arl8-test3@contoso.onmicrosoft.com
            # WindowsLiveID : arl8-test3@contoso.onmicrosoft.com
            # MicrosoftOnlineServicesID : arl8-test3@contoso.onmicrosoft.com
        # ExternalDirectoryObjectId : 7bcf6e60-306d-42a7-8ed2-6f15a83f30c9
        # Guid : 00cabc6e-3c3d-4ea1-9308-bd8fd242505e
        # ExchangeObjectId : 2af132fa-39c3-4d91-aee9-8de9cf340734
        # DisplayName : ARL8 Test User3
            # WindowsEmailAddress : arl8-test3@contoso.onmicrosoft.com
            # Sid : S-1-5-21-1244299112-3084700354-4037116849-31563230

        # [ ] msExchImmutableID, msExchMailboxGuid
        # [ ] msDS-ExternalDirectoryObjectID

    $exchUser = Get-MMLocalUser; $exchUser.Count # 15
    #$exchUser | select * | Out-GridView
    #$exchUser | select * | ft
    #$exchUser[6] | select UserPrincipalName, Guid, DisplayName, WindowsEmailAddress, *ID* | fl

        $exchUserOnly = $exchUser | Where-Object {$_.RecipientType -eq "User"}


        # UserPrincipalName : arl8-test3@contoso.fabrikam.com
        # Guid : d43358bc-7b22-495e-9f83-a5f93bbae2a1
        # DisplayName : ARL8 Test User3
            # WindowsEmailAddress : arl8-test3@contoso.fabrikam.com
            # Sid : S-1-5-21-2562156354-3420627960-2476036480-6601


        # ConvertTo-MMImmutableID -Guid "d43358bc-7b22-495e-9f83-a5f93bbae2a1"
        # vFgz1CJ7Xkmfg6X5O7rioQ==

    if ($FixUPN) {
        #$exoUser | foreach {
        # $FoundWithUPN = $_.UserPrincipalName -in $exchUser.UserPrincipalName; $FoundWithUPN
        #}
        #Compare-Object -ReferenceObject $exoUser -DifferenceObject $exchUser -Property UserPrincipalName -ExcludeDifferent -IncludeEqual # OK
        $result = Compare-Object -ReferenceObject $exoUserMailbox -DifferenceObject $exchUser -Property UserPrincipalName -PassThru
        $result = $result | Where-Object {$_.SideIndicator -eq "<=" }
        #$result | ft Name, DisplayName, RecipientType, RecipientTypeDetails, SideIndicator
            # 7bcf6e60-306d-42a7-8ed2-6f15a83f30c9 ARL8 Test User3 UserMailbox <= 00cabc6e-3c3d-4ea1-9308-bd8fd242505e
    }

    if ($FixTargetAddress) {
            #Write-Warning "Bug with PassThru?"
            #> PassThru --> sometimes wrong object reference?
        $result2 = Compare-Object -ReferenceObject $exchUserOnly -DifferenceObject $exoUserMailbox -Property UserPrincipalName -PassThru -IncludeEqual
        $result2 = $result2 | Where-Object {$_.SideIndicator -eq "==" }
        #$result2 | ft Name, DisplayName, RecipientType, RecipientTypeDetails, SideIndicator
            # ARL8 Test User9 ARL8 Test User9 User User ==

        $result3 = Compare-Object -ReferenceObject $result2 -DifferenceObject $exoUserMailbox -Property RecipientTypeDetails -PassThru
        $result3 = $result3 | Where-Object {$_.SideIndicator -eq "<=" }
        #$result3 | ft Name, DisplayName, RecipientType, RecipientTypeDetails, SideIndicator
            # ARL8 Test User3 ARL8 Test User3 MailUser RemoteUserMailbox <=
            # ARL8 Test User9 ARL8 Test User9 User User <=
        $result = $result3
    }

    if ($CountVariable) {
        # https://learn.microsoft.com/de-de/powershell/module/microsoft.powershell.core/about/about_scopes?view=powershell-7.5
        Set-Variable -Name $CountVariable -Value ($result.Count) -Scope 1
        #Set-Variable -Name $CountVariable -Value (($result| Measure).Count) -Scope 1
    }

    return $result
}
#Compare-MMSyncedUser -FixTargetAddress