I need to be able to use Powershell to "compare" the 2 JSON files below, and delete the registry value in the key named in the Name parameter that has been removed from JSON file 1.
I already know how to delete the keys/values from the registry, it's just identifying the one(s) that need deleting that is befuddling me.
Note: These files are used with a TFS Pipeline to apply registry changes to various servers in our organization. We currently have to manage key and value deletions manually and I'm trying to help automate this process.
JSON File 1:
{
"Name": "HKLM:\\SOFTWARE\\FirstNode\\SecondNode",
"Values": [
{
"ValueName": "Version",
"ValueDefault": "2",
"ValueType": "String",
"Tier": [
"app",
"log",
"svc",
"web"
]
},
{
"ValueName": "Limit",
"ValueDefault": "true",
"ValueType": "String",
"Tier": [
"web"
]
},
{
"ValueName": "ReportFlag",
"ValueDefault": "true",
"ValueType": "String",
"Tier": [
"web"
]
},
]
}
JSON File 2:
{
"Name": "HKLM:\\SOFTWARE\\FirstNode\\SecondNode",
"Values": [
{
"ValueName": "Version",
"ValueDefault": "2",
"ValueType": "String",
"Tier": [
"app",
"log",
"svc",
"web"
]
},
{
"ValueName": "ReportFlag",
"ValueDefault": "true",
"ValueType": "String",
"Tier": [
"web"
]
},
]
}
The following solution combines ConvertFrom-Json
, member-access enumeration, Compare-Object
and the .Where()
array method:
$objFromJson1 = ConvertFrom-Json (Get-Content -Raw file1.json)
$objFromJson2 = ConvertFrom-Json (Get-Content -Raw file2.json)
$removedProps, $addedProps = (
Compare-Object -PassThru $objFromJson1.Values.ValueName $objFromJson2.Values.ValueName
).Where({ $_.SideIndicator -eq '<=' }, 'Split')
With your sample input, $removedProps
now contains the string Limit
, i.e. the property no longer present (at the specified level) in file2.json
.
If potentially added properties are not of interest, you can simplify to $removedProps = ...
and remove the , 'Split'
argument from the .Where()
call.