Private/ConnectionReferences.ps1

function Invoke-SetConnectionReferences {
    Param(
        [Microsoft.Xrm.Tooling.Connector.CrmServiceClient] $CRMConn,
        [string] [Parameter(Mandatory = $true)] $SolutionName,
        [string] [Parameter(Mandatory = $true)] $EnvId,
        [bool] [Parameter(Mandatory = $true)] $FailOnError,
        [bool] [Parameter(Mandatory = $false)] $RunLocally = $false
    )

    try {
        Write-PPDOMessage -Message "Checking for Connections References in $SolutionName" -RunLocally $RunLocally
        $solutionId = (Get-CrmRecords -conn $CRMConn -EntityLogicalName "solution" -FilterAttribute "uniquename" -FilterOperator "eq" -FilterValue $SolutionName -Fields "solutionid" -TopCount 1).CrmRecords[0].solutionid
        $connRefs = (Get-CrmRecords -conn $CRMConn -EntityLogicalName "connectionreference" -FilterAttribute "solutionid" -FilterOperator "eq" -FilterValue $solutionid -Fields "connectionreferencelogicalname", "connectionid", "connectorid", "connectionreferenceid").CrmRecords
        foreach ($connReference in $connRefs) {
            try {
                $connectionType = $connReference.connectorid.Replace("/providers/Microsoft.PowerApps/apis/", "")
                Write-PPDOMessage -Message "Found Connection Reference $($connReference.connectionreferencelogicalname), searching for related Connection" -RunLocally $RunLocally

                $connection = Get-AdminPowerAppConnection -EnvironmentName $EnvId | Select-Object -ExpandProperty "Statuses" -Property "ConnectionName", "DisplayName", "ConnectorName", "CreatedBy", "CreatedTime" | Where-Object { ($_.status -eq "Connected") -and ($_.ConnectorName -eq $connectionType) } | Sort-Object -Property CreatedTime

                if (!$connection) {
                    Throw "Unable to find a Connection in this environment of type $connectionType."
                }
                Write-PPDOMessage -Message "Found Connection: $connection" -RunLocally $RunLocally
                
                Write-PPDOMessage -Message "Setting Connection Reference to use $($connection[0].DisplayName)" -Type "command" -RunLocally $RunLocally
                Set-CrmRecord -conn $CRMConn -EntityLogicalName "connectionreference" -Id $connReference.connectionreferenceid -Fields @{"connectionid" = $connection[0].ConnectionName }
            }
            catch {
                Write-PPDOMessage -Message $_ -type "error" -RunLocally $RunLocally
                $message = "An error occurred for Connection Reference $($connReference.connectionreferencelogicalname)."
                if ($FailOnError) {
                    Throw $message
                }
                else {
                    Write-PPDOMessage -Message $message -type "warning" -LogWarning $true -RunLocally $RunLocally
                }
            }
        }
    }
    catch {
        if ($FailOnError) {
            Throw $_
        }
        else {
            Write-PPDOMessage -Message $_ -type "warning" -LogWarning $true -RunLocally $RunLocally
        }
    }
}