0

I'm pulling 'down device' data from a network monitor via API using PowerShell

I'm struggling with how to reference the hostname value to add it to a variable. I'm after a list of hostnames of down devices.

In this case, there is only one host but obviously, there can be more than one with various IDs.

    $user = 'user'
$pass = 'password'

$pair = "$($user):$($pass)"

$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))

$basicAuthValue = "Basic $encodedCreds"

$Headers = @{
    Authorization = $basicAuthValue
}

$webData = Invoke-RestMethod -Method Get -Uri 'http://fqdn/api/v0/devices/?status=0' -ContentType application/json -Headers $Headers # -OutFile c:\data\Config.html
#$webData= $webData | ConvertTo-Json 
$webData | ForEach-Object {$_.devices} | Select -Property $_.hostname

Resulting Data from Powershell

201                                                                                                                       
---                                                                                                                       
@{device_id=201; poller_id=0; hostname=lonts01.local; sysName=lonts01.local; snmp_authname=; 

Direct output of $webData without the -ContentType

{"status":"ok","count":3,"devices":{"201":{"device_id":"201","poller_id":"0","hostname":"lonts01.local","sysName":"lonts01.local",...
richh
  • 5
  • 2
  • have you tried doing the `Invoke-RestMethod` without `-ContentType` specified? this will return a nice powershell object that you can then reference using the dot notation. e.g. `$_.content.hostname`, i am unsure of the structure of the object so feel free to post it if you get stuck – Otter Dec 13 '21 at 10:08
  • Thanks @Otter , sorry for being a bit dim on this. The data looks like ...{"status":"ok","count":3,"devices":{"201":{"device_id":"201","poller_id":"0","hostname":"lonts01.local","sysName":"lonts01.local" I have no idea how to reference 'hostname' in the powershell code. The data is in $webData the array looks link devices and the property is hostname. – richh Dec 13 '21 at 14:49
  • Dont be sorry, we are here to help. Can you remove `-ContentType application/json` and send the output again? – Otter Dec 13 '21 at 14:56
  • Thanks. I've output the data to file https://drive.google.com/file/d/1j7X3_DRR6xKDs81BAZrVabG2ObnLwAUM/view?usp=sharing I'm not sure if you can or want to access that link (is there a better way to share?) but I had removed the -ContentType In my first reply to you. The first few lines of data are... {"status":"ok","count":3,"devices":{"201":{"device_id":"201","poller_id":"0","hostname":"lonts01.local","sysName":"lonts01.local","snmp_authname":nu.... {"device_id":"228","poller_id":"0","hostname":"sydney.router.local","sysName":"",... etc – richh Dec 13 '21 at 15:21
  • Try ```$webData.devices.GetEnumerator() | foreach-object { $_.Value.hostname }``` – mclayton Dec 13 '21 at 16:15
  • Thanks @mclayton . I get the following Method invocation failed because [System.Management.Automation.PSCustomObject] does not contain a method named 'GetEnumerator'. – richh Dec 13 '21 at 16:51
  • Ah, you've gos PSCustomObjects - I was testing locally with hashtables. Looks like you got your answer anyway... – mclayton Dec 13 '21 at 19:45

1 Answers1

0

This is a typical example where you need the JSON subkeys' name if you want to access the underlying object, as you cannot expand them without being named explicitly.

Notice that i am fetching the keys' value (name) first then using it to expand the object.

$webData.devices | Get-Member -MemberType NoteProperty | ForEach-Object {
    $key = $_.Name
    $webData.devices.$key.hostname
}
Otter
  • 1,086
  • 7
  • 18
  • Many thanks @Otter that makes sense and most importantly works. Very useful for my the API future. Cheers. – richh Dec 13 '21 at 17:27