如何从powershell脚本中的列表中查询

Sha*_*boz 1 powershell

我有以下 json 格式的响应:

{
    "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
                }
            ]
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我试图在 powershell 脚本中执行的操作是查明给定节点在nodes_table 中是否可用并获取它们的状态。例如:

$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"
    }
Run Code Online (Sandbox Code Playgroud)

问题: 我在获取“特定”节点的状态时遇到问题,因此我想检查“给定”节点是否在表中以及该节点的状态是否处于活动/禁用状态。我将如何在脚本中实现这一点?

iRo*_*Ron 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"}
Run Code Online (Sandbox Code Playgroud)