0

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.

Lars Olof
  • 1
  • 1
  • 1
    You can't compare it that way. It always return $false because both objects have different pointers (pscustomobject is a reference type). Either you compare the plain json document (see: https://stackoverflow.com/a/58189249/13699968 ; you'll also find much more information about the "reference") or you have to compare the required properties one by one. If you need help for the latter, we need more infos about what you want to compare. – swbbl Jan 04 '21 at 12:07
  • Thank you. The links you provided seem to have a solution for me, I'll look further into it shortly. At least I know I won't have to tread further down the path I was on. If your comment was an answer, I would accept it. When I need to look into specific properties, I will get back in another question if get stuck. – Lars Olof Jan 04 '21 at 13:02
  • Why convert JSON -> CliXml? JSON is already easily stored on disk... – Mathias R. Jessen Jan 04 '21 at 15:53

0 Answers0