Reputation: 27
I'm accessing a REST API from an Elasticsearch Cluster from Powershell. Invoking the Get call and receiving the data is not an issue. Normally I can get the desired data without an issues but I'm struggling with this particular response. Me messing up somehow.
So the call is a get to the API and I have the response in a variable ($r).
When using Postman the JSON response looks like this (only the first two nodes in the response shown. In the actual response there are 10 nodes):
{
"_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"
}
}
}
}
So I can use
$r.nodes
to get the 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=}
I then expected to do
$r.nodes[0]
to get the first node but this gives me the exact same as $r.nodes before.
$r.nodes."LvrIjMcFR1ahhFOnPl-gsQ"
then gives me the info I need about that node:
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}
But how can I get some sort of iterator over the nodes as I do not know the ID of the nodes beforehand?
I'm using Powershell 5.1
Upvotes: 0
Views: 626
Reputation: 6860
You can use the PSObject property to break down the object. Then pipe till you get Value
$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
}
Upvotes: 2
Reputation: 31
The problem here is that the json is converted to a PSCustomObject. Probably the easiest way to enumerate the keys is to do something like:
$NodeNames = $r.Nodes | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name
foreach ($NodeName in $NodeNames) {
$r.Nodes.$Nodename
}
If you were to use PowerShell Core 6.1, you could also do something like:
$r = (Invoke-WebRequest 'http://url').Content | ConvertFrom-Json -AsHashTable
And then you could enumerate through the keys with:
foreach ($node in $r.Nodes.GetEnumerator()) {
#code here
}
Upvotes: 0