如何用数组格式化散列table?

How to format a hash table with an array?

我想在 PowerShell 中显示散列table,我想使用数组来填充其中一列。

基本上,我有的是:

$Servers = "training01.us", "training02.us", "training03.us"

#Table
$table = @(@{ColumnA="$Servers";ColumnB=online})
$table.ForEach({[PSCustomObject]$_}) | Format-Table -AutoSize

我想要它做的是在 table 中的不同行显示每个服务器。例如:

列A
------
training01.us
training02.us
training03.us

但我却显示了这个:

列A
------
training01.us training02.us training03.us

我该如何解决这个问题?

这应该会给你想要的输出:

$Servers = "training01.us", "training02.us", "training03.us"
$OFS = "`n"
$table = @(@{ColumnA="$Servers";ColumnB='online'})
$table.ForEach({[PSCustomObject]$_}) | Format-Table -AutoSize -Wrap

但请注意,所有服务器仍位于同一字段中(作为单个字符串)。三个名称字符串仅用换行符连接(通过将输出字段分隔符设置为 `n)。

Format-Table -Wrap 然后显示包装的字符串值而不截断输出。

获得相同结果(无需修改 $OFS)的另一种方法是

$table = @(@{ColumnA=$Servers -join "`n";ColumnB='online'})
$table.ForEach({[PSCustomObject]$_}) | Format-Table -AutoSize -Wrap