ps/Modules/Cole.PowerShell.Developer/Public/Get-AllAppSettings.ps1

69 lines
2.3 KiB
PowerShell
Raw Permalink Normal View History

2023-05-30 22:51:22 -07:00
function Get-AllAppSettings {
[CmdletBinding()]
[OutputType([object[]])]
param (
[Parameter(Mandatory = $false)]
[Alias("Path")]
[string[]]$FilePaths = @(),
[Parameter(Mandatory = $false)]
[Alias("Computer")]
[string]$ComputerName = "localhost",
[Parameter(Mandatory = $false)]
[Alias('Key')]
[Alias('Name')]
[string]$KeyName
)
$logLead = (Get-LogLeadName)
$normalizedPaths = @()
if (!(Any $FilePaths)) {
$FilePaths = @()
$FilePaths += (Get-DotNetConfigPath -use64Bit $true)
$FilePaths += (Get-DotNetConfigPath -use64Bit $false)
}
foreach($path in $FilePaths) {
$normalizedPaths += (Get-NormalizedPath -FilePath $path -ComputerName $ComputerName)
}
$returnValues = @()
foreach($path in $normalizedPaths) {
if (!(Test-Path $path)) {
Write-Warning "$logLead : Could not connect to [$path], continuing"
continue
}
$content = (Get-Content $path -Raw)
$content0 = $content[0]
if ([string]::IsNullOrWhiteSpace($content0)) {
Write-Warning "$logLead : No content found in [$path], continuing"
continue
}
if ($content0 -eq '<') {
$content = ([xml]$content).configuration.appSettings.add
foreach($appSetting in $content) {
if ([string]::IsNullOrWhiteSpace($KeyName) -or (![string]::IsNullOrWhiteSpace($KeyName) -and ($appSetting.Key -match $KeyName))) {
$returnValues += @{ Key = $appSetting.Key; Value = $appSetting.Value; Path = $path; }
}
}
} elseif (@('[','{') -contains $content0) {
$content = (Get-Json -JsonBlob $content)
$appSettings = Get-FlattenedAppsettingJson $content
foreach($appSetting in $appSettings) {
if ([string]::IsNullOrWhiteSpace($KeyName) -or (![string]::IsNullOrWhiteSpace($KeyName) -and ($appSetting.Key -match $KeyName))) {
$returnValues += @{ Key = $appSetting.Key; Value = $appSetting.Value; Path = $path; }
}
}
} else {
Write-Warning "$logLead : Do not know how to process [$path] for character-0 of [$content0], continuing"
continue
}
}
return $returnValues
}