1

I'm trying to get the sitename of a device by using the hostname of a single device from an object that contains the details of 100's of devices in Powershell. How do I filter it?

The data is originally from an API and is being pulled in as Json, I've converted it using ConvertFrom-Json so it should be in an object now.

I have tried piping the object through Select-Object and Where-Object unsuccessfully, the way I'm using the commands don't seem to do anything but I'm not sure what I'm doing wrong.

The data is initially pulled using:

$allDevices = New-AemApiRequest @params -ApiAccessToken $apiAccessToken

And is then converted to an object using:

$allDevicesObj = $allDevices | ConvertFrom-Json

The results of that can then be seen using:

Write-Host $allDevicesObj.devices

Which will show data similar to this:

@{id=1234; uid=123-456-789; siteId=1; siteUid=11aa; siteName=site1; deviceType=; hostname=DESKTOP-abc123;}
@{id=2345; uid=987-654-321; siteId=2; siteUid=22bb; siteName=site2; deviceType=; hostname=DESKTOP-abc456;} 
@{id=3456; uid=234-345-456; siteId=3; siteUid=33bb; siteName=site3; deviceType=; hostname=DESKTOP-abc789;} 

I want to be able to filter the output to 1 of the results based on the hostname, so I tried using a combination of the Where-Object and Select-Object functions:

Write-Host $allDevicesObj.devices | Where-Object {$_.hostname -eq DESKTOP-abc123}

That just seems to do nothing and displays everything again. I tried being a bit less specific, but to also only select the siteName:

Write-Host $allDevicesObj.devices | Where-Object -Contains "123" | Select-Object -Property siteName

But that just showed everything again too. I tried similar variants with Select-Object with the same results.

When using the Where-Object to specify the object I want and then to just select the siteName value/property using Select-Object I am hoping to get the output to just be

site1
mklement0
  • 382,024
  • 64
  • 607
  • 775
Weighsone
  • 103
  • 1
  • 10

1 Answers1

3

Write-Host writes a string representation of your object to the console and doesn't send anything down the pipeline. Filter using Where-Object and just allow it to be written to the Output stream.

$allDevicesObj | Where-Object {$_.hostname -eq 'DESKTOP-abc123'}

To get just the site you can pipe from where-object to select-object

$allDevicesObj | Where-Object {$_.hostname -eq 'DESKTOP-abc123'} | Select-Object -property SiteName
StephenP
  • 3,895
  • 18
  • 18
  • Ah, I did not know that about Write-Host. Thank you, that did exactly what I needed. – Weighsone Apr 25 '19 at 01:38
  • @Weighsone [`Write-Host` is typically the wrong tool to use](http://www.jsnover.com/blog/2013/12/07/write-host-considered-harmful/), unless the intent is to write _to the display only_, bypassing the success output stream and with it the ability to send output to other commands, capture it in a variable, redirect it to a file. In PSv5+ `Write-Host` writes to the [information stream](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_redirection), whose output _can_ be captured, but only via `6>`. See also: https://stackoverflow.com/a/50416448/45375 – mklement0 Apr 25 '19 at 02:33