Public/Get-ConnectorSyncSessionOperation.ps1

function Get-ConnectorSyncSessionOperation {
    [CmdletBinding()]
    param ()

    process {
        if ([String]::IsNullOrEmpty($Script:APIRoot)) {
            throw "Please connect first"
        }
        
        $GetExistingConnectorObjectsResult = Invoke-RestMethod -Uri "$($Script:APIRoot)data" -Headers (Get-EntraIDAccessTokenHeader -Profile $Script:AccessTokenProfile) -Method Get -SkipHttpErrorCheck
    
        if (!$GetExistingConnectorObjectsResult.IsSuccess) {
            throw "Unable to get connector objects"
        }

        $ExistingConnectorObjects = @{}
        if ($GetExistingConnectorObjectsResult.Data) {
            $GetExistingConnectorObjectsResult.Data | ForEach-Object {
                $ExistingConnectorObjects[$_.ExternalId] = $_
            }
        }
        Write-Verbose "Found $($ExistingConnectorObjects.Count) existing objects"

        $Script:SyncSessionObjects.GetEnumerator() | ForEach-Object {
            if ($ExistingConnectorObjects.ContainsKey($_.Key)) {
                Write-Debug "Object $($_.Key) already exists, checking for diff"
                $ExistingConnectorObject = $ExistingConnectorObjects[$_.Key]

                $ExistingJSON = ($ExistingConnectorObject.Data | ConvertTo-Json -Compress -Depth 20).ToCharArray()
                $SyncJSON = ($_.Value.Data | ConvertTo-Json -Depth 20 -Compress).ToCharArray()

                $IsDifferent = $ExistingJSON.Length -ne $SyncJSON.Length
                if (!$IsDifferent) {
                    $IsDifferent = (($SyncJSON | Sort-Object) -join "") -cne (($ExistingJSON | Sort-Object) -join "")
                }

                if ($IsDifferent) {
                    Build-ConnectorOperation -OperationType Update -ConnectorObject $_.Value -Id $ExistingConnectorObject.Id
                }
            }
            else {
                Build-ConnectorOperation -OperationType Create -ConnectorObject $_.Value
            }
        }

        $ExistingConnectorObjects.GetEnumerator() | ForEach-Object {
            if (-not $Script:SyncSessionObjects.ContainsKey($_.Key)) {
                Build-ConnectorOperation -OperationType Delete -ConnectorObject $_.Value -Id $_.Value.id
            }
        }
    }
}