Powershell - 从哈希表中显示多个相邻的列。

问题描述 投票:0回答:1

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

我的powershell代码: Output

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个哈希表? 或者我应该使用两个数组?

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

任何帮助都将是非常感激的。

arrays powershell formatting hashtable tabular
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.