Functions/Connection/Out-CdsConnectionString.ps1

<#
    .SYNOPSIS
Build Connection String from connection.
#>

function Out-CdsConnectionString {
    [CmdletBinding()]
    [OutputType([String])]
    param
    (
        [Parameter(Mandatory = $true, ValueFromPipeline)]
        $CdsInstance
    )
    begin {  
        $StopWatch = [System.Diagnostics.Stopwatch]::StartNew(); 
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters); 
    }    
    process {

        $cdsConnection = $Global:CdsContext.CurrentConnection;

        # TODO : Handle different auth type connection string formats
        # => https://docs.microsoft.com/fr-fr/powerapps/developer/common-data-service/xrm-tooling/use-connection-strings-xrm-tooling-connect
        $connectionString = "AuthType=$($cdsConnection.AuthType);";
        $connectionString += "Url=$($CdsInstance.Url);";

        if($cdsConnection.AuthType -eq "Office365" -or $cdsConnection.AuthType -eq "AD"-or $cdsConnection.AuthType -eq "Ifd")
        {
            $connectionString += "Username=$($cdsConnection.Username);";
            $connectionString += "Password=$($cdsConnection.Password);";
        }
        elseif($cdsConnection.AuthType -eq "ClientSecret")
        {
            $connectionString += "ClientId=$($cdsConnection.ApplicationId);";
            $connectionString += "ClientSecret=$($cdsConnection.ClientSecret);";
        }
        elseif($cdsConnection.AuthType -eq "Certificate")
        {
            $connectionString += "ClientId=$($cdsConnection.ApplicationId);";
            $connectionString += "thumbprint=$($cdsConnection.CertificateThumbprint);";
        }
        $connectionString;
    }
    end {
        $StopWatch.Stop();
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }
}

Export-ModuleMember -Function Out-CdsConnectionString -Alias *;