Powershell - 显示哈希表中的多个列,彼此相邻



我的powershell代码:

$h1 = [ordered]@{
"item1" = "command1"
"item2" = "command2"
"item3" = "command3"
"item4" = "command4"
"item5" = "command5"
}
$h2 = [ordered]@{
"col1" = "result1"
"col2" = "result2"
}
$h1.GetEnumerator() | Format-Table @{
Label = "Item";
Expression = { $_.Key }
}, @{
Label = "Command";
Expression = { $_.Value }
}
$h2.GetEnumerator() | Format-Table @{
Label = "Column";
Expression = { $_.Key }
}, @{
Label = "Result";
Expression = { $_.Value }
}

输出

Item  Command
----  -------
item1 command1
item2 command2
item3 command3
item4 command4
item5 command5

Column Result
------ ------
col1   resul1
col2   resul2

期望的输出

Item  Command  Column Result
----  -------  ------ ------
item1 command1 col1   resul1
item2 command2 col2   resul2
item3 command3
item4 command4
item5 command5

我正在尝试并排放置显示两个哈希表。 可以使用 2 个哈希表吗? 或者也许我应该使用两个数组?

基本上,我只想显示多列,但数据不均匀,如我所需的输出所示。

任何帮助将不胜感激。

您可以使用自定义对象执行以下操作:

$index = 0
$output = $h1.GetEnumerator() | Foreach-Object {
[pscustomobject]@{'Item' = $_.key; 'Command' = $_.value}
}
$h2.GetEnumerator() | Foreach-Object {
$output[$index++] | Add-Member -NotePropertyMembers @{'column' = $_.key;'result' = $_.value}
}
$output

最新更新