使用 powershell 从 json 中提取值
Extract value from json using powershell
我有以下 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"
}
我需要从 topology 部分提取 memberRole、status 等值。
当我转到拓扑部分时
$ClusterDetails = $ClusterStatus.defaultReplicaSet.topology
$ClusterDetails 的值类似于(数据仅对 2 个服务器可见,但所有 3 个服务器都存在)
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...
来自 shell 如果我 select 喜欢
我可以看到单独的输出
PS C:\Windows\system32> $ClusterDetails.'X92SL224XXXX1XX:3306'
address : X92SL224XXXX1XX:3306
memberRole : PRIMARY
mode : R/W
readReplicas :
role : HA
status : ONLINE
version : 5.7.36
我需要帮助从 $ClusterDetails 获取上述各个服务器的数据,但不知道如何通过脚本获取该点部分。请告诉我该怎么做。
相当长的声明,但这应该有效:
$json.defaultReplicaSet.topology.PSObject.Properties.Value | Select-Object memberRole, status
# Results in:
memberRole status
---------- ------
SECONDARY ONLINE
PRIMARY ONLINE
SECONDARY (MISSING)
您可以访问 Object 的每个 属性 的 Values在 $json.defaultReplicaSet.topology
中访问 PSObject 属性。
值得注意的是,由于 Member-Access Enumeration.
,.PSObject.Properties.Value
可以一次枚举所有 属性 值
同样可以使用循环来完成,例如:
foreach($property in $json.defaultReplicaSet.topology.PSObject.Properties) {
[pscustomobject]@{
ThisProperty = $property.Name
memberRole = $property.Value.memberRole
status = $property.Value.status
}
}
我有以下 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"
}
我需要从 topology 部分提取 memberRole、status 等值。
当我转到拓扑部分时
$ClusterDetails = $ClusterStatus.defaultReplicaSet.topology
$ClusterDetails 的值类似于(数据仅对 2 个服务器可见,但所有 3 个服务器都存在)
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...
来自 shell 如果我 select 喜欢
我可以看到单独的输出PS C:\Windows\system32> $ClusterDetails.'X92SL224XXXX1XX:3306'
address : X92SL224XXXX1XX:3306
memberRole : PRIMARY
mode : R/W
readReplicas :
role : HA
status : ONLINE
version : 5.7.36
我需要帮助从 $ClusterDetails 获取上述各个服务器的数据,但不知道如何通过脚本获取该点部分。请告诉我该怎么做。
相当长的声明,但这应该有效:
$json.defaultReplicaSet.topology.PSObject.Properties.Value | Select-Object memberRole, status
# Results in:
memberRole status
---------- ------
SECONDARY ONLINE
PRIMARY ONLINE
SECONDARY (MISSING)
您可以访问 Object 的每个 属性 的 Values在 $json.defaultReplicaSet.topology
中访问 PSObject 属性。
值得注意的是,由于 Member-Access Enumeration.
,.PSObject.Properties.Value
可以一次枚举所有 属性 值
同样可以使用循环来完成,例如:
foreach($property in $json.defaultReplicaSet.topology.PSObject.Properties) {
[pscustomobject]@{
ThisProperty = $property.Name
memberRole = $property.Value.memberRole
status = $property.Value.status
}
}