Functions/Connection/Get-CdsConnection.ps1

<#
    .SYNOPSIS
    Explore connection files.
#>

function Get-CdsConnection {
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $false)]
        [Switch]
        $ListAvailable
    )
    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;
        if ($ListAvailable) {
            $instanceNameParameterAttribute.Mandatory = $false;
        }
        else {        
            $instanceNameParameterAttribute.Mandatory = (-not ($PSBoundParameters.ContainsKey('Name')));            
        }
        $instanceNameParameterAttributes.Add($instanceNameParameterAttribute);        
        $instanceNameParameter = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameter("InstanceName", [string], $instanceNameParameterAttributes);
        $dynamicParameters.Add("InstanceName", $instanceNameParameter);
              
        return $dynamicParameters;    
    }
    begin {   
        $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");
            $fileContent = [xml] [IO.File]::ReadAllText($targetFilePath);
            if ($instanceName) {
                $fileContent.Instances.Instance | Where-Object -Property "Name" -EQ -Value $instanceName;
            }
            else {
                $fileContent.Instances.Instance | Sort-Object -Property Name;
            }
        }
        else {                          
            $files = Get-ChildItem -Path $Global:PowerCdsModuleFolderPath -Include "*.xml" -Recurse;
            $connections = @();
            foreach ($file in $files) {
                $fileContent = [xml] [IO.File]::ReadAllText($file.FullName);
                foreach ($instance in ($fileContent.Instances.Instance | Sort-Object -Property Name)) {
                    $instance | Add-Member -MemberType NoteProperty -Name File -Value $file.FullName;                        
                    $instance | Add-Member -MemberType NoteProperty -Name FileName -Value $file.Name.ToString().Replace(".xml", "");
                    $connections += $instance;
                }
            }
            $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);
    $fileContent = [xml] [IO.File]::ReadAllText($targetFilePath);
    $validInstanceNames = @();
    $fileContent.Instances.Instance | Sort-Object -Property Name | ForEach-Object {
        $validInstanceNames += "`"$($_.Name)`"";
    }           
    return $validInstanceNames | Where-Object { $_ -like "*$wordToComplete*"};
}