Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell: Display array-members in ConvertTo-HTML

Tags:

powershell

I have a collection of Objects with NoteProperties. One of these properties is an array. I would like to have an easy way to display the members of this noteproperty not just '[System.Object]' when I use ConvertTo-HTML.

For example, I have a property called "Computername" and one called "Disks" that is actually an array of all Disks of that computer. The output of ConvertTo-HTML looks like this:

Computername    Disks
fs              System.Object[]
dc1             System.Object[]

Is there an easy way to display the members of "Disks" without manually creating the html-output?

Edit:
One Disk-object has actually 3 values (name, capacity and free space) that have to be shown too. The output of $collection[0].Disks on the powershell prompt could look like this (depending on the disks-count and so on):

PS > $collection[0].Disks

Bezeichnung                                                  Kapazität (MB)                                              Freier Speicher (MB)                                       
-----------                                                  --------------                                              --------------------                                       
C:\                                                          40963                                                       23040 (56 %)                                               
D:\                                                          184324                                                      39024 (21 %)                                               
E:\                                                          204805                                                      103373 (50 %)              

I was thinking about something like a tree that shows the computername first and then a table of all disks of that computer. Something like (could be more beautiful ;)):

Computername    
fs             
               Bezeichnung                                                  Kapazität (MB)                                              Freier Speicher (MB)                                       
               -----------                                                  --------------                                              --------------------                                       
               C:\                                                          40963                                                       23040 (56 %)                                               
               D:\                                                          184324                                                      39024 (21 %)                                               
               E:\                                                          204805                                                      103373 (50 %)              
dc1 
like image 452
wullxz Avatar asked Dec 03 '25 21:12

wullxz


2 Answers

My normal approach in these kinds of cases is to expand the data into a more table-friendly format, rather than spend a lot of time customizing the HTML.

$collection |
  select ComputerName -ExpandProperty Disks |
  ConvertTo-Html -Fragment -Property ComputerName,Name,Capacity,'Free Space'

This expands the list of disks for each computer, so that each appears in a separate row in the table along side the computer name.

ComputerName Name Capacity Free Space
------------ ---- -------- ----------
fs           C:\    109324      60986
fs           D:\    289313     281692
dc1          C:\    290473      12626
dc1          D:\    283195     125983
dc1          E:\    105652      13898
like image 81
Emperor XLII Avatar answered Dec 06 '25 12:12

Emperor XLII


You can convert them into a string beforehand:

Get-Foo | foreach { $_.Disks = $_.Disks -join ', ' } | ConvertTo-HTML

or maybe without actually changing your objects:

Get-Foo | select Computername,@{L=Disks;E={$_.Disks -join ', '}}
like image 45
Joey Avatar answered Dec 06 '25 12:12

Joey