如果我采用 Microsoft 的示例 但它不起作用?
示例命令和预期结果:
@(
@{ 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 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