67 lines
2.1 KiB
PowerShell
67 lines
2.1 KiB
PowerShell
|
function Get-AppSettingPrivateJson {
|
|||
|
<#
|
|||
|
.SYNOPSIS
|
|||
|
Returns an appSetting value from the specified file content
|
|||
|
|
|||
|
.DESCRIPTION
|
|||
|
Returns an appSetting value from the specified file content
|
|||
|
|
|||
|
.PARAMETER Key
|
|||
|
[string] The name of the key to get the value from, if it exists.
|
|||
|
|
|||
|
.PARAMETER FileContent
|
|||
|
[string] The file content to parse
|
|||
|
|
|||
|
.PARAMETER SuppressWarnings
|
|||
|
[switch] Suppress warnings about keys not found
|
|||
|
|
|||
|
.OUTPUTS
|
|||
|
Can return $null if key not found
|
|||
|
#>
|
|||
|
[CmdletBinding()]
|
|||
|
param (
|
|||
|
[string]$Key,
|
|||
|
[string]$FileContent,
|
|||
|
[switch]$SuppressWarnings
|
|||
|
)
|
|||
|
|
|||
|
$logLead = (Get-LogLeadName)
|
|||
|
|
|||
|
$json = (ConvertFrom-Json -InputObject $FileContent)
|
|||
|
|
|||
|
# appsettings.json has a magic trick for us
|
|||
|
# if I specify Parent:Child then I'm looking for something like
|
|||
|
# { "Parent": { "Child": value } }
|
|||
|
# if I specify Parent.Child then I'm looking for something like
|
|||
|
# { "Parent.Child": value }
|
|||
|
# So we want to be able to do a depth search by the colon'd parts
|
|||
|
$keyNodeList = $Key.Split(':')
|
|||
|
if (Test-IsCollectionNullOrEmpty $keyNodeList) {
|
|||
|
throw "$logLead : For config file [$FilePath] - No valid key specified - Can not continue. Key was [$Key]"
|
|||
|
}
|
|||
|
foreach($node in $keyNodeList) {
|
|||
|
if ([string]::IsNullOrWhiteSpace($node)) {
|
|||
|
throw "$logLead : For config file [$FilePath] - Found invalid node element [$node] in Key path [$Key]"
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
$lastNode = "<root>"
|
|||
|
$tempNode = $json
|
|||
|
foreach($node in $keyNodeList) {
|
|||
|
Write-Verbose "$logLead : Checking `$tempNode [$tempNode] for property `$node [$node]"
|
|||
|
|
|||
|
if (($node -eq $keyNodeList[-1]) -or ($null -eq $tempNode.$node)) {
|
|||
|
if ($null -eq $tempNode.$node) {
|
|||
|
if ($SuppressWarnings) {
|
|||
|
Write-Verbose "$logLead : Can not find [$node] of [$Key] for element under [$lastNode]. Returning `$null"
|
|||
|
} else {
|
|||
|
Write-Warning "$logLead : Can not find [$node] of [$Key] for element under [$lastNode]. Returning `$null"
|
|||
|
}
|
|||
|
}
|
|||
|
return $tempNode.$node
|
|||
|
}
|
|||
|
|
|||
|
$tempNode = $tempNode.$node
|
|||
|
$lastNode = $node
|
|||
|
}
|
|||
|
}
|