src/Connection/Out-XrmConnectionStringParameter.ps1

<#
    .SYNOPSIS
    Extract parameter value from connectionstring.
    
    .DESCRIPTION
    Output connection string parameter value.
    The connection string is read as the Dataverse SDK reads it: names without case or spaces, their aliases (AppId or ApplicationId for ClientId, ServiceUri for Url, CertificateThumbprint for Thumbprint...) and quoted values.

    .PARAMETER ConnectionString
    Connection string.

    .PARAMETER ParameterName
    Parameter name (e.g. Url, AuthType, ClientId).

    .PARAMETER RaiseErrorIfMising
    If parameter is not found, throw an exception. (Default: $null is returned)

    .OUTPUTS
    System.String. The parameter value, or $null when the connection string does not have the parameter.

    .EXAMPLE
    $url = $connectionString | Out-XrmConnectionStringParameter -ParameterName "Url";
    $clientId = Out-XrmConnectionStringParameter -ConnectionString $connectionString -ParameterName "ClientId" -RaiseErrorIfMising;

    .LINK
    https://github.com/AymericM78/PowerDataOps/blob/main/documentation/commands/Out-XrmConnectionStringParameter.md
#>

function Out-XrmConnectionStringParameter {
    [CmdletBinding()]
    [OutputType([String])]
    param
    (
        [Parameter(Mandatory = $true, ValueFromPipeline)]
        [String]
        $ConnectionString,

        [Parameter(Mandatory = $true)]
        [String]
        $ParameterName,

        [Parameter(Mandatory = $false)]
        [Switch]
        $RaiseErrorIfMising
    )
    begin {  
        $StopWatch = [System.Diagnostics.Stopwatch]::StartNew(); 
        Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters); 
    }    
    process {

        # Parsed as the Dataverse SDK reads it (M3): names without case or spaces, their aliases (AppId for ClientId...), quoted values
        $parameters = ConvertTo-XrmConnectionStringMapInternal -RawConnectionString $ConnectionString;
        $names = Get-XrmConnectionStringNamesInternal -Name $ParameterName;
        $value = Get-XrmConnectionStringValueInternal -Parameters $parameters -Names $names;

        if ($null -eq $value) {
            if ($RaiseErrorIfMising) {
                throw "Parameter '$ParameterName' is not in given connectionstring!";
            }
            return $null;
        }

        $value;
    }
    end {
        $StopWatch.Stop();
        Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }
}

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