问题描述
我正在尝试使用单个设备的主机名来获取设备的站点名称,该对象包含 Powershell 中 100 个设备的详细信息.我如何过滤它?
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?
数据最初来自 API 并作为 Json 引入,我已使用 ConvertFrom-Json
对其进行转换,因此它现在应该在一个对象中.
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.
我尝试通过 Select-Object
和 Where-Object
管道对象失败,我使用命令的方式似乎没有做任何事情,但我我不确定我做错了什么.
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;}
我希望能够根据主机名将输出过滤为 1 个结果,因此我尝试使用 Where-Object
和 Select-Object
的组合> 功能:
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
但这也再次显示了一切.我用 Select-Object
尝试了类似的变体,结果相同.
But that just showed everything again too. I tried similar variants with Select-Object
with the same results.
当使用 Where-Object
指定我想要的对象,然后使用 Select-Object
选择 siteName 值/属性时,我希望得到输出只是
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
推荐答案
Write-Host 将对象的字符串表示形式写入控制台,并且不向管道发送任何内容.使用 Where-Object 过滤并允许将其写入输出流.
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'}
要获得可以从 where-object 到 select-object 的管道
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
这篇关于如何根据powershell中的主机名将设备对象过滤为单个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!