我正在从Powershell的Elasticsearch集群访问REST API。调用Get调用并接收数据不是问题。通常,我可以毫无问题地获取所需的数据,但是我正在为这种特殊的响应而苦苦挣扎。我搞砸了。
因此,调用是对API的获取,我的响应包含在变量($ r)中。
使用Postman时,JSON响应如下所示(仅显示响应中的前两个节点。实际响应中有10个节点):
{
"_nodes": {
"total": 10,
"successful": 10,
"failed": 0
},
"cluster_name": "helios",
"nodes": {
"LvrIjMcFR1ahhFOnPl-gsQ": {
"name": "log001",
"transport_address": "172.16.1.36:9300",
"host": "172.16.1.36",
"ip": "172.16.1.36",
"version": "6.2.4",
"build_hash": "ccec39f",
"total_indexing_buffer": 1601889894,
"roles": [
"master",
"data",
"ingest"
],
"attributes": {
"ml.machine_memory": "68718481408",
"ml.max_open_jobs": "20",
"box_type": "hot",
"ml.enabled": "true"
}
},
"yuW4R6yyRFKYdNDcrnEbXA": {
"name": "log008",
"transport_address": "172.16.1.63:9300",
"host": "172.16.1.63",
"ip": "172.16.1.63",
"version": "6.2.4",
"build_hash": "ccec39f",
"total_indexing_buffer": 1603639705,
"roles": [
"data",
"ingest"
],
"attributes": {
"ml.machine_memory": "68718481408",
"ml.max_open_jobs": "20",
"box_type": "warm",
"ml.enabled": "true"
}
}
}
}
我可以使用
$r.nodes
获取节点
LvrIjMcFR1ahhFOnPl-gsQ : @{name=log001; transport_address=172.16.1.36:9300; host=172.16.1.36; ip=172.16.1.36;
version=6.2.4; build_hash=ccec39f; total_indexing_buffer=1601889894; roles=System.Object[];
attributes=; settings=; os=; process=; jvm=; thread_pool=; transport=; http=;
plugins=System.Object[]; modules=System.Object[]; ingest=}
6QHP2i8rTXyw_z2jKqXnaA : @{name=log004; transport_address=172.16.1.68:9300; host=172.16.1.68; ip=172.16.1.68;
version=6.2.4; build_hash=ccec39f; total_indexing_buffer=1603639705; roles=System.Object[];
attributes=; settings=; os=; process=; jvm=; thread_pool=; transport=; http=;
plugins=System.Object[]; modules=System.Object[]; ingest=}
DAgkSb6gQQauZlMDaA-VCw : @{name=log002; transport_address=172.16.1.49:9300; host=172.16.1.49; ip=172.16.1.49;
version=6.2.4; build_hash=ccec39f; total_indexing_buffer=1601889894; roles=System.Object[];
attributes=; settings=; os=; process=; jvm=; thread_pool=; transport=; http=;
plugins=System.Object[]; modules=System.Object[]; ingest=}
aVu1OKHoRSinIZ-Ggf1eFg : @{name=log010; transport_address=172.16.1.60:9300; host=172.16.1.60; ip=172.16.1.60;
version=6.2.4; build_hash=ccec39f; total_indexing_buffer=1603639705; roles=System.Object[];
attributes=; settings=; os=; process=; jvm=; thread_pool=; transport=; http=;
plugins=System.Object[]; modules=System.Object[]; ingest=}
6i9_HsVjTH29r0_vnnIvHg : @{name=log006; transport_address=172.16.1.67:9300; host=172.16.1.67; ip=172.16.1.67;
version=6.2.4; build_hash=ccec39f; total_indexing_buffer=1603639705; roles=System.Object[];
attributes=; settings=; os=; process=; jvm=; thread_pool=; transport=; http=;
plugins=System.Object[]; modules=System.Object[]; ingest=}
QN7vh_UZTg2lgMJ_bZOcFw : @{name=log003; transport_address=172.16.1.35:9300; host=172.16.1.35; ip=172.16.1.35;
version=6.2.4; build_hash=ccec39f; total_indexing_buffer=1601889894; roles=System.Object[];
attributes=; settings=; os=; process=; jvm=; thread_pool=; transport=; http=;
plugins=System.Object[]; modules=System.Object[]; ingest=}
C6iDoIA7RkaM8-OXwG49TQ : @{name=log009; transport_address=172.16.1.62:9300; host=172.16.1.62; ip=172.16.1.62;
version=6.2.4; build_hash=ccec39f; total_indexing_buffer=1603639705; roles=System.Object[];
attributes=; settings=; os=; process=; jvm=; thread_pool=; transport=; http=;
plugins=System.Object[]; modules=System.Object[]; ingest=}
gEJx2P5uRmClV7R_oOyo2w : @{name=log005; transport_address=172.16.1.73:9300; host=172.16.1.73; ip=172.16.1.73;
version=6.2.4; build_hash=ccec39f; total_indexing_buffer=1603639705; roles=System.Object[];
attributes=; settings=; os=; process=; jvm=; thread_pool=; transport=; http=;
plugins=System.Object[]; modules=System.Object[]; ingest=}
yuW4R6yyRFKYdNDcrnEbXA : @{name=log008; transport_address=172.16.1.63:9300; host=172.16.1.63; ip=172.16.1.63;
version=6.2.4; build_hash=ccec39f; total_indexing_buffer=1603639705; roles=System.Object[];
attributes=; settings=; os=; process=; jvm=; thread_pool=; transport=; http=;
plugins=System.Object[]; modules=System.Object[]; ingest=}
HLQquAaoQaedjtwvATf2cg : @{name=log007; transport_address=172.16.1.69:9300; host=172.16.1.69; ip=172.16.1.69;
version=6.2.4; build_hash=ccec39f; total_indexing_buffer=1603639705; roles=System.Object[];
attributes=; settings=; os=; process=; jvm=; thread_pool=; transport=; http=;
plugins=System.Object[]; modules=System.Object[]; ingest=}
然后我希望做
$r.nodes[0]
获得第一个节点,但这与之前的$ r.nodes完全相同。
$r.nodes."LvrIjMcFR1ahhFOnPl-gsQ"
然后给我有关该节点的信息:
name : log001
transport_address : 172.16.1.36:9300
host : 172.16.1.36
ip : 172.16.1.36
version : 6.2.4
build_hash : ccec39f
total_indexing_buffer : 1601889894
roles : {master, data, ingest}
attributes : @{ml.machine_memory=68718481408; ml.max_open_jobs=20; box_type=hot; ml.enabled=true}
但是由于我事先不知道节点的ID,如何在节点上获得某种迭代器?
我正在使用Powershell 5.1
答案 0 :(得分:2)
您可以使用PSObject属性分解对象。然后管道直到获得价值
$II = @"
{
"_nodes": {
"total": 10,
"successful": 10,
"failed": 0
},
"cluster_name": "helios",
"nodes": {
"LvrIjMcFR1ahhFOnPl-gsQ": {
"name": "log001",
"transport_address": "172.16.1.36:9300",
"host": "172.16.1.36",
"ip": "172.16.1.36",
"version": "6.2.4",
"build_hash": "ccec39f",
"total_indexing_buffer": 1601889894,
"roles": [
"master",
"data",
"ingest"
],
"attributes": {
"ml.machine_memory": "68718481408",
"ml.max_open_jobs": "20",
"box_type": "hot",
"ml.enabled": "true"
}
},
"yuW4R6yyRFKYdNDcrnEbXA": {
"name": "log008",
"transport_address": "172.16.1.63:9300",
"host": "172.16.1.63",
"ip": "172.16.1.63",
"version": "6.2.4",
"build_hash": "ccec39f",
"total_indexing_buffer": 1603639705,
"roles": [
"data",
"ingest"
],
"attributes": {
"ml.machine_memory": "68718481408",
"ml.max_open_jobs": "20",
"box_type": "warm",
"ml.enabled": "true"
}
}
}
}
"@ | ConvertFrom-Json
$II.nodes.PSobject.Properties | %{
$_.Value
}
答案 1 :(得分:0)
这里的问题是将json转换为PSCustomObject。枚举键的最简单方法可能是执行以下操作:
$NodeNames = $r.Nodes | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name
foreach ($NodeName in $NodeNames) {
$r.Nodes.$Nodename
}
如果要使用PowerShell Core 6.1,还可以执行以下操作:
$r = (Invoke-WebRequest 'http://url').Content | ConvertFrom-Json -AsHashTable
然后您可以使用以下命令枚举按键:
foreach ($node in $r.Nodes.GetEnumerator()) {
#code here
}