是否可以在 powershell 中向集合添加属性?

发布于 2024-10-20 17:08:14 字数 825 浏览 1 评论 0原文

我有一个通过普通 Get-ChildItem 获得的非常大的对象数组,并且我需要能够通过各种不同的属性对它们进行索引。我有一组哈希表,可以通过这些属性对它们进行索引,但现在它只是一堆必须单独构建和管理的集合。在某一时刻,我突然想到,将哈希表作为“ByPath”、“ByGuid”等属性添加到基本集合中会很好。构建它的代码只会将我已经拥有的各个语句组合在一起:

$items = Get-ChildItem -recurse blahblah
$items | Add-Member -membertype NoteProperty -name "ByGuid" -value (Get-ItemsByGuid)
$items | Add-Member -memberType NoteProperty -name "ByPath" -value (Get-ItemsByPath)
$items

不幸的是,虽然这不会引发执行,但它不会执行任何操作。这些属性存在并且通过 Get-Member 可见,但在询问时它们始终为 null,并在之后设置时抛出。

在此对象上找不到属性“ByGuid”;确保它存在并且可设置。 在行:1 字符:14 <代码>+$项目。 <<<<< ByGuid = $itemsByGuid + CategoryInfo:InvalidOperation:(:) [],RuntimeException +FullyQualifiedErrorId:PropertyAssignmentException

谎言!

根据我想要做的事情,有什么理由认为这是不可能的吗?或者我收到的奇怪的混杂信息有什么原因吗?

I've got a very large array of objects obtained by a normal Get-ChildItem, and I need to be able to index them by a variety of different properties. I've got a set of hashtables that I've made that index them by those properties, but right now it's just a bunch of collections that must be built and managed separately. At one point it occurred to me that it would be nice to just add the hashtables onto the base collection as properties such as "ByPath" "ByGuid" etc. The code to build it would just draw together the individual statements I already have:

$items = Get-ChildItem -recurse blahblah
$items | Add-Member -membertype NoteProperty -name "ByGuid" -value (Get-ItemsByGuid)
$items | Add-Member -memberType NoteProperty -name "ByPath" -value (Get-ItemsByPath)
$items

Unfortunately, while this doesn't throw on execution, it doesn't do anything. The properties exist and are visible with Get-Member, but they're always null when interrogated and throw when being set afterwards.

Property 'ByGuid' cannot be found on this object; make sure it exists and is settable.
At line:1 char:14

+ $items. <<<< ByGuid = $itemsByGuid
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException

lies!

Is there any reason that this shouldn't be possible based on what I'm trying to do? Or is there some reason for the oddly mixed messages I'm getting?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我只土不豪 2024-10-27 17:08:14

是的,这是可能的,而且您走在正确的轨道上。请记住,管道枚举集合(包括数组)。您需要考虑该枚举,否则最终会将属性添加到数组中的每个项目而不是数组本身。您可以使用逗号运算符来解决此枚举问题,如下所示:

$items = ,$items | Add-Member NoteProperty ByGuid (Get-ItemsByGuid) -PassThru
$items = ,$items | Add-Member NoteProperty ByPath (Get-ItemsByPath) -PassThru

请注意,如果您想使用 Get-Member 检查 $items 数组上的成员,则需要使用相同的技巧:

,$items | Get-Member

, 运算符只是将目标包装在另一个数组中,其中目标是唯一的元素。当管道枚举这个新数组时,我们将原始数组作为管道的唯一输出。

Yes it is possible and you are on the right track. Just remember that the pipeline enumerates collections (including arrays). You need to account for that enumeration otherwise you wind up adding the properties to each item in the array rather than to the array itself. You can workaround this enumeration by using the comma operator like so:

$items = ,$items | Add-Member NoteProperty ByGuid (Get-ItemsByGuid) -PassThru
$items = ,$items | Add-Member NoteProperty ByPath (Get-ItemsByPath) -PassThru

Note that if you want to check the members on the $items array using Get-Member, you need to use the same trick:

,$items | Get-Member

The , operator simply wraps the target in another array where the target is the only element. When the pipeline enumerates this new array, we get the original array as the only output into the pipeline.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文