@@ -58,6 +58,13 @@ function Compare-CIPPIntuneObject {
5858 [int ]$MaxDepth = 20
5959 )
6060
61+ # Check for arrays at the start of every recursive call - this catches arrays at any nesting level
62+ $isObj1Array = $Object1 -is [Array ] -or $Object1 -is [System.Collections.IList ]
63+ $isObj2Array = $Object2 -is [Array ] -or $Object2 -is [System.Collections.IList ]
64+ if ($isObj1Array -or $isObj2Array ) {
65+ return
66+ }
67+
6168 if ($Depth -ge $MaxDepth ) {
6269 $result.Add ([PSCustomObject ]@ {
6370 Property = $PropertyPath
@@ -153,34 +160,78 @@ function Compare-CIPPIntuneObject {
153160 }
154161 }
155162 } elseif ($Object1 -is [PSCustomObject ] -or $Object1.PSObject.Properties.Count -gt 0 ) {
156- $allPropertyNames = @ (
157- $Object1.PSObject.Properties | Select-Object - ExpandProperty Name
158- $Object2.PSObject.Properties | Select-Object - ExpandProperty Name
159- ) | Select-Object - Unique
163+ # Skip comparison if either object is an array - arrays can't have custom properties set
164+ $isObj1Array = $Object1 -is [Array ] -or $Object1 -is [System.Collections.IList ]
165+ $isObj2Array = $Object2 -is [Array ] -or $Object2 -is [System.Collections.IList ]
166+ if ($isObj1Array -or $isObj2Array ) {
167+ return
168+ }
169+
170+ # Safely get property names - ensure objects are not arrays before accessing PSObject.Properties
171+ $allPropertyNames = @ ()
172+ try {
173+ if (-not ($Object1 -is [Array ] -or $Object1 -is [System.Collections.IList ])) {
174+ $allPropertyNames += $Object1.PSObject.Properties | Select-Object - ExpandProperty Name
175+ }
176+ if (-not ($Object2 -is [Array ] -or $Object2 -is [System.Collections.IList ])) {
177+ $allPropertyNames += $Object2.PSObject.Properties | Select-Object - ExpandProperty Name
178+ }
179+ $allPropertyNames = $allPropertyNames | Select-Object - Unique
180+ } catch {
181+ return
182+ }
160183
161184 foreach ($propName in $allPropertyNames ) {
162185 if (ShouldSkipProperty - PropertyName $propName ) { continue }
163186
164187 $newPath = if ($PropertyPath ) { " $PropertyPath .$propName " } else { $propName }
165- $prop1Exists = $Object1.PSObject.Properties.Name -contains $propName
166- $prop2Exists = $Object2.PSObject.Properties.Name -contains $propName
188+ # Safely check if properties exist - ensure objects are not arrays
189+ $prop1Exists = $false
190+ $prop2Exists = $false
191+ try {
192+ if (-not ($Object1 -is [Array ] -or $Object1 -is [System.Collections.IList ])) {
193+ $prop1Exists = $Object1.PSObject.Properties.Name -contains $propName
194+ }
195+ if (-not ($Object2 -is [Array ] -or $Object2 -is [System.Collections.IList ])) {
196+ $prop2Exists = $Object2.PSObject.Properties.Name -contains $propName
197+ }
198+ } catch {
199+ continue
200+ }
167201
168202 if ($prop1Exists -and $prop2Exists ) {
169- if ($Object1 .$propName -and $Object2 .$propName ) {
170- Compare-ObjectsRecursively - Object1 $Object1 .$propName - Object2 $Object2 .$propName - PropertyPath $newPath - Depth ($Depth + 1 ) - MaxDepth $MaxDepth
203+ try {
204+ # Double-check arrays before accessing properties
205+ if (($Object1 -is [Array ] -or $Object1 -is [System.Collections.IList ]) -or
206+ ($Object2 -is [Array ] -or $Object2 -is [System.Collections.IList ])) {
207+ continue
208+ }
209+ if ($Object1 .$propName -and $Object2 .$propName ) {
210+ Compare-ObjectsRecursively - Object1 $Object1 .$propName - Object2 $Object2 .$propName - PropertyPath $newPath - Depth ($Depth + 1 ) - MaxDepth $MaxDepth
211+ }
212+ } catch {
213+ throw
171214 }
172215 } elseif ($prop1Exists ) {
173- $result.Add ([PSCustomObject ]@ {
174- Property = $newPath
175- ExpectedValue = $Object1 .$propName
176- ReceivedValue = ' '
177- })
216+ try {
217+ $result.Add ([PSCustomObject ]@ {
218+ Property = $newPath
219+ ExpectedValue = $Object1 .$propName
220+ ReceivedValue = ' '
221+ })
222+ } catch {
223+ throw
224+ }
178225 } else {
179- $result.Add ([PSCustomObject ]@ {
180- Property = $newPath
181- ExpectedValue = ' '
182- ReceivedValue = $Object2 .$propName
183- })
226+ try {
227+ $result.Add ([PSCustomObject ]@ {
228+ Property = $newPath
229+ ExpectedValue = ' '
230+ ReceivedValue = $Object2 .$propName
231+ })
232+ } catch {
233+ throw
234+ }
184235 }
185236 }
186237 } else {
0 commit comments