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
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/

相关文章:

powershell - 如何检查 IIS 状态和正常运行时间?

list - 使用 PowerShell 更新列表中的所有项目

python - 如何在 anaconda 提示符下运行 powershell 脚本?

powershell - 如果文件匹配,递归删除DIR? (电源外壳)

windows - 如何使用PowerShell和Windows 10打开/关闭WiFi状态?

windows - Remove-Item 不删除文件

powershell - 从子文件夹复制文件并重命名

iis-7 - 使用 MSDeploy.exe 创建文件夹、网站和部署的 Powershell 脚本

powershell - 减法数组 (Powershell)

powershell - 在远程服务器(网络驱动器)上创建连接点