Looking to merge two hash tables with nested tables and/or arrays. I found posting Merging hashtables in Powershell: how? which lead me to crated the following function modifying sonjz code found within the post and it works well with non nested objects however when it comes to nested tables and arrays within the parent tables it fails to merge correctly.
how would one go about updating this code to output the merging of hash tables with nested tables an/or arrays if exists?
Function Merge-HashTables
{
[CmdletBinding(ConfirmImpact = 'Low',
PositionalBinding = $true)]
[OutputType([hashtable])]
param
(
[Parameter(Mandatory = $true,
Position = 0)]
[ValidateNotNullOrEmpty()]
[hashtable]$BaseTable,
[Parameter(Mandatory = $true,
Position = 1)]
[ValidateNotNullOrEmpty()]
[hashtable]$NewTable
)
Begin
{
[hashtable]$Base = $BaseTable.Clone()
}
Process
{
foreach ($Key in $NewTable.Keys)
{
if ($Base.ContainsKey($Key))
{
$Base.Remove($Key)
}
}
Write-Output ($Base + $NewTable)
}
}