I am trying to retrieve a server status, and see if it have changed since last run, using Powershell. My server outputs a json hashtable. The idea is to import the data and compare it to previous data, if it has changed, save the new data to disk and do some "things".
It's easy enough to import the json to a PSObject, and it seems the common way to store it to disk is using Export-Clixml. But reading back the data with Import-Clixml does not give an identical PSObject. At least they have different TypeName values.
My code boils down to this:
$newserverstatus = Get-Content C:\status.json | ConvertFrom-Json
#for real I am piping the output from the status command into
#ConvertFrom-Json, in testing I read the content from a file
$oldserverstatus = Import-Clixml C:\oldstatus.xml
#read the saved information from disk
if ($oldserverstatus -ne $newserverstatus)
{
#do "things" using the data#
$newserverstatus | Export-Clixml C:\oldstatus.xml
#and finish with saving the updated status to disk
}
If this worked, I wouldn't be posting here. As mentioned I have noticed the objects have different TypeName values, but the data seems identical.
Main question is if there is a way to make a comparison of object data, without looking at the PSObject metadata?
I am not a programmer, so there is naturally many different better ways to do this, I just thought this was a possible solution, to import the new data into an object and to compare it with an object created from saved data.
The json data that I work with is this:
{
"clusterName": "CLUSTER11",
"defaultReplicaSet": {
"name": "default",
"primary": "192.168.2.30:3306",
"ssl": "DISABLED",
"status": "OK",
"statusText": "Cluster is ONLINE and can tolerate up to ONE failure.",
"topology": {
"192.168.2.30:3306": {
"address": "192.168.2.30:3306",
"mode": "R/W",
"readReplicas": {},
"replicationLag": null,
"role": "HA",
"status": "ONLINE",
"version": "8.0.19"
},
"192.168.2.31:3306": {
"address": "192.168.2.31:3306",
"mode": "R/O",
"readReplicas": {},
"replicationLag": null,
"role": "HA",
"status": "ONLINE",
"version": "8.0.19"
},
"192.168.3.139:3306": {
"address": "192.168.3.139:3306",
"mode": "R/O",
"readReplicas": {},
"replicationLag": null,
"role": "HA",
"status": "ONLINE",
"version": "8.0.19"
}
},
"topologyMode": "Single-Primary"
},
"groupInformationSourceMember": "192.168.2.30:3306"
}
I have also tried saving the data in json format with ConvertTo-Json, and using ConvertFrom-Json to read the old status, to no help. The thought of saving the new server status straight to file, and compare that file with an old copy has crossed my mind, but to me it seems like a horribly ugly way of doing it.