Reputation: 77
I am trying to display results of mutlidimentional array as a table so it is easier readable by the user potentially using this script.
I am getting the result displayed by looping through each object in the array. However, it is all crammed together and not very readable for the user to make a decision.
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
So currently the table is displayed as
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
What I would like it to be displayed like below or similar with the headers
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
Any explanation would also be handy so I can replicate this to use for other parts of the script.
Upvotes: 0
Views: 846
Reputation: 200503
To display a list of objects in tabular form: just pipe it into Format-Table
.
$ResourceA | Format-Table
If you need an additional index column you can add that like this:
$global:id = 0
$ResourceA | Format-Table @{n='ID';e={$global:id;$global:id++}},*
However, a more user-friendly approach would probably be something like
$selection = $ResourceA | Out-GridView -PassThru
which will display a simple graphical selection dialog with filtering/sorting capabilities that will return the selected rows when closed. The returned rows are then stored in the variable $selection
.
Upvotes: 1