从 Powershell 中的对象数组中删除项目 [英] Removing an item from a array of objects in Powershell

查看:39
本文介绍了从 Powershell 中的对象数组中删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组对象 $a,它返回如下输出.

I have an array object $a which returns an output like below.

通过执行 $a[0].Name 我可以访问每个Name"条目,$a[0].Available 我可以访问其对应的可用空间.

And by doing $a[0].Name I can access each "Name" entry, $a[0].Available I can access its corresponding Available space.

我有另一个数组说 $b 包含一些名字,说 $b 返回我两个名字sandeep_aggr1"和aggr4".这只是一个数组(没有 Name、Avaiable 之类的属性),不是对象,所以它不能使用 Compare-Object.

I have another array say $b which contains some names, say $b returns me two names "sandeep_aggr1" and "aggr4". This is just an array (no properties like Name, Avaiable), not an object, so It can't use Compare-Object.

我想删除原始对象 $a 中的其他条目,除了那些Name"等于sandeep_aggr1"和aggr4"的条目.

I want to remove other entries in the original object $a, except for those with "Name" equal to "sandeep_aggr1" and "aggr4".

这就是我正在做的.

foreach($bb in $b)
    {
          foreach($aa in $a)
          {
                if($aa.Name -ne $bb)
                {
                   $aa.Remove($aa.Name)
                }

          }


    }

    echo $a

但是,我没有看到删除的元素,我在这里遗漏了什么吗?任何帮助表示赞赏

But, I don't see the elements deleted, am I missing something here ? Any help appreciated

推荐答案

如果我没看错问题,这应该可行:

If I'm reading the question correctly, this should work:

$a = $a | where {$b -contains $_.Name}

这篇关于从 Powershell 中的对象数组中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆