Public/Compare-IntuneBackupFile.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
function Compare-IntuneBackupFile() { <# .SYNOPSIS Compare two Intune Backup Files for changes. .DESCRIPTION Compare two Intune Backup Files for changes. .PARAMETER ReferenceFilePath Any Intune Backup file. .PARAMETER DifferenceFilePath Latest Intune Backup file, that matches the Intune Configuration (e.g. Device Compliance Policy, Device Configuration Profile or Device Management Script). .EXAMPLE Compare-IntuneBackupFile -ReferenceFilePath 'C:\temp\IntuneBackup\Device Configurations\Windows - Endpoint Protection.json' -DifferenceFilePath 'C:\temp\IntuneBackupLatest\Device Configurations\Windows - Endpoint Protection.json' .NOTES The DifferenceFilePath should point to the latest Intune Backup file, as it might contain new properties. #> param( [Parameter(Mandatory = $true)] [string]$ReferenceFilePath, [Parameter(Mandatory = $true)] [string]$DifferenceFilePath ) try { $backupFile = Get-Content -LiteralPath $ReferenceFilePath -ErrorAction Stop | ConvertFrom-Json } catch { Write-Error -Message "Could not retrieve ReferenceFile from the ReferenceFilePath location." -ErrorAction Stop } try { $latestBackupFile = Get-Content -LiteralPath $DifferenceFilePath -ErrorAction Stop | ConvertFrom-Json } catch { Write-Error -Message "Could not retrieve DifferenceFile from the DifferenceFilePath location." -ErrorAction Stop } function Invoke-FlattenBackupObject() { param( [Parameter (Mandatory = $true)] [PSCustomObject]$PSCustomObject, [Parameter (Mandatory = $false)] [string]$KeyName ) $flatObject = New-Object -TypeName PSObject $psCustomObject.PSObject.Properties | ForEach-Object { if ($null -eq $($_.Value)) { if ($KeyName) { $flatObject | Add-Member -NotePropertyName "$KeyName-$($_.Name)" -NotePropertyValue 'null' } else { $flatObject | Add-Member -NotePropertyName $_.Name -NotePropertyValue 'null' } } else { if ($($_.Value).GetType().Name -eq 'PSCustomObject') { Invoke-FlattenBackupObject -PSCustomObject $_.Value -KeyName $_.Name } else { if ($KeyName) { $flatObject | Add-Member -NotePropertyName "$KeyName-$($_.Name)" -NotePropertyValue $_.Value } else { $flatObject | Add-Member -NotePropertyName $_.Name -NotePropertyValue $_.Value } } } } return $flatObject } $flattenBackupArray = Invoke-FlattenBackupObject -PSCustomObject $backupFile $flattenLatestBackupArray = Invoke-FlattenBackupObject -PSCustomObject $latestBackupFile # Check if the JSON needs flattening, else it's just an object instead of an array. if ($flattenBackupArray -is [array]) { $flattenBackupObject = New-Object -TypeName PSObject for ($i=0; $i -le $flattenBackupArray.Length; $i++) { foreach ($property in $flattenBackupArray[$i].PSObject.Properties) { $flattenBackupObject | Add-Member -NotePropertyName $property.Name -NotePropertyValue $property.Value } } } else { $flattenBackupObject = $flattenBackupArray } # Check if the JSON needs flattening, else it's just an object instead of an array. if ($flattenLatestBackupArray -is [array]) { $flattenLatestBackupObject = New-Object -TypeName PSObject for ($i=0; $i -le $flattenLatestBackupArray.Length; $i++) { foreach ($property in $flattenLatestBackupArray[$i].PSObject.Properties) { $flattenLatestBackupObject | Add-Member -NotePropertyName $property.Name -NotePropertyValue $property.Value } } } else { $flattenLatestBackupObject = $flattenLatestBackupArray } $backupComparison = foreach ($latestBackupFileProperty in $flattenBackupObject.PSObject.Properties.Name) { $compareBackup = Compare-Object -ReferenceObject $flattenBackupObject -DifferenceObject $flattenLatestBackupObject -Property $latestBackupFileProperty if ($compareBackup.SideIndicator) { # If the property exists in both Intune Backup Files if ($null -ne $flattenBackupObject.$latestBackupFileProperty) { New-Object PSCustomObject -Property @{ 'Property' = $latestBackupFileProperty 'Old value' = $flattenBackupObject.$latestBackupFileProperty 'New value' = $flattenLatestBackupObject.$latestBackupFileProperty } } # If the property only exists in the latest Intune Backup File else { New-Object PSCustomObject -Property @{ 'Property' = $latestBackupFileProperty 'Old value' = $null 'New value' = $flattenLatestBackupObject.$latestBackupFileProperty } } } } return $backupComparison } |