Public/Connect-Connector.ps1

function Connect-Connector {
    [CmdletBinding(DefaultParameterSetName = 'Default')]
    param (
        [Parameter(Mandatory = $true)]
        [ValidatePattern('^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$')]
        $ConnectorId,

        [Parameter(Mandatory = $false)]
        $AccessTokenProfile = "default",

        [Parameter(Mandatory = $false)]
        [ValidateSet("production", "development")]
        $Instance = "production",
        
        [Parameter(Mandatory = $true, ParameterSetName = 'Internal')]
        [ValidatePattern('^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$')]
        [string] $TenantId,
        
        [Parameter(Mandatory = $true, ParameterSetName = 'Development')]
        [ValidateScript({ $_ -match '^https?://.+$' })]
        [string] $ApiRoot
    )

    $Script:APIRoot = $null
    $Script:AccessTokenProfile = $null
    $Script:ConnectorConfiguration = $null

    if (!(Get-EntraIDAccessTokenProfile -Profile $AccessTokenProfile)) {
        throw "Access token profile '$AccessTokenProfile' not found. Please create it using New-EntraIDAccessTokenProfile."
    }

    $Script:AccessTokenProfile = $AccessTokenProfile
    # $_AccessTokenPayload = Get-EntraIDAccessToken -Profile $AccessTokenProfile | Get-EntraIDAccessTokenPayload

    if ($PSCmdlet.ParameterSetName -eq "Development") {
        Write-Verbose "Using development instance with API root: $ApiRoot"
        if($ApiRoot -notlike "*sync/connectors/$ConnectorId/") {
            throw "When using the Development parameter set, the ApiRoot must end with 'sync/connectors/{ConnectorId}/'"
        }
        $Script:APIRoot = $ApiRoot
    }
    elseif ($PSCmdlet.ParameterSetName -eq "Internal") {
        if ($Instance -eq "development") {
            $authsettings = Get-ConnectorAuthSettings -Internal -Development
            Write-Verbose "Using internal development instance"
            $Script:APIRoot = "https://$($authsettings.fqdn)/tenants/$($TenantId)/sync/connectors/$ConnectorId/"
        }
        else {
            $authsettings = Get-ConnectorAuthSettings -Internal
            Write-Verbose "Using internal production instance"
            $Script:APIRoot = "https://$($authsettings.fqdn)/tenants/$($TenantId)/sync/connectors/$ConnectorId/"
        }
    }
    else {
        if ($Instance -eq "development") {
            $authsettings = Get-ConnectorAuthSettings -Development
            Write-Verbose "Using development instance"
            $Script:APIRoot = "https://$($authsettings.fqdn)/sync/connectors/$ConnectorId/"
        }
        else {
            $authsettings = Get-ConnectorAuthSettings
            Write-Verbose "Using production instance"
            $Script:APIRoot = "https://$($authsettings.fqdn)/sync/connectors/$ConnectorId/"
        }
    }

    # Try to get connector configuration
    $Script:ConnectorConfiguration = Get-ConnectorConfiguration
}