Functions/Client/New-CdsClient.ps1

<#
    .SYNOPSIS
    Initialize CrmserviceClient instance.
#>

function New-CdsClient {
    [CmdletBinding()]
    [OutputType("Microsoft.Xrm.Tooling.Connector.CrmServiceClient")]
    param
    (
        # https://docs.microsoft.com/fr-fr/powerapps/developer/common-data-service/xrm-tooling/use-connection-strings-xrm-tooling-connect
        [Parameter(Mandatory = $true)]
        [String]
        $ConnectionString,

        [Parameter(Mandatory = $false)]
        [int]
        $MaxCrmConnectionTimeOutMinutes = 2
    )
    begin {   
        $StopWatch = [System.Diagnostics.Stopwatch]::StartNew(); 
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters); 
    }    
    process {
        # Optimizations
        [System.Net.ServicePointManager]::Expect100Continue = $false;
        [System.Net.ServicePointManager]::UseNagleAlgorithm = $false;
        [System.Net.ServicePointManager]::DefaultConnectionLimit = 1000;
        [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12;
    
        # Initialize CRM Client
        $CdsClient = Get-CrmConnection -ConnectionString $ConnectionString -MaxCrmConnectionTimeOutMinutes $MaxCrmConnectionTimeOutMinutes -ErrorAction Stop;
    
        if ($CdsClient.IsReady -eq $false) {
            throw $CdsClient.LastCrmError;
        }

        $Global:CdsClient = $CdsClient;

        $url = $CdsClient.ConnectedOrgPublishedEndpoints["WebApplication"];
        $userId = $CdsClient.GetMyCrmUserId();
        # Store current settings to context as connection could be initiated with a simple connectionstring and we need thoose parameters for admin operations
        $userName = $ConnectionString | Out-CdsConnectionStringParameter -ParameterName "Username";
        if(-not $userName)
        {
            $userName = $userId;
        }
        

        Write-HostAndLog -Message "Connected to $($CdsClient.ConnectedOrgFriendlyName)! [Url = $url | User : $userName]" -ForegroundColor Yellow;        
        $CdsClient;
    }
    end {
        $StopWatch.Stop();
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }    
}

Export-ModuleMember -Function New-CdsClient -Alias *;