Files
Merge-DSCConfigurationData/Public/Merge-ConfigurationData.ps1
Torsten Brendgen e10ab48bb4 Creating Module
2026-04-21 13:02:51 +02:00

99 lines
5.5 KiB
PowerShell

function Merge-ConfigurationData {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[System.Collections.Hashtable]
$Template,
[Parameter(Mandatory=$true)]
[System.Collections.Hashtable]
$Deployment,
[Parameter(Mandatory=$false)]
[System.Collections.Hashtable]
$Output
)
if($Output -eq $null){
$Output = $Deployment
}
foreach($Property in $Template.GetEnumerator()){
if($Property.Value -is [System.Collections.Hashtable]){
Write-Verbose "Key [$($Property.Name)] is a Hashtable"
if($null -ne $Deployment.$($Property.Name)){
Write-Verbose "Key [$($Property.Name)] is present in Deployment Data"
$Output.($Property.Name) = Merge-ConfigurationData -Template $Template.$($Property.Name) -Deployment $Deployment.$($Property.Name) -Output $Output.$($Property.Name)
}else{
Write-Verbose "Key [$($Property.Name)] is not present in Deployment Data"
$Output.Add($($Property.Name),(Copy-ConfigurationDataValue -Value $Template.$($Property.Name)))
}
}elseif($Property.Value -is [System.Collections.Specialized.OrderedDictionary]){
Write-Verbose "Key [$($Property.Name)] is a Ordered Dictionary"
if($null -ne $Deployment.$($Property.Name)){
Write-Verbose "Key [$($Property.Name)] is present in Deployment Data"
$Output.($Property.Name) = Merge-ConfigurationData -Template $Template.$($Property.Name) -Deployment $Deployment.$($Property.Name) -Output $Output.$($Property.Name)
}else{
Write-Verbose "Key [$($Property.Name)] is not present in Deployment Data"
$Output.Add($($Property.Name),(Copy-ConfigurationDataValue -Value $Template.$($Property.Name)))
}
}elseif($Property.Value -is [System.Array]){
Write-Verbose "$($Property.Name) is ein Array"
Write-Verbose "Total Items in Template Array [$($Property.Value.Count)]"
Write-Verbose "Total Items in Deployment Array [$($Deployment.$($Property.Name).Count)]"
if($null -ne $Deployment.$($Property.Name)){
Write-Verbose "Array is defined in Deployment"
$TemplateItems = for($i=0;$i -lt $Property.Value.Count; $i++){
$SearchKeyNames = @(Get-ConfigurationDataArrayMergeKeyNames -ArrayName $Property.Name -Item $Property.Value[$i])
[PSCustomObject]@{
Value = $Property.Value[$i]
SearchKeyNames = $SearchKeyNames
IsWildcard = Test-ConfigurationDataItemWildcard -Item $Property.Value[$i] -KeyNames $SearchKeyNames
}
}
$TemplateItems = @($TemplateItems | Where-Object { -not $_.IsWildcard }) + @($TemplateItems | Where-Object { $_.IsWildcard })
foreach($TemplateItem in $TemplateItems){
$SearchKeyNames = @($TemplateItem.SearchKeyNames)
if($SearchKeyNames.Count -eq 0){
Write-Verbose "No matching name key found for item in [$($Property.Name)]"
$Output.$($Property.Name) += ,(Copy-ConfigurationDataValue -Value $TemplateItem.Value)
continue
}
if($TemplateItem.IsWildcard){
$MatchingDeploymentItems = @($Deployment.$($Property.Name) | Where-Object { Test-ConfigurationDataItemHasKeys -Item $_ -KeyNames $SearchKeyNames })
}else{
$MatchingDeploymentItems = @($Deployment.$($Property.Name) | Where-Object { Test-ConfigurationDataItemKeyMatch -Left $_ -Right $TemplateItem.Value -KeyNames $SearchKeyNames })
}
if($MatchingDeploymentItems.Count -gt 0){
foreach($DeploymentItem in $MatchingDeploymentItems){
$OutputItem = @($Output.$($Property.Name) | Where-Object { Test-ConfigurationDataItemKeyMatch -Left $_ -Right $DeploymentItem -KeyNames $SearchKeyNames })[0]
Merge-ConfigurationData -Template $TemplateItem.Value -Deployment $DeploymentItem -Output $OutputItem | Out-Null
}
}else{
if($TemplateItem.IsWildcard){
Write-Verbose "Wildcard item [$(Format-ConfigurationDataMergeKey -Item $TemplateItem.Value -KeyNames $SearchKeyNames)] in [$($Property.Name)] matched no deployment items"
}else{
Write-Verbose "Pair [$(Format-ConfigurationDataMergeKey -Item $TemplateItem.Value -KeyNames $SearchKeyNames)] not present"
$Output.$($Property.Name) += ,(Copy-ConfigurationDataValue -Value $TemplateItem.Value)
}
}
}
}else{
Write-Verbose "Array is not defined in Deployment"
$Output.$($Property.Name) = Copy-ConfigurationDataValue -Value $Template.$($Property.Name)
}
}else{
Write-Verbose "$($Property.Name) is a String or Integer Value"
if($null -eq $Deployment.$($Property.Name)){
$Output.Add($Property.Name,(Copy-ConfigurationDataValue -Value $Template.($Property.Name)))
}elseif($Deployment.$($Property.Name) -ne $Property.Value){
}
}
}
return $Output
}