ps/Modules/Alkami.PowerShell.Configuration/Public/Get-AppSetting.ps1
2023-05-30 22:51:22 -07:00

120 lines
4.1 KiB
PowerShell

function Get-AppSetting {
<#
.SYNOPSIS
Returns an appSetting value from the specified file. Filepath defaults to the 64 bit machine config.
.DESCRIPTION
Returns an appSetting value from the specified file for the given key.
Will default to the global 64 bit machine.config file if no config file value specified.
Can connect to remote computers as well.
.PARAMETER key
[string] The name of the key to get the value from, if it exists.
.PARAMETER filePath
[string] The location of the specific config file to check in
.PARAMETER ComputerName
[string] The remote computer to connect to. Defaults to localhost.
.PARAMETER SuppressWarnings
[switch] Suppress warnings about keys not found
.OUTPUTS
Can return $null if key not found
#>
[CmdletBinding(DefaultParameterSetName = 'ByFile')]
param (
[Parameter(ParameterSetName = 'ByFile', Mandatory = $true, Position = 0)]
[Parameter(ParameterSetName = 'ByXml', Mandatory = $true, Position = 0)]
[Alias("appSettingKey")]
[string]$Key,
[Parameter(ParameterSetName = 'ByFile', Mandatory = $false)]
[Alias("Path")]
[string]$FilePath = (Get-DotNetConfigPath -use64Bit $true),
[Parameter(ParameterSetName = 'ByFile', Mandatory = $false)]
[string]$ComputerName = "localhost",
[Parameter(ParameterSetName = 'ByXml', Mandatory = $false)]
[Xml]$XmlDocument = $null,
[Parameter(ParameterSetName = 'ByFile', Mandatory = $false)]
[Parameter(ParameterSetName = 'ByXml', Mandatory = $false)]
[switch]$SuppressWarnings
)
$logLead = (Get-LogLeadName)
if ([string]::IsNullOrWhiteSpace($Key)) {
throw "$logLead : Provided Key value [$Key] must be provided and non-empty/non-whitespace"
}
if ([string]::IsNullOrWhiteSpace($ComputerName)) {
Write-Warning "$logLead : ComputerName is null or whitespace, assigning 'localhost' to ComputerName"
$ComputerName = 'localhost'
}
if ($PSCmdlet.ParameterSetName -eq 'ByFile') {
if(!(Compare-StringToLocalMachineIdentifiers -StringToCheck $ComputerName)) {
$FilePath = (Get-UncPath -filePath $FilePath -ComputerName $ComputerName)
}
}
if (!(Test-Path -Path $FilePath)) {
Write-Warning "$logLead : Could not find a file at [$FilePath]. Execution cannot continue."
return ## exit early
}
Write-Verbose "$logLead : Reading Config file at [$FilePath]";
$isLikelyXml = ($null -ne $XmlDocument) #becomes true if we gave an xmldoc
$isLikelyJson = $false
$fileContent = $null
# If we started off with xmldoc already, no need to do this, we can just crack on
if (!$isLikelyXml) {
$fileContent = Get-Content -Path $FilePath -Raw
$firstCharacter = $fileContent[0]
if ($firstCharacter -eq '<') {
$isLikelyXml = $true
}
if (($firstCharacter -eq '{') -or ($firstCharacter -eq '[')) {
$isLikelyJson = $true
}
}
if ($isLikelyXml) {
if ($null -eq $XmlDocument) {
$XmlDocument = [xml]$fileContent
}
if($null -eq $XmlDocument) {
throw "$logLead : Config at [$FilePath] expected to be xml but could not be parsed as xml."
}
Write-Verbose "$logLead : Ensuring configuration root node exists...";
if(!$XmlDocument.configuration){
throw "$logLead : How does $FilePath not have a root <configuration> element??"
}
return Get-AppSettingPrivateXml -Key $Key -FileContent $XmlDocument -SuppressWarnings:$SuppressWarnings
}
if ($isLikelyJson) {
try {
# See if we can parse this as a json object
(ConvertFrom-Json -InputObject $fileContent) | Out-Null
} catch {
Write-Host "$logLead : $($_.Exception.Message)"
throw "$logLead : Config at [$FilePath] expected to be json but could not be parsed as json."
}
return Get-AppSettingPrivateJson -Key $Key -FileContent $fileContent -SuppressWarnings:$SuppressWarnings
}
throw "$logLead : Could not parse this file [$FilePath]"
}