Functions/Connection/Out-CdsConnectionStringParameter.ps1

<#
    .SYNOPSIS
    Extract parameter value from connectionstring.
#>

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

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

        [Parameter(Mandatory = $false)]
        [Switch]
        $RaiseErrorIfMising
    )
    begin {  
        $StopWatch = [System.Diagnostics.Stopwatch]::StartNew(); 
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters); 
    }    
    process {
        
        if (-not $ConnectionString.Contains($ParameterName)) {
            if ($RaiseErrorIfMising) {
                throw "Parameter '$ParameterName' is not in given connectionstring!";
            }
            return $null;
        }

        $startIndex = $ConnectionString.IndexOf($ParameterName);
        $stopIndex = $ConnectionString.IndexOf(";", $startIndex);
        if ($stopIndex -eq -1) {
            $stopIndex = $ConnectionString.Length - 1;
        }
        $value = $ConnectionString.Remove($stopIndex);
        $value = $value.Substring($startIndex);
        $value = $value.Replace("$ParameterName=", "");
        
        $value;
    }
    end {
        $StopWatch.Stop();
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }
}

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