Public/Add-ConnectorSyncSessionObject.ps1

<#
.SYNOPSIS
    Adds an object to the current connector sync session.

.DESCRIPTION
    Adds a connector object (hashtable) to the active sync session's object collection.
    The object must contain ExternalId, ObjectType, and Data keys. Objects are keyed by
    ExternalId, so adding an object with a duplicate ExternalId will overwrite the previous entry.

.PARAMETER Object
    A hashtable representing the connector object to add. Must contain:
    - ExternalId (string): A unique identifier for the object in the source system.
    - ObjectType (string): The type/category of the object.
    - Data (hashtable): The object's attribute data.

.EXAMPLE
    $obj = Build-ConnectorObject -ExternalId "user-123" -ObjectType "User" -Data @{ DisplayName = "Jane Doe" }
    Add-ConnectorSyncSessionObject -Object $obj

.EXAMPLE
    Build-ConnectorObject -ExternalId "user-123" -ObjectType "User" -Data @{ DisplayName = "Jane Doe" } | Add-ConnectorSyncSessionObject

.NOTES
    A sync session must be started with Start-ConnectorSyncSession before calling this function.
#>

function Add-ConnectorSyncSessionObject {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [System.Collections.Hashtable] $Object
    )

    process {
        if ($null -eq $Script:SyncSessionObjects) {
            throw "No active sync session. Please start a sync session before adding objects."
        }

        if ([String]::IsNullOrEmpty($Object.ExternalId)) {
            Write-Warning "Object must contain an 'ExternalId' key - skipping object."
            return
        }

        if ([String]::IsNullOrEmpty($Object.ObjectType)) {
            Write-Warning "Object must contain an 'ObjectType' key - skipping object."
            return
        }

        if ($null -eq $Object.Data -or $Object.Data.GetType().Name -notin "OrderedHashtable", "Hashtable") {
            Write-Warning "Object must contain a 'Data' key - skipping object."
            return
        }

        Write-Verbose "Adding object with ExternalId '$($Object.ExternalId)' and ObjectType '$($Object.ObjectType)' to sync session"
        $Script:SyncSessionObjects[$Object.ExternalId] = $Object
    }
}