如何格式化多维数组的结果以在PowerShell中显示为表格

时间:2019-09-12 14:05:59

标签: arrays powershell arraylist multidimensional-array

我正尝试将多元数组的结果显示为表格,以便可能使用此脚本的用户更容易阅读。

我通过遍历数组中的每个对象来显示结果。但是,它们全都塞满了,对于用户做出决定不是很容易理解。

Write-Host "Avalible Resource Pools at Site A" -ForegroundColor White

$ResourceA = Get-ResourcePool -Server $vmhostA |
             Sort-Object -Property name |
             Select-Object -Property name, CpuLimitMHz, MemReservationGB, MemLimitGB 

$menu = @{}

$selmenu = New-Object 'object[,]' 0,0
for ($i=1; $i -le $ResourceA.Count; $i++) {
    $selmenu += ,@($i, $ResourceA[$i-1].name, $ResourceA[$i-1].CpuLimitMHz, $ResourceA[$i-1].MemReservationGB, $ResourceA[$i-1].MemLimitGB)
    $menu.Add($i, ($ResourceA[$i-1].name))
}

foreach ($item in $selmenu) {
    Write-Host($item)
}

[int]$ans = Read-Host 'Enter selection'
$selection = $menu.Item($ans);
$selection

因此当前表格显示为

1 Cloud -1 0 -1
2 Cloud 16568 31.25 31.25
3 CS -1 125 125
4 Cust Sols 22000 19.53125 19.53125
5 Devop -1 0 -1
6 Devop -1 500 -1
7 Tus' lab 35000 0 68.359375
8 IT -1 0 -1
9 IT -1 0 -1
10 IT -1 0 -1
11 PS-DMS -1 62.5 62.5
12 PS-Unbu 20000 4 29.296875
13 QA -1 116.2109375 -1
14 QA -1 0 -1
15 Resources 43248 750.6875 750.6875
16 Resources 44975 750.974609375 750.974609375
17 Resources 36378 246.8544921875 246.8544921875
18 Resources -1 0 -1

我希望它显示如下或类似的标题

ID    Name       CPULimitMHz     MemReservationGB        MemoryLimitGB
--------------------------------------------------------------------------
1    Cloud        -1               0                      -1
2    Cloud        16568            31.25                  31.25
3    CS           -1               125                    125
4    Cust Sols    22000            19.53125               19.53125
5    Develop      -1               0                      -1
6    Develop      -1               500                    -1
7    Tus' lab     35000            0                       68.359375
8    IT           -1               0                       -1
9    IT           -1               0                       -1
10   IT           -1               0                       -1
11   PS-DMS       -1               62.5                    62.5
12   PS-Unbu      20000            4                       29.296875
13   QA           -1               116.2109375             -1
14   QA           -1               0                       -1
15   Resources    43248            750.6875                750.6875
16   Resources    44975            750.974609375           750.974609375
17   Resources    36378            246.8544921875          246.8544921875
18   Resources    -1               0                       -1

任何解释都将很方便,因此我可以将其复制以用于脚本的其他部分。

1 个答案:

答案 0 :(得分:1)

要以表格形式显示对象列表:只需将其通过管道插入Format-Table

$ResourceA | Format-Table

如果您需要其他索引列,可以这样添加:

$global:id = 0
$ResourceA | Format-Table @{n='ID';e={$global:id;$global:id++}},*

但是,更人性化的方法可能类似于

$selection = $ResourceA | Out-GridView -PassThru

将显示一个简单的图形选择对话框,该对话框具有过滤/排序功能,在关闭时将返回选定的行。然后,将返回的行存储在变量$selection中。