1

I have this response in json format:

{
    "properties": {
        "basic": {
            "nodes_table": [
                {
                    "node": "node1.prod.local:80",
                    "state": "active",
                    "weight": 1
                },
                {
                    "node": "node2.prod.local:80",
                    "state": "disabled",
                    "weight": 1
                },
                {
                    "node": "node3.prod.local:80",
                    "state": "disabled",
                    "weight": 1
                },
                {
                    "node": "node4.prod.local:80",
                    "state": "disabled",
                    "weight": 1
                },
                {
                    "node": "node5.prod.local:80",
                    "state": "active",
                    "weight": 1
                }
            ]
        }
    }
}

What I am trying to do in my powershell script is to find out if given node(s) is available in the nodes_table and get their state. For example:

$nodes_table_hostnames = $GetNodesResponse.properties.basic.nodes_table.node
$nodes_table_status = $GetNodesResponse.properties.basic.nodes_table.state

if($nodes_table_hostnames -contains "node1.prod.local:80" -and $nodes_table_status -eq "active")
    {
        Write-Output  "Node matches and is Active"
    }

Problem: I am having an issue in getting the state of the "specific" node, so I want to check if "given" node is in the table and that node's state is active/disabled. How would I accomplish that in the script?

2 Answers 2

5
$Active = $GetNodesResponse.properties.basic.nodes_table |
            Where {$_.Node -eq "node1.prod.local:80" -and $_.state -eq "active"}
If ($Active) {Write-Output  "Node matches and is Active"}
Sign up to request clarification or add additional context in comments.

Comments

1

To check against multiple strings, use the -in operator

Where {$_.Node -in "node1.prod.local:80", "node5.prod.local:80" -and $_.state -eq "active"}

or you could use the -like or -match operators to match a pattern as in:

Where {$_.Node -like "node[15].prod.local:80" -and $_.state -eq "active"}

1 Comment

Thanks for the answer Bruce! Your solution definitely works.

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.