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)]
        [ValidateSet("Office365", "OAuth", "AD", "Ifd", "ClientSecret")]
        [ArgumentCompleter( { Get-CdsAutTypes })]
        [string]
        $AuthType,
        
        [Parameter(Mandatory=$false)]
        [String]
        $UserName,

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

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

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

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

        [Parameter(Mandatory=$false)]
        [String]
        $CertificateThumbprint,
        
        [Parameter(Mandatory = $false)]
        [String]
        $AzDevOpsOrgName,

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

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

        # Set Credential object required authentications
        $credentials = Set-CdsCredentials -Login $Login -Password $Password;        
        
        # Load instances
        $Global:CdsContext = New-CdsContext; 
        
        # TODO : Connect-CdsAdmin with current credentials
        # TODO : Handle non admin case !!!!

        $instances = Get-CdsInstances;

        $cdsConnection = New-CdsConnection;   
        $cdsConnection.Name = $Name;     
        $cdsConnection.AuthType = $AuthType;
        $cdsConnection.UserName = $Login;
        $cdsConnection.UserPassword = $Password;
        
        $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;
        $cdsConnection = Get-CdsConnection -Name $Name;
        $cdsConnection;
    }
    end {
        $StopWatch.Stop();
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }    
}

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