Functions/Connection/Out-CdsInstancesToFile.ps1

<#
    .SYNOPSIS
    Export instances collection to XML file with connection strings.
#>

function Out-CdsInstancesToFile {
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory, ValueFromPipeline = $True)]
        [Object[]]
        $InputObject,

        [Parameter(Mandatory = $true)]
        [String]
        $FilePath,
        
        [Switch]
        $NoCredentialsInConnectionString
    )
    begin {   
        $instances = @();

        $StopWatch = [System.Diagnostics.Stopwatch]::StartNew(); 
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters); 
    }    
    process {
        $instances += $InputObject;
    }
    end {
        $instanceLineTemplate = "<Instance Name=`"[Name]`" UniqueName=`"[UniqueName]`" DisplayName=`"[DisplayName]`" ConnectionString=`"[ConnectionString]`" />";

        $output = New-Object -TypeName "System.Text.StringBuilder";
        $output.AppendLine("<?xml version=`"1.0`" encoding=`"utf-8`"?>") | Out-Null;
        $output.AppendLine("<Instances>") | Out-Null;
        foreach ($instance in $instances) {   
            $instanceLineContent = $instanceLineTemplate;

            $instance.ConnectionString = $instance | Out-CdsConnectionString;

            $instanceLineContent = $instanceLineContent.Replace("[Id]", $instance.Id);
            $instanceLineContent = $instanceLineContent.Replace("[Name]", $instance.Name);
            $instanceLineContent = $instanceLineContent.Replace("[UniqueName]", $instance.UniqueName);
            $instanceLineContent = $instanceLineContent.Replace("[DisplayName]", $instance.FriendlyName);
            $instanceLineContent = $instanceLineContent.Replace("[ConnectionString]", $instance.ConnectionString);

            $output.AppendLine($instanceLineContent) | Out-Null;
        }
        $output.AppendLine("</Instances>") | Out-Null;
        $output.ToString() | Out-File -FilePath $FilePath -Encoding utf8 -Force;

        $StopWatch.Stop();
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }    
}

Export-ModuleMember -Function Out-CdsInstancesToFile -Alias *;