I'm trying to merge two objects in PowerShell, but I seem to be stuck in a recursive loop. Arrays should be appended from source to target, and the rest should be replaced. We should also go as far down in the structure as we can to avoid replacing data when not needed. Can anyone spot something wrong? It seems to be line 27 that's causing it.
function Merge-Objects {
Param(
# Do not append to array. Replace it instead.
[bool]$replaceArrays = $false,
# Source object (the data that will be merged)
[Parameter(Mandatory = $true)]
[object]$source,
# Target object (where the data will be merged)
[Parameter(Mandatory = $true)]
[object]$target
)
ForEach ($child in $source.PSObject.Properties)
{
# Set basic variables
$childName = $child.Name
$childValue = $child.Value
$childType = $source.$childName.GetType().Name
# If the item is a table, iterate further
If ($childType -eq 'Hashtable' -and `
$target.${childName}.GetType().Name -eq 'Hashtable')
{
$target.$childName = Merge-Objects -source $childValue -target $target.$childName
}
# If the object is an array, append or replace depending on parameters
ElseIf ($childValue -is [array] -and `
$target.$childName -is [array] -and `
$replaceArrays -eq $false)
{
$target.$childName = $source.$childName + $target.$childName
}
# If none of the above, then replace/add the item
Else
{
$target.$childName = $childValue
}
}
Return $target
}
$object1 = [PSCustomObject]@{
value1 = "test1"
value3 = @{
test2 = "test3"
array = @( "asd3" )
}
array = @(
"hejsan"
@{
asd1 = "value1"
anotherarray = @( "test1" )
}
)
}
$object2 = [PSCustomObject]@{
value1 = "value3"
value2 = "test2"
value3 = @{
test1 = "test1"
test2 = "test2"
array = @( "asd2" )
}
array = @(
"svejsan"
@{
asd2 = "value2"
anotherarray = @( "test1" )
}
)
}
# Merge the objects recursively
Merge-Objects -source $object1 -target $object2
I tried to solve this recursive loop by not using the function recursively, but I can't make it work either. The array parts seems to work at least.
All the other code snippets found on StackOverflow had all had the problem of not recursing downwards and appending to arrays as far down as possible (in my example the array in value3 is not appended).
merge-objects. At the moment your recursive call iterates over the properties of a hashtable, not the keys. The problematic property isSyncRootwhich itself is a hashtable., so that makes another call toMerge-Objectswith the syncroot values, and that hashtable itself has a syncroot property so your recursion goes into a tailspin that only ends when you run out of recursion juice...