2
votes

This is yet another time when I have problems with Invoke-RestMethod. Hope with your help I can progress. This time that problem is that I am struggling to get a port value of the object where ipaddress=10.128.64.44.

Running this code:

$user = "admin"
$pass = 'Password1'
$ipdir = '10.128.64.44'
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $pass)))
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add('Authorization',('Basic {0}' -f $base64AuthInfo))


$uri = 'https://myhostname/MAM/wfservice/workers/'
$rep = Invoke-restmethod -method get -Header $headers -Uri $uri

 Foreach($worker_port in $rep){

      write-output $worker_port.WorkersList.Worker

 }

Returns this in a terminal:

ActiveJobs          : 
ForbiddenAgentsList : ForbiddenAgentsList
IPAddress           : 10.128.64.34
LastMsgTime         : 2016-11-21T00:16:04.2813148+01:00
Name                : Worker02
NumOfSlots          : NumOfSlots
Port                : 52268
SelectedAgentsList  : SelectedAgentsList
Status              : Active 
Version             : 2.29.3740.0
Iteration           : 2016/2 SP3 (87)

ActiveJobs          : 
ForbiddenAgentsList : ForbiddenAgentsList
IPAddress           : 10.128.64.44 
LastMsgTime         : 2016-11-21T00:16:04.2813148+01:00
Name                : Worker05
NumOfSlots          : NumOfSlots
Port                : 53246
SelectedAgentsList  : SelectedAgentsList
Status              : Active
Version             : 2.29.3740.0
Iteration           : 2016/2 SP3 (87)

I've thought about running something like this to get a port:

if ( ($worker_port.WorkersList.Worker.IPaddress) -eq '10.128.64.44'){Write-Output $worker_port.Worker.Port }
         else {write-host "ouch!""}

... but it seems like this is not the way. It returns both ports:

53246
52268

Anybody knows what's wrong here? How can I parse it to get the port number for 10.128.64.44 only. Any advice is highly appreciated.

1
Try this: $rep.WorkersList.Worker | Where IPAddress -eq '10.128.64.44' | Select -expand Port - Martin Brandl
Incredible. This is the right answer @MartinBrandl. Well done and many, many, many thanks! Do you want post it as an answer? I imagine you get some points for that? - Okrx
Sure, I will post it as an answer. Thanks. - Martin Brandl

1 Answers

2
votes

Use the Where-Object cmdlet to filter the object based on the desired IPAddress and just select the Port:

$rep.WorkersList.Worker | Where IPAddress -eq '10.128.64.44' | Select -expand Port