Group-Object 示例有什么问题?

问题描述 投票:0回答:1

如果我采用 Microsoft 的示例 但它不起作用?

enter image description here

示例命令和预期结果:

@(
    @{ name = 'a' ; weight = 7 }
    @{ name = 'b' ; weight = 1 }
    @{ name = 'c' ; weight = 3 }
    @{ name = 'd' ; weight = 7 }
) | Group-Object -Property weight -NoElement

Count Name
----- ----
    1 1
    1 3
    2 7

最新的 Windows 10 企业版 22H2。

powershell grouping
1个回答
0
投票

此问题在最新的 PowerShell 7 中无法重现,但在 Windows PowerShell 5.1 中,具有字符串属性 (

Group-Object
) 的
-Property weight
不知道如何处理来自管道的传入哈希表 (
@{ ... }
),但您可以提供帮助它使用计算表达式代替:

@(
    @{ name = 'a' ; weight = 7 }
    @{ name = 'b' ; weight = 1 }
    @{ name = 'c' ; weight = 3 }
    @{ name = 'd' ; weight = 7 }
) | Group-Object -Property { $_.weight } -NoElement
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.