Convert Cisco "Show Interfaces" output to PowerShell objects

Hi.
I have output like this from a Cisco switch that I try to convert to Objects:

switch01#show interfaces counters errors

Port Align-Err FCS-Err Xmit-Err Rcv-Err UnderSize OutDiscards
Gi0/1 0 0 0 0 0 0
Gi0/2 0 0 0 0 0 0
Gi0/3 0 0 0 0 0 125
Gi0/4 0 0 0 0 0 0
Gi0/5 0 0 0 0 0 0
Gi0/6 0 0 0 0 0 409
Gi0/7 0 0 0 0 0 0
Gi0/8 0 0 0 0 0 0

Port Single-Col Multi-Col Late-Col Excess-Col Carri-Sen Runts Giants
Gi0/1 0 0 0 0 0 0 0
Gi0/2 0 0 0 0 0 0 0
Gi0/3 30 60 0 1 0 0 0
Gi0/4 0 0 0 0 0 0 0
Gi0/5 0 0 0 0 0 0 0
Gi0/6 0 0 0 0 0 0 0
Gi0/7 0 0 0 0 0 0 0
Gi0/8 0 0 0 0 0 0 0
switch01#

I try to process the text like this:
$PortFil = ‘C:\data\ScriptLAB\SwitchType_C2960G_8port.txt’
$Ports = Get-Content $PortFil
$SWPorts = $Ports | Select-Object -Skip 2 | Select-Object -first($Ports.length - 3)
$CSVports = ConvertFrom-Csv $SWPorts

My main problem is that the output from the switch is split up into 2(or more depending on switch type) blocks of text.
I’d like to created Objects With names from the “Port” column, and Properties with PropertyName from the HeaderRow(s), and values from the corresponding rows in all the columns including columns in the following Block(s).

Can ConvertFrom-Csv do this, or do I have to revert to reading line by line and constructing the Objects based on
the content of each line?

Regards Geir

The two tables thing is annoying. You could isolate the two tables into their own strings or arrays of strings, and use ConvertFrom-Csv on each on separately, then pass the two to something like Join-Object (which you’ll have to search for; it’s not part of PowerShell out of the box), but personally, I’d probably just write code to handle this format directly. You’d have to do some parsing to split the tables up anyway, and if you’re going to that effort, may as well just build the objects properly in one shot rather than having to use Join-Object to combine them later.

Please test if below code works for you.

$PortFil = 'C:\data\ScriptLAB\SwitchType_C2960G_8port.txt' $CSVports = Import-Csv -Path $PortFil -Delimiter ' '