Functions/Connection/Export-CdsConnection.ps1
<#
.SYNOPSIS Export instances collection to XML file with connection strings. #> function Protect-CdsPassword($pass) { $protectedPassword = ConvertTo-SecureString -string $pass -asplaintext -force; $protectedPasswordString = ConvertFrom-SecureString -SecureString $protectedPassword; return $protectedPasswordString; } function Export-CdsConnection { [CmdletBinding()] param ( [Parameter(Mandatory, ValueFromPipeline = $True)] [Object] $CdsConnection, [Parameter(Mandatory = $true)] [String] $FilePath ) begin { $StopWatch = [System.Diagnostics.Stopwatch]::StartNew(); Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters); } process { $securePwd = Protect-CdsPassword -pass $CdsConnection.UserPassword; $instanceLineTemplate = "`t`t<Instance Id=`"[Id]`" Name=`"[Name]`" UniqueName=`"[UniqueName]`" DisplayName=`"[DisplayName]`" ConnectionString=`"[ConnectionString]`" ` Url=`"[Url]`" AdminApiUrl=`"[AdminApiUrl]`" ApiUrl=`"[ApiUrl]`" TenantId=`"[TenantId]`" EnviromentId=`"[EnviromentId]`" />"; $output = New-Object -TypeName "System.Text.StringBuilder"; $output.AppendLine("<?xml version=`"1.0`" encoding=`"utf-8`"?>") | Out-Null; $output.AppendLine("<Connection>") | Out-Null; $output.AppendLine("`t<Name>$($CdsConnection.Name)</Name>") | Out-Null; $output.AppendLine("`t<AuthType>$($CdsConnection.AuthType)</AuthType>") | Out-Null; $output.AppendLine("`t<UserName>$($CdsConnection.UserName)</UserName>") | Out-Null; $output.AppendLine("`t<UserPassword>$securePwd</UserPassword>") | Out-Null; $output.AppendLine("`t<Region>$($CdsConnection.Region)</Region>") | Out-Null; $output.AppendLine("`t<ConnectionStringParameters>$($CdsConnection.ConnectionStringParameters)</ConnectionStringParameters>") | Out-Null; $output.AppendLine("`t<Instances>") | Out-Null; foreach ($instance in $instances) { $instanceLineContent = $instanceLineTemplate; $instance.ConnectionString = $instance | Out-CdsConnectionString; $instance.ConnectionString = $instance.ConnectionString.Replace($CdsConnection.UserPassword, $securePwd); $instanceLineContent = $instanceLineContent.Replace("[Id]", $instance.Id); $instanceLineContent = $instanceLineContent.Replace("[Name]", $instance.Name); $instanceLineContent = $instanceLineContent.Replace("[UniqueName]", $instance.UniqueName); $instanceLineContent = $instanceLineContent.Replace("[DisplayName]", $instance.DisplayName); $instanceLineContent = $instanceLineContent.Replace("[ConnectionString]", $instance.ConnectionString); $instanceLineContent = $instanceLineContent.Replace("[Url]", $instance.Url); $instanceLineContent = $instanceLineContent.Replace("[AdminApiUrl]", $instance.AdminApiUrl); $instanceLineContent = $instanceLineContent.Replace("[ApiUrl]", $instance.ApiUrl); $instanceLineContent = $instanceLineContent.Replace("[TenantId]", $instance.TenantId); $instanceLineContent = $instanceLineContent.Replace("[EnviromentId]", $instance.EnviromentId); $output.AppendLine($instanceLineContent) | Out-Null; } $output.AppendLine("`t</Instances>") | Out-Null; $output.AppendLine("</Connection>") | Out-Null; $output.ToString() | Out-File -FilePath $FilePath -Encoding utf8 -Force; } end { $StopWatch.Stop(); Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch; } } Export-ModuleMember -Function Export-CdsConnection -Alias *; |