Home > Software engineering >  Extract data from json using powershell
Extract data from json using powershell

Time:05-25

I executing below command using strem from powershell and I am getting response in form of json using the below code

$session = New-SSHSession -ComputerName $S_1 -Credential $cred

$Strem = New-SSHShellStream -SSHSession $Session

        $cmd_4 = $Strem.WriteLine("shell.connect('USER@X92SL224XXX2XX:3306')")
        sleep -Seconds 5
        $Strem.read()
        $pass = $Strem.WriteLine("password")
        sleep -Seconds 5
        $Strem.read()
        $cmd_5 = $Strem.WriteLine("var cluster = dba.getCluster('IBDCluster')")
        sleep -Seconds 5
        $Strem.read()
        $cmd_6 = $Strem.WriteLine("cluster.status()")
        sleep -Seconds 5
        $ClusterStatus = $Strem.read()  
[DBG]: PS C:\Windows\system32>> 
cluster.status()
{
    "clusterName": "IBDCluster", 
    "defaultReplicaSet": {
        "name": "default", 
        "primary": "X92SL224XXX2XX:3306", 
        "ssl": "REQUIRED", 
        "status": "OK_NO_TOLERANCE", 
        "statusText": "Cluster is NOT tolerant to any failures. 1 member is not active.", 
        "topology": {
            "X92SL224XXX1XX:3306": {
                "address": "X92SL224XXXXXXX:3306", 
                "memberRole": "SECONDARY", 
                "mode": "R/O", 
                "readReplicas": {}, 
                "role": "HA", 
                "status": "ONLINE", 
                "version": "5.7.36"
            }, 
            "X92SL224XXX2XX:3306": {
                "address": "X92SL224XXX2XX:3306", 
                "memberRole": "PRIMARY", 
                "mode": "R/W", 
                "readReplicas": {}, 
                "role": "HA", 
                "status": "ONLINE", 
                "version": "5.7.36"
            }, 
            "X92SL224XXXX3XX:3306": {
                "address": "X92SL224XXX3XX:3306", 
                "instanceErrors": [
                    "ERROR: group_replication has stopped with an error."
                ], 
                "memberRole": "SECONDARY", 
                "memberState": "ERROR", 
                "mode": "R/O", 
                "readReplicas": {}, 
                "role": "HA", 
                "status": "(MISSING)", 
                "version": "5.7.36"
            }
        }, 
        "topologyMode": "Single-Primary"
    }, 
    "groupInformationSourceMember": "X92SL224XXXXXXX:3306"
}
[48;5;254m[38;5;23m My[0m[48;5;254m[38;5;166mSQL [0m[48;5;237m[38;5;15m X92SL224QDBA2DB:3306 ssl [0m[48;5;221m[38;5;0m JS [0m[48;5;0m> [0m

I need to fetch all the address, memberRole, memberState from the above data.

I was trying to convert from Json but getting error like

ConvertFrom-Json : Invalid JSON primitive: cluster.status.

Please let me know how to get the data from above

CodePudding user response:

It looks like you simply need to remove the cluster.status() line that precedes the JSON text in the multi-line string that $Strem.Read() returns, as well as the extra line that follows it:

$ClusterStatus = $Strem.Read() -replace '^. |. $' | ConvertFrom-Json

Note: -replace '^. |. $' removes the first and last line from the input string, without also trying to remove a newline that follows / precedes them; however, these newlines are incidental whitespace that doesn't affect the operation of ConvertFrom-Json.

  • Related