ps/Modules/Alkami.PowerShell.Common/Public/Get-ConnectionString.ps1

50 lines
1.8 KiB
PowerShell
Raw Normal View History

2023-05-30 22:51:22 -07:00
function Get-ConnectionString {
<#
.SYNOPSIS
Returns a ConnectionString by name from the specified config file. Filepath defaults to the 64 bit machine config.
#>
param (
[Parameter(Mandatory = $true)]
[string]$name,
[Parameter(Mandatory = $false)]
[Alias("Path")]
[string]$filePath = (Get-DotNetConfigPath -use64Bit $true),
[Parameter(Mandatory = $false)]
[string]$ComputerName = "localhost"
)
$logLead = (Get-LogLeadName);
# If a computername was provided, modify the filepath to be a UNC path.
if((![string]::IsNullOrWhiteSpace($ComputerName)) -and ($ComputerName -ne "localhost")) {
$filePath = (Get-UncPath -filePath $filePath -ComputerName $ComputerName);
}
if (!(Test-Path -PathType Leaf -Path $filePath)) {
Write-Warning ("$logLead : Could not find a file at {0}. Execution cannot continue" -f $filePath)
return $null
}
$xml = Read-XMLFile $filePath
Write-Verbose "$logLead : Looking for appSettings Node"
[array]$connectionStringNode = $xml.SelectNodes("//connectionStrings")
if (($null -eq $connectionStringNode) -or ($connectionStringNode.Count -eq 0)) {
Write-Warning ("$logLead : Could not Find appSettings Node in {0}" -f $filePath)
return $null
}
Write-Verbose ("$logLead : Looking for Child Nodes with Key {0}" -f $name)
[array]$targetNodes = $connectionStringNode.ChildNodes | Where-Object {$_.Name -eq $name}
if (($null -eq $targetNodes) -or ($targetNodes.Count -eq 0)) {
Write-Warning ("$logLead : Could not Find ConnectionString Add Node with Name {0} in {1}" -f $name, $filePath)
return $null
} elseif ($targetNodes.Count -gt 1) {
Write-Warning ("$logLead : Found {0} connection strings with name {1}. This is incorrect, and only the first will be returned" -f $targetNodes.Count, $name)
}
return ($targetNodes | Select-Object -First 1).connectionString;
}