ps/Modules/Alkami.DevOps.Inventory/Public/Get-AppSettingsInventory.ps1
2023-05-30 22:51:22 -07:00

101 lines
4.3 KiB
PowerShell

function Get-AppSettingsInventory {
<#
.SYNOPSIS
Returns an OrderedDictionary that Represents the App Settings Inventory.
#>
[CmdletBinding()]
Param()
$logLead = (Get-LogLeadName);
$providerStopWatch = [System.Diagnostics.StopWatch]::StartNew()
$appSettingsDictionary = New-Object System.Collections.Specialized.OrderedDictionary
$appSettingsDetails = New-Object System.Collections.Specialized.OrderedDictionary
[array]$configs = (Get-AppSettingsInventoryConfigPaths)
Write-Verbose "$logLead : Found these config paths for processing [$($configs -join ',')]"
Write-Verbose "$logLead : [$($providerStopWatch.Elapsed)] : Processing App Configs"
try {
foreach ($config in $configs) {
Write-Verbose "$logLead : Collecting inventory from $($config)"
# Filter out Choco configs which are not for the parent package
if ($config.DirectoryName -match "chocolatey") {
$packageName = ($config.Directory.FullName).Split("\") | Where-Object {$_.ToString().StartsWith("Alkami")} | Select-Object -First 1
if ($null -ne $packageName -and $config.FullName -notmatch $packageName -and $config.Name -notmatch "machine.config") {
Write-Verbose ("$logLead : [$($providerStopWatch.Elapsed)] : {0} does not match package name {1} and will be skipped" -f $config.Name, $packageName)
continue;
}
}
if ($config.DirectoryName -match "ORB") {
# Handle Legacy App Configs Which May Conflict on Name
$parentKey = ($config.DirectoryName.Split("\") | Select-Object -Last 1)
}
else {
# The machine.config and choco configs should be unique by name
$parentKey = $config.Name.Replace(".exe.config", "")
}
Write-Verbose "$logLead : found `$parentKey to be [$parentKey]"
$appSettingsDetails[$parentKey] = New-Object System.Collections.Specialized.OrderedDictionary
$appSettingsDetails[$parentKey]["FilePath"] = $config.FullName
[xml]$configXml = Read-XmlFile $config.FullName
if ($null -eq $configXml) {
Write-Verbose "$logLead : The Content of the Specified File [$($config.FullName)] is Null, or Could Not be Cast to XML, or could not be found"
$appSettingsDetails[$parentKey]["ERROR"] = "The Content of the Specified File is Null, or Could Not be Cast to XML, or could not be found"
} else {
$appSettings = $configXml.SelectSingleNode("//appSettings")
if ($null -eq $appSettings -or !$appSettings.HasChildNodes) {
Write-Verbose ("$logLead : [$($providerStopWatch.Elapsed)] : No AppSettings Found in {0}" -f $config.Name)
continue;
}
foreach ($appSetting in ($appSettings.ChildNodes | Where-Object {$_.NodeType -ne "Comment"})) {
try {
if ($appSetting.key -match "password" -or $appSetting.value -match "password") {
if ([string]::IsNullOrEmpty($appSetting.value)) {
$appSetting.Value = "EMPTY"
}
elseif ($appSetting.value -match "REPLACEME") {
$appSetting.Value = "UNSET"
}
else {
$appSetting.Value = "HIDDEN"
}
}
$appSettingsDetails[$parentKey][$appSetting.key] = $appSetting.value
} catch {
Write-Verbose "could not capture value from [$($appSetting.OuterXml)]"
}
}
}
}
}
catch {
Write-Verbose $_.Exception.ToString()
$appSettingsDetails["Error"] = $_.Exception.ToString()
}
Write-Verbose "$logLead : [$($providerStopWatch.Elapsed)] : Done Processing App Configs"
$appSettingsDictionary.Add("AppSettings", $appSettingsDetails)
Write-Verbose "$logLead : [$($providerStopWatch.Elapsed)] : Provider Complete"
$providerStopWatch.Stop()
return $appSettingsDictionary
}