单行计算 属性 抛出 ParserError。错误与否?

Single line calculated property throws ParserError. Bug or not?

已在 Windows PowerShell 5.1 和 PowerShell Core 7.2.2 的两台机器上复制了此内容,但未在网上看到任何提及。这是一个已知错误,还是不应将计算属性简单地写在一行中?

失败 Unexpected token 'expression=' in expression or statement.

Get-CimInstance -Class Win32_LogicalDisk | Select-Object -Property Name, @{ label='FreeSpace' expression={($_.FreeSpace/1GB).ToString('F2')}}

expression= 之前添加回车符 return 可防止 ParserError

Get-CimInstance -Class Win32_LogicalDisk | Select-Object -Property Name, @{ label='FreeSpace'
expression={($_.FreeSpace/1GB).ToString('F2')}}

Is this a known bug

没有

should calculated properties simply not be written on a single line?

当然,您只需要一个明确的 语句终止符 来分隔两个 key-value 条目。

当您组织哈希表文字(@{<key>="<value>"} 构造)时,每个 key-value 对在不同的行上, 换行符 充当隐式语句终止符.

删除换行符后,您需要使用 ; 代替它们:

# this ...
@{
  Label = 'FreeSpace'
  Expression = { ... }
}

# ... becomes this
@{ Label = 'FreeSpace'; Expression = { ... } }