Functions/Connection/Add-CdsConnection.ps1

<#
    .SYNOPSIS
    Create a connection file with all instances connection strings.
#>

function Add-CdsConnection {
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory=$true)]
        [String]
        $Name,

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

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

        [Parameter(Mandatory=$true)]
        [ValidateSet("Office365", "OAuth", "AD", "Ifd", "ClientSecret")]
        [ArgumentCompleter( { Get-CdsAutTypes })]
        [string]
        $AuthType,
        
        [Parameter(Mandatory = $false)]
        [String]
        [ArgumentCompleter( { Get-CdsRegions })]
        $Region,        
        
        [Parameter(Mandatory = $false)]
        [String]
        $ConnectionStringParameters,
        
        [Parameter(Mandatory = $false)]
        [Switch]
        $NoCredentialsInConnectionString
    )
    begin {   
        $StopWatch = [System.Diagnostics.Stopwatch]::StartNew();
        Write-Verbose "[$($MyInvocation.MyCommand.Name)] Function started";
        ($MyInvocation.MyCommand.Parameters ).Keys | ForEach-Object {
            $val = (Get-Variable -Name $_ -EA SilentlyContinue).Value
            if ( $val.length -gt 0 ) {
                Write-Verbose "[$($MyInvocation.MyCommand.Name)] Param : $($_) => $($val)"
            }
        }
    }    
    process {

        $credentials = Set-CdsCredentials -Login $Login -Password $Password;
             
        if (-not($Region)) {
            $Region = Resolve-CdsRegion -Credentials $credentials;
        }
        
        # Store current settings to context
        $Global:CdsContext = New-CdsContext;
        $Global:CdsContext.AuthType = $AuthType;
        $Global:CdsContext.UserName = $Login;
        $Global:CdsContext.UserPassword = $Password;
        $Global:CdsContext.Credentials = $credentials;
        $Global:CdsContext.Region = $Region;
        $Global:CdsContext.ConnectionStringParameters = $ConnectionStringParameters;
        
        $targetFilePath = [IO.Path]::Combine($Global:PowerCdsModuleFolderPath, "$Name.xml");
        
        $instances = Get-CdsInstances -Region $Region -AuthType $AuthType -Credentials $credentials;
        $instances | Out-CdsInstancesToFile -FilePath $targetFilePath;
    }
    end {
        $StopWatch.Stop();
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }    
}

Export-ModuleMember -Function Add-CdsConnection -Alias *;