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)]
        [String]
        $AzDevOpsOrgName,

        [Parameter(Mandatory = $false)]
        [String]
        $AzDevOpsProjectName,

        [Parameter(Mandatory = $false)]
        [String]
        $AzDevOpsToken
    )
    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 {

        # Set Credential object required authentications
        $credentials = Set-CdsCredentials -Login $Login -Password $Password;        
        
        # Retrieve region if missing
        if (-not($Region)) {
            if ($AuthType -ne "AD" -or $AuthType -ne "Ifd") {            
                $Region = Resolve-CdsRegion -Credentials $credentials;      
            }                  
        }

        # Load instances
        $Global:CdsContext = New-CdsContext;          
        $instances = Get-CdsInstances -Region $Region -AuthType $AuthType -Credentials $credentials;

        $cdsConnection = New-CdsConnection;   
        $cdsConnection.Name = $Name;     
        $cdsConnection.AuthType = $AuthType;
        $cdsConnection.UserName = $Login;
        $cdsConnection.UserPassword = $Password;
        $cdsConnection.Region = $Region;
        $cdsConnection.ConnectionStringParameters = $ConnectionStringParameters;
        $cdsConnection.Instances = $instances;
        $cdsConnection.Instances | ForEach-Object {
            $_.ParentConnection = $cdsConnection;
        }
        $cdsConnection.FilePath = [IO.Path]::Combine($Global:PowerCdsModuleFolderPath, "$Name.xml");

        $cdsConnection.DevOpsSettings.OrganizationName = $AzDevOpsOrgName;
        $cdsConnection.DevOpsSettings.ProjectName = $AzDevOpsProjectName;
        $cdsConnection.DevOpsSettings.Token = $AzDevOpsToken;

        $Global:CdsContext.CurrentConnection = $cdsConnection;
        
        $cdsConnection | Export-CdsConnection;
    }
    end {
        $StopWatch.Stop();
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }    
}

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