Powershell 缺少属性的对象的筛选器列表
我有一个自定义对象的通用列表,如下所示:Powershell 缺少属性的对象的筛选器列表,powershell,Powershell,我有一个自定义对象的通用列表,如下所示: 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 autoProper
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”属性,而另一些没有。我试图过滤掉那些没有“自动特殊设备”的
作为一个示例,下面的代码是我所拥有的:
$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填充列表,如下所示:
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=$string|convertfromJSON
使用这些对象填充列表后,以下操作将起作用:
$list |其中{$\.Autoproperties.name-notcontains'auto.specialDevice'}
这是因为
$.AutoProperties.name
是AutoProperties集合中所有名称的列表。您的$list.Add(($string | ConvertFrom Json))
将数组嵌套为$list
的第一个元素。因此,当前您的对象数组位于$list[0]
中。如果您改为使用$list=ConvertFrom Json-InputObject$string
,那么Mike Shepard下面的回答就行了。如果出于某种原因,您觉得只需要特定的列表类型,您可以执行[System.Collections.Generic.list[PSObject]]$list=convertfromJSON-InputObject$string
以强式键入$list
变量。