Compare selected scalar fields across two trusted JSON array snapshots.
How to use it
.\Compare-SocJsonBaseline.ps1 -BaselinePath 'C:\Lab\baseline.json' -CurrentPath 'C:\Lab\current.json' -Key Name -Properties State,StartMode
Platform and permissions
Read permission to two local JSON files. No Windows-only cmdlets are used by this comparison script.
What changes / what is read
Reads inputs and returns differences; never rewrites the snapshots.
Scope and interpretation
Only selected scalar fields are compared. Keys are case-insensitive; values are case-sensitive. Duplicate/empty keys and missing fields fail rather than producing a clean result. Trust and collection provenance of each baseline are separate concerns.
Practice task
Create two fictional service arrays with one added, removed and modified entry; verify the reported changes and a duplicate-key rejection.
Reference
Microsoft documentation. Review the documentation for your installed version and use only an authorised learning or investigation environment.
This is personal educational material. Review the complete code and your signing policy before use. PowerShell code is displayed and downloaded as text; WordPress does not run it. Collection caps limit rows/files, not every provider’s wall-clock execution time.
#requires -Version 5.1
[CmdletBinding()]
param([Parameter(Mandatory)][string]$BaselinePath,[Parameter(Mandatory)][string]$CurrentPath,[Parameter(Mandatory)][string]$Key,[Parameter(Mandatory)][string[]]$Properties,[ValidateRange(1,50)][int]$MaxMB=10)
function Read-SocRows {
param([string]$Path)
$file=Get-Item -LiteralPath $Path -ErrorAction Stop
if($file.PSIsContainer -or $file.Length -gt ($MaxMB*1MB) -or ($file.Attributes -band [IO.FileAttributes]::ReparsePoint)){throw 'Input must be a regular non-reparse JSON file within the size limit.'}
$text=Get-Content -LiteralPath $file.FullName -Raw -ErrorAction Stop
if(!$text.TrimStart().StartsWith('[')){throw 'Each input must be a JSON array of flat objects.'}
return @(ConvertFrom-Json -InputObject $text -ErrorAction Stop)
}
function New-SocIndex {
param([object[]]$Rows)
$index=[System.Collections.Generic.Dictionary[string,object]]::new([StringComparer]::OrdinalIgnoreCase)
foreach($row in $Rows){
if($null -eq $row -or $row -isnot [pscustomobject]){throw 'Each row must be a JSON object.'}
foreach($name in (@($Key)+$Properties)){
if(!$row.PSObject.Properties[$name]){throw ('Missing property: '+$name)}
$value=$row.$name
if($null -ne $value -and $value -isnot [string] -and $value -isnot [bool] -and $value -isnot [ValueType]){throw 'Only scalar comparison fields are supported.'}
}
$id=[string]$row.$Key
if([string]::IsNullOrWhiteSpace($id) -or $index.ContainsKey($id)){throw 'Empty or duplicate keys are not accepted (case-insensitive matching).'}
$index.Add($id,$row)
}
return ,$index
}
$baseRows=@(Read-SocRows $BaselinePath);$currentRows=@(Read-SocRows $CurrentPath)
$base=New-SocIndex $baseRows;$current=New-SocIndex $currentRows
$changes=New-Object 'System.Collections.Generic.List[object]'
foreach($id in $base.Keys){
if(!$current.ContainsKey($id)){$changes.Add([pscustomobject]@{Key=$id;Change='Removed';Property=$null;Before=$base[$id];After=$null});continue}
foreach($name in $Properties){
$before=ConvertTo-Json -InputObject $base[$id].$name -Compress -Depth 5
$after=ConvertTo-Json -InputObject $current[$id].$name -Compress -Depth 5
if($before -cne $after){$changes.Add([pscustomobject]@{Key=$id;Change='Changed';Property=$name;Before=$base[$id].$name;After=$current[$id].$name})}
}
}
foreach($id in $current.Keys){if(!$base.ContainsKey($id)){$changes.Add([pscustomobject]@{Key=$id;Change='Added';Property=$null;Before=$null;After=$current[$id]})}}
[pscustomobject]@{ComparedUtc=[datetime]::UtcNow;BaselineRows=$baseRows.Count;CurrentRows=$currentRows.Count;Key=$Key;Properties=$Properties;Changes=$changes.ToArray();Note='Compares only selected scalar fields, using case-insensitive keys and case-sensitive serialized values. A baseline must have a trusted origin; differences are investigation leads, not automatic malicious findings. No input file is changed.'}
Example code is displayed as text. Review it and test in your own lab before use.