5

There are many tools that output their data in in a table format. One such example is diskpart. Shaving off some extraneous output, you would get something like this.

Disk ###  Status         Size     Free     Dyn  Gpt
--------  -------------  -------  -------  ---  ---
Disk 0    Online          136 GB      0 B
Disk 1    Offline         136 GB   136 GB
Disk 2    Reserved       1027 MB      0 B        *
Disk 3    Reserved        500 GB      0 B        *
Disk 4    Reserved        500 GB      0 B        *
Disk 5    Reserved         10 GB      0 B        *
Disk 6    Reserved         13 GB      0 B        *
Disk 7    Reserved       4102 MB      0 B        *
Disk 8    Reserved       7169 MB      0 B        *
Disk 9    Reserved        503 GB      0 B        *
Disk 10   Reserved        506 GB      0 B        *
Disk 11   Reserved        500 GB      0 B        *
Disk 12   Reserved       3891 GB      0 B        *
Disk 13   Reserved        500 GB      0 B        *
Disk 14   Reserved       3891 GB      0 B        *
Disk 15   Reserved       1843 GB      0 B
Disk 16   Reserved       3072 GB      0 B        *
Disk 17   Reserved       2048 GB      0 B        *
Disk 18   Reserved        808 GB      0 B        *
Disk 19   Reserved        805 GB      0 B        *
Disk 20   Reserved       3891 GB      0 B        *
Disk 21   Reserved       3891 GB      0 B        *
Disk 22   Reserved       3891 GB      0 B        *
Disk 23   Reserved       6144 GB      0 B        *

Another example is netstat, which looks like the following:

 Proto  Local Address          Foreign Address        State
 TCP    0.0.0.0:80             7ANDYS:0               LISTENING
 TCP    0.0.0.0:135            7ANDYS:0               LISTENING
 TCP    0.0.0.0:443            7ANDYS:0               LISTENING
 TCP    0.0.0.0:445            7ANDYS:0               LISTENING
 TCP    0.0.0.0:1025           7ANDYS:0               LISTENING
 TCP    0.0.0.0:1026           7ANDYS:0               LISTENING
 TCP    0.0.0.0:1027           7ANDYS:0               LISTENING
 TCP    0.0.0.0:1028           7ANDYS:0               LISTENING
 TCP    0.0.0.0:1029           7ANDYS:0               LISTENING
 TCP    0.0.0.0:2048           7ANDYS:0               LISTENING

I am trying to figure out if there is a fairly repeatable way to convert this type of data into an object, such that the properties of the object are the headers in the first row. I know there are a bunch of ways to do this for the output of individual tools using regex, but I am looking for more of a strategy on how to go about solving this, rather than a one-off solution just for diskpart or netstat.

I was trying to figure out how to use Lee Holmes' script up on Poshcode called Convert-TextToObject, but wasn't quite sure where to start.

Andy Schneider
  • 8,516
  • 6
  • 36
  • 52
  • First is to just generalize the code to work over a list of items. The code in the link can be used to generate each object (e.g. run once per row, excluding header). Then it's just extracting the 'format' from the header line(s). Optimizations can come later. Should be a fun little project. –  Apr 21 '11 at 18:27

5 Answers5

0

Something for the netstat example:

get-content netstat.txt | select -skip 1 | 
ConvertFrom-String -propertynames blank,proto,local,foreign,state | 
select * -ExcludeProperty blank

proto local        foreign  state
----- -----        -------  -----
TCP   0.0.0.0:80   7ANDYS:0 LISTENING
TCP   0.0.0.0:135  7ANDYS:0 LISTENING
TCP   0.0.0.0:443  7ANDYS:0 LISTENING
js2010
  • 23,033
  • 6
  • 64
  • 66
0

On a strategical point of view, I would try to transform your text file into a correct CSV (coma separated values) file and then use Import-Csv.

JPBlanc
  • 70,406
  • 17
  • 130
  • 175
0

I did this yesterday :). You can use out-datatable to transform your data into a System.DataTable. Get it at poshcode.

For more details, see my recent entry.

Community
  • 1
  • 1
Emiliano Poggi
  • 24,390
  • 8
  • 55
  • 67
0

Using this ConvertFrom-SourceTable cmdlet:

$DiskPart | ConvertFrom-SourceTable -Literal | Format-Table

Disk ### Status   Size    Free   Dyn Gpt
-------- ------   ----    ----   --- ---
Disk 0   Online   136 GB  0 B
Disk 1   Offline  136 GB  136 GB
Disk 2   Reserved 1027 MB 0 B        *
Disk 3   Reserved 500 GB  0 B        *
Disk 4   Reserved 500 GB  0 B        *
...

$NetStat | ConvertFrom-SourceTable -Literal | Format-Table

Proto Local Address Foreign Address State
----- ------------- --------------- -----
TCP   0.0.0.0:80    7ANDYS:0        LISTENING
TCP   0.0.0.0:135   7ANDYS:0        LISTENING
TCP   0.0.0.0:443   7ANDYS:0        LISTENING
TCP   0.0.0.0:445   7ANDYS:0        LISTENING
...

(This also means that you can also do a full round trip on the above display results)

iRon
  • 20,463
  • 10
  • 53
  • 79