1

I am having the below json

{
    "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"
}

I need to extract value like memberRole, status from the topology section.

when I go to the topology part

$ClusterDetails = $ClusterStatus.defaultReplicaSet.topology

the $ClusterDetails have value like (data visible only for 2 servers but all 3 servers are present)

PS C:\Windows\system32> $ClusterDetails

X92SL224XXXX1XX:3306                                                                                                   X92SL224XXXX2XX:3306                              
--------------------                                                                                                   --------------------                              
@{address=X92SL224XXXX1XX:3306; memberRole=SECONDARY; mode=R/O; readReplicas=; role=HA; status=ONLINE; version=5.7.36} @{address=X92SL224XXXX2XX:3306; memberRole=PRIM...

from shell I am able to see the individual output if i select like

PS C:\Windows\system32> $ClusterDetails.'X92SL224XXXX1XX:3306'


address      : X92SL224XXXX1XX:3306
memberRole   : PRIMARY
mode         : R/W
readReplicas : 
role         : HA
status       : ONLINE
version      : 5.7.36

I need help to fetch the data from $ClusterDetails for individual servers like above but not getting how to get that dot part via script. please let me know how to do that.

1
  • Something like $ClusterDetails.'X92SL224XXXX1XX:3306'.memberRole should work from script as well. Please clarify what exactly does not work. An minimal reproducible example would be helpful. Commented May 24, 2022 at 15:23

1 Answer 1

1

Quite a long statement but this should work:

$json.defaultReplicaSet.topology.PSObject.Properties.Value | Select-Object memberRole, status

# Results in:

memberRole status
---------- ------
SECONDARY  ONLINE
PRIMARY    ONLINE
SECONDARY  (MISSING)

You can access the Values of each Property of the Object in $json.defaultReplicaSet.topology accessing the PSObject Properties.

It's worth noting that .PSObject.Properties.Value works to enumerate all Property Values at once due to Member-Access Enumeration.

The same can be accomplished using a loop, for example:

foreach($property in $json.defaultReplicaSet.topology.PSObject.Properties) {
    [pscustomobject]@{
        ThisProperty = $property.Name
        memberRole   = $property.Value.memberRole
        status       = $property.Value.status
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.