如何用数组格式化哈希表?

techguy1029

我想在 PowerShell 中显示一个哈希表,并且我想使用一个数组来填充其中一列。

基本上,我所拥有的是:

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

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

我希望它做的是在表中的不同行上显示每个服务器。例如:

柱子
-------
培训01.us
培训02.us
培训03.us

但相反,我得到了这个显示:

柱子
-------
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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章