I'm working on a recursive function to compare two JSON objects and ran into a strange issue. My recursion works fine... until I assign the result of the recursive call to a variable. If I just make the function call, the recursion works, and all members are iterated. When the assignment is present, recursion breaks and only the first level of the object is iterated.
(Line with arrow indicates recursive call)
Is there some reason this doesn't work?
function Compare-Objects() {
[CmdletBinding()]
Param(
$Current,
$Expected
)
foreach ($Property in $Expected.PSObject.Properties) {
$Property.Name + " : " + $Property.TypeNameOfValue
if ($Property.TypeNameOfValue -like "*PSCustomObject") {
-> $Match = Compare-Objects -Current $Current.$($Property.Name) -Expected $Expected.$($Property.Name)
}
elseif ($Property.TypeNameOfValue -like "*Object[[][]]"){
# Compare-Arrays -Current $Current.$($Property.Name) -Expected $Expected.$($Property.Name)
}
else {
if ($Property.Value -like "*enums*") {
$Expected.$($Property.Name) = Invoke-Expression -Command $Expected.$($Property.Name)
}
if ($Current.$($Property.Name) -ne $Expected.$($Property.Name)) {
return $false
}
}
}
return $true
}