@@ -69,8 +69,8 @@ function Compare-CIPPIntuneObject {
6969 [string ]$PropertyName
7070 )
7171 return ($PropertyName -like ' *@OData*' -or
72- $PropertyName -like ' #microsoft.graph*' -or
73- $excludeProps -contains $PropertyName )
72+ $PropertyName -like ' #microsoft.graph*' -or
73+ $excludeProps -contains $PropertyName )
7474 }
7575
7676 # Recursive function to compare objects deeply
@@ -83,7 +83,7 @@ function Compare-CIPPIntuneObject {
8383 $Object2 ,
8484
8585 [Parameter (Mandatory = $false )]
86- [string ]$PropertyPath = " "
86+ [string ]$PropertyPath = ' '
8787 )
8888
8989 # If both objects are null or empty, they're equal
@@ -94,20 +94,20 @@ function Compare-CIPPIntuneObject {
9494 # If one object is null but the other isn't, they're different
9595 if (($null -eq $Object1 -or $Object1 -eq ' ' ) -xor ($null -eq $Object2 -or $Object2 -eq ' ' )) {
9696 $result.Add ([PSCustomObject ]@ {
97- Property = $PropertyPath
98- ExpectedValue = if ($null -eq $Object1 ) { ' ' } else { $Object1 }
99- ReceivedValue = if ($null -eq $Object2 ) { ' ' } else { $Object2 }
100- })
97+ Property = $PropertyPath
98+ ExpectedValue = if ($null -eq $Object1 ) { ' ' } else { $Object1 }
99+ ReceivedValue = if ($null -eq $Object2 ) { ' ' } else { $Object2 }
100+ })
101101 return
102102 }
103103
104104 # If objects are of different types, they're different
105105 if ($Object1.GetType () -ne $Object2.GetType ()) {
106106 $result.Add ([PSCustomObject ]@ {
107- Property = $PropertyPath
108- ExpectedValue = $Object1
109- ReceivedValue = $Object2
110- })
107+ Property = $PropertyPath
108+ ExpectedValue = $Object1
109+ ReceivedValue = $Object2
110+ })
111111 return
112112 }
113113
@@ -122,25 +122,25 @@ function Compare-CIPPIntuneObject {
122122 $newPath = if ($PropertyPath ) { " $PropertyPath .$key " } else { $key }
123123
124124 if ($Object1.ContainsKey ($key ) -and $Object2.ContainsKey ($key )) {
125- Compare-ObjectsRecursively - Object1 $Object1 [$key ] - Object2 $Object2 [$key ] - PropertyPath $newPath
126- }
127- elseif ($Object1.ContainsKey ($key )) {
125+ # only run if both props are not null
126+ if ($Object1 [$key ] -and $Object2 [$key ]) {
127+ Compare-ObjectsRecursively - Object1 $Object1 [$key ] - Object2 $Object2 [$key ] - PropertyPath $newPath
128+ }
129+ } elseif ($Object1.ContainsKey ($key )) {
128130 $result.Add ([PSCustomObject ]@ {
129- Property = $newPath
130- ExpectedValue = $Object1 [$key ]
131- ReceivedValue = ' '
132- })
133- }
134- else {
131+ Property = $newPath
132+ ExpectedValue = $Object1 [$key ]
133+ ReceivedValue = ' '
134+ })
135+ } else {
135136 $result.Add ([PSCustomObject ]@ {
136- Property = $newPath
137- ExpectedValue = ' '
138- ReceivedValue = $Object2 [$key ]
139- })
137+ Property = $newPath
138+ ExpectedValue = ' '
139+ ReceivedValue = $Object2 [$key ]
140+ })
140141 }
141142 }
142- }
143- elseif ($Object1 -is [Array ] -or $Object1 -is [System.Collections.IList ]) {
143+ } elseif ($Object1 -is [Array ] -or $Object1 -is [System.Collections.IList ]) {
144144 # Compare arrays
145145 $maxLength = [Math ]::Max($Object1.Count , $Object2.Count )
146146
@@ -149,24 +149,21 @@ function Compare-CIPPIntuneObject {
149149
150150 if ($i -lt $Object1.Count -and $i -lt $Object2.Count ) {
151151 Compare-ObjectsRecursively - Object1 $Object1 [$i ] - Object2 $Object2 [$i ] - PropertyPath $newPath
152- }
153- elseif ($i -lt $Object1.Count ) {
152+ } elseif ($i -lt $Object1.Count ) {
154153 $result.Add ([PSCustomObject ]@ {
155- Property = $newPath
156- ExpectedValue = $Object1 [$i ]
157- ReceivedValue = ' '
158- })
159- }
160- else {
154+ Property = $newPath
155+ ExpectedValue = $Object1 [$i ]
156+ ReceivedValue = ' '
157+ })
158+ } else {
161159 $result.Add ([PSCustomObject ]@ {
162- Property = $newPath
163- ExpectedValue = ' '
164- ReceivedValue = $Object2 [$i ]
165- })
160+ Property = $newPath
161+ ExpectedValue = ' '
162+ ReceivedValue = $Object2 [$i ]
163+ })
166164 }
167165 }
168- }
169- elseif ($Object1 -is [PSCustomObject ] -or $Object1.PSObject.Properties.Count -gt 0 ) {
166+ } elseif ($Object1 -is [PSCustomObject ] -or $Object1.PSObject.Properties.Count -gt 0 ) {
170167 # Compare PSCustomObjects or objects with properties
171168 $allPropertyNames = @ (
172169 $Object1.PSObject.Properties | Select-Object - ExpandProperty Name
@@ -181,35 +178,35 @@ function Compare-CIPPIntuneObject {
181178 $prop2Exists = $Object2.PSObject.Properties.Name -contains $propName
182179
183180 if ($prop1Exists -and $prop2Exists ) {
184- Compare-ObjectsRecursively - Object1 $Object1 .$propName - Object2 $Object2 .$propName - PropertyPath $newPath
185- }
186- elseif ($prop1Exists ) {
181+ # only run if both props are not null
182+ if ($Object1 .$propName -and $Object2 .$propName ) {
183+ Compare-ObjectsRecursively - Object1 $Object1 .$propName - Object2 $Object2 .$propName - PropertyPath $newPath
184+ }
185+ } elseif ($prop1Exists ) {
187186 $result.Add ([PSCustomObject ]@ {
188- Property = $newPath
189- ExpectedValue = $Object1 .$propName
190- ReceivedValue = ' '
191- })
192- }
193- else {
187+ Property = $newPath
188+ ExpectedValue = $Object1 .$propName
189+ ReceivedValue = ' '
190+ })
191+ } else {
194192 $result.Add ([PSCustomObject ]@ {
195- Property = $newPath
196- ExpectedValue = ' '
197- ReceivedValue = $Object2 .$propName
198- })
193+ Property = $newPath
194+ ExpectedValue = ' '
195+ ReceivedValue = $Object2 .$propName
196+ })
199197 }
200198 }
201- }
202- else {
199+ } else {
203200 # Compare primitive values
204201 $val1 = $Object1.ToString ()
205202 $val2 = $Object2.ToString ()
206203
207204 if ($val1 -ne $val2 ) {
208205 $result.Add ([PSCustomObject ]@ {
209- Property = $PropertyPath
210- ExpectedValue = $val1
211- ReceivedValue = $val2
212- })
206+ Property = $PropertyPath
207+ ExpectedValue = $val1
208+ ReceivedValue = $val2
209+ })
213210 }
214211 }
215212 }
@@ -228,7 +225,10 @@ function Compare-CIPPIntuneObject {
228225 }
229226
230227 # Start the recursive comparison
231- Compare-ObjectsRecursively - Object1 $obj1 - Object2 $obj2
228+ # only do the compare if the objects are not null
229+ if ($obj1 -and $obj2 ) {
230+ Compare-ObjectsRecursively - Object1 $obj1 - Object2 $obj2
231+ }
232232
233233 # If no differences found, return null
234234 if ($result.Count -eq 0 ) {
0 commit comments