我有一个通用的自定义对象列表,如下所示:
id : 1
displayName : server1.domain.tdl
autoProperties : { @{name = auto.bios_version; value = 6.00 }, @{name = auto.specialDevice; value = True} }
id : 2
displayName : server2.domain.tdl
autoProperties : { @{name = auto.bios_version; value = 6.00 } }
其中一些具有“auto.SpecialDevice”属性,而有些则没有。我试图过滤掉那些没有“auto.SpecialDevice”的文件。
作为示例,此代码可以达到我的要求:
$string = ' [
{
"id": 1,
"displayName": "server1.domain.tdl",
"autoProperties": [
{
"name": "auto.bios_version",
"value": "6.00"
},
{
"name": "auto.specialDevice",
"value": "True"
}
]
},
{
"id": 2,
"displayName": "server2.domain.tdl",
"autoProperties": [
{
"name": "auto.bios_version",
"value": "6.00"
}
]
}
]
'
$list = [System.Collections.Generic.List[PSObject]]::New()
$list.Add(($string | ConvertFrom-Json))
因此,对象位于名为$ list的变量中,然后我尝试了以下操作,该操作返回了两个设备:
$list | Where-Object { -Not $_.autoProperties['auto.specialDevice'] }
什么是正确的方法?
最佳答案
(添加@ansgar wiechers和@john rees建议的列表人口代码)
您可以像这样从json填充列表:$list = $string | ConvertFrom-Json
一旦使用这些对象填充了列表,便可以进行以下操作:$list | where {$_.Autoproperties.name -notcontains 'auto.specialDevice'}
这是因为$_.AutoProperties.name
是autoproperties集合中所有名称的列表。
关于powershell - 缺少属性的对象的过滤器列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57616312/