Convert the lancom router file sysinfo to csv

I loaded the file sysinfo from my LANCOM Router to get some infos. It is possible convert this file to csv with powershell?

Code sample:

DEVICE:                LANCOM 1781AW
HW-RELEASE:            B
SERIAL-NUMBER:         123456789232
MAC-ADDRESS:           00a057xxxxxx
IP-ADDRESS:            192.168.0.251
IP-NETMASK:            255.255.255.0
INTRANET-ADDRESS:      0.0.0.0
INTRANETMASK:          0.0.0.0
VERSION:               10.42.0383RU2 / 03.03.2021
VERSION-GIT:           0aefbcd5f2aed7fe48f91a0232621aaaf0fed4e6
NAME:                  1781AW
CONFIG-STATUS:         17440;0;bd524ad8aa0436cc571317e9bf5f3f25fa4bb961.12254905032021.2414
FIRMWARE-STATUS:       0;0.58;0.2;10.42.0383RU2.03032021.58;10.42.0280RU1.09022021.57
LOADER:                4.12.0001Rel
HW-MASK:               00001100000000000000000001110011
FEATUREWORD:           00000100001000000100000100011100
REGISTERED-WORD:       00000100001000000100000100011100

The first two lines are empty.

UPDATE:

I don't have PowerShell 4.0 or higher on some machines. I wan't a solution without ConvertFrom-String.

This is my code sample:

    [email protected]'
DEVICE:                LANCOM 1781AW
HW-RELEASE:            B
SERIAL-NUMBER:         123456789232
MAC-ADDRESS:           00a057xxxxxx
IP-ADDRESS:            192.168.0.251
IP-NETMASK:            255.255.255.0
INTRANET-ADDRESS:      0.0.0.0
INTRANETMASK:          0.0.0.0
VERSION:               10.42.0383RU2 / 03.03.2021
VERSION-GIT:           0aefbcd5f2aed7fe48f91a0232621aaaf0fed4e6
NAME:                  1781AWs
CONFIG-STATUS:         17440;0;bd524ad8aa0436cc571317e9bf5f3f25fa4bb961.12254905032021.2414
FIRMWARE-STATUS:       0;0.58;0.2;10.42.0383RU2.03032021.58;10.42.0280RU1.09022021.57
LOADER:                4.12.0001Rel
HW-MASK:               00001100000000000000000001110011
FEATUREWORD:           00000100001000000100000100011100
REGISTERED-WORD:       00000100001000000100000100011100
'@

$content=$content -replace '\s{2,}' -split ':|\n'
[email protected]()

for($i=0;$i -lt $content.count;$i+=2)
{
    $array+=[pscustomobject]@{($content[$i]).trim() = ($content[$i+1]).trim()}
}
$array |Export-Csv sysinfo.csv -Delimiter ";" -NoTypeInformation

This is the content in sysinfo.csv

"DEVICE"
"LANCOM 1781AWHW-RELEASE"

This is not a CSV-Format. Have some ideas?

My solution without ConvertFrom-String:

    $sysinfo = Get-Content .\temp\$filename -Raw

    $sysinfo=$sysinfo -split ':|\r|\n'
    $sysinfo=$sysinfo -replace '\s{2,}'
    $sysinfo=$sysinfo -notmatch '^\s*$'

    [email protected]()

    for($i=0;$i -lt $sysinfo.count;$i+=2) {   
        [array]$sysinfoParsed+=[pscustomobject]@{Name=($sysinfo[$i]).trim();Value=($sysinfo[$i+1])}
    }

# Export to CSV
$sysinfoParsed |Export-Csv .\temp\sysinfo.csv -Delimiter ";" -NoTypeInformation

Answers

Exporting to CSV is easy (use Export-Csv) - it's parsing the input file that's tricky.

Windows PowerShell (version 4.0 and up) comes with a rather experimental, but very cool cmdlet called ConvertFrom-String - give it a sample template and it'll "figure out" how to parse your input.

This use case is perfectly fit for ConvertFrom-String:

# Read sysinfo from disk as a single multi-line string
$sysinfo = Get-Content .\path\to\sysinfo.txt -Raw

# Pass template to ConvertFrom-String, it'll figure out the rest:
$sysinfoParsed = $sysinfo |ConvertFrom-String -TemplateContent @'
{Name*:DEVICE}:                {Value:LANCOM 1781AW}
{Name*:HW-RELEASE}:            {Value:B}
'@

# Export to CSV
$sysinfoParsed |Export-Csv .\path\to\output.csv -NoTypeInformation
Posted on by Mathias R. Jessen