Powershell - 显示哈希表中的多个列并排显示

Powershell - Display multiple columns from hash tables next to each other

我的 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