Functions/Connection/Get-CdsConnection.ps1

<#
    .SYNOPSIS
    Explore connection files.
#>

function Get-CdsConnection {
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $false)]
        [Switch]
        $ListAvailable,

        [Parameter(Mandatory = $false)]
        [switch]
        $DoNotDecryptPassword        
    )
    dynamicparam {
        $dynamicParameters = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameterDictionary;
             
        # Define Dynamic Parameter : Name
        $nameParameterAttributes = New-Object -TypeName System.Collections.ObjectModel.Collection[System.Attribute];
        $nameParameterAttribute = New-Object System.Management.Automation.ParameterAttribute;
        $nameParameterAttribute.Mandatory = (-not $ListAvailable);        
        $nameParameterAttributes.Add($nameParameterAttribute);        
        $nameParameter = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameter("Name", [string], $nameParameterAttributes);
        $dynamicParameters.Add("Name", $nameParameter);
                                 
        # Define Dynamic Parameter : InstanceName
        $instanceNameParameterAttributes = New-Object -TypeName System.Collections.ObjectModel.Collection[System.Attribute];
        $instanceNameParameterAttribute = New-Object System.Management.Automation.ParameterAttribute;
        $instanceNameParameterAttributes.Add($instanceNameParameterAttribute);        
        $instanceNameParameter = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameter("InstanceName", [string], $instanceNameParameterAttributes);
        $dynamicParameters.Add("InstanceName", $instanceNameParameter);
              
        return $dynamicParameters;    
    }
    begin {   
        $Global:CdsContext = New-CdsContext;
        $StopWatch = [System.Diagnostics.Stopwatch]::StartNew(); 
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters); 
    }    
    process {
        $name = $PSBoundParameters.Name;
        $instanceName = $PSBoundParameters.InstanceName;
               
        if ($name) {  
            $targetFilePath = [IO.Path]::Combine($Global:PowerCdsModuleFolderPath, "$name.xml");
            
            $cdsConnection = Import-CdsConnection -FilePath $targetFilePath -DoNotDecryptPassword:$DoNotDecryptPassword;
            $Global:CdsContext.CurrentConnection = $cdsConnection;

            if ($instanceName) {
                $cdsInstance = $cdsConnection.Instances | Where-Object -Property "Name" -EQ -Value $instanceName;
                $Global:CdsContext.CurrentInstance = $cdsInstance;
                $cdsInstance;                
            }
            else {
                if ($ListAvailable) {                    
                    $cdsConnection.Instances;
                }
                $cdsConnection;
            }
        }
        else {                          
            $files = Get-ChildItem -Path $Global:PowerCdsModuleFolderPath -Include "*.xml" -Recurse;
            $connections = @();
            foreach ($file in $files) {
                $cdsConnection = Import-CdsConnection -FilePath $file.FullName -DoNotDecryptPassword:$DoNotDecryptPassword;
                $connections += $cdsConnection;
            }
            $connections;
        } 
    }
    end {
        $StopWatch.Stop();
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }    
}

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

Register-ArgumentCompleter -CommandName Get-CdsConnection -ParameterName "Name" -ScriptBlock {

    param($CommandName, $ParameterName, $WordToComplete, $CommandAst, $FakeBoundParameters)

    $fileNames = @(Get-ChildItem -Path $Global:PowerCdsModuleFolderPath -Include "*.xml" -Recurse | Select-Object -Property BaseName);
    $validNames = @();
    $fileNames | Sort-Object -Property BaseName  | ForEach-Object {
        $validNames += "`"$($_.BaseName)`"";
    }
    return $validNames | Where-Object { $_ -like "*$wordToComplete*" };
}

Register-ArgumentCompleter -CommandName Get-CdsConnection -ParameterName "InstanceName" -ScriptBlock {

    param($CommandName, $ParameterName, $WordToComplete, $CommandAst, $FakeBoundParameters)

    if (-not ($fakeBoundParameters.ContainsKey("Name"))) {
        return @();
    }
    $fileName = "$($fakeBoundParameters.Name).xml"; 
    $targetFilePath = [IO.Path]::Combine($Global:PowerCdsModuleFolderPath, $fileName);
    $cdsConnection = Import-CdsConnection -FilePath $targetFilePath;

    $validInstanceNames = @();
    $cdsConnection.Instances | Sort-Object -Property Name | ForEach-Object {
        $validInstanceNames += "`"$($_.Name)`"";
    }           
    return $validInstanceNames | Where-Object { $_ -like "*$wordToComplete*" };
}