make rows to become columns in txt with powershell

Viewed 126

I have an input txt file with different data like this:

------------------------------------------------------------------------
Current Local Time:             Fri Jul 01 04:54:27 2016

Current GMT Time:               Thu Jun 30 20:54:27 2016
Machine ID:                     6090
Machine Name:                   WL6090
Display S/N:                    0253G020TG
Terrain version:                5.3.843.843  
CAESTaskList DLL version:       5.3.843.843  
Application Type:               Terrain / CAES Ultra
Operating System:               1.04
Total RAM used by active files: 339.72 kB

----------------------------------------------------------------------------------------------------------

                                   Disk Space

Free Disk Space:  6758 MB               Total Disk Space:  7076 MB              

----------------------------------------------------------------------------------------------------------

 Current Local Time:    07-01-16, 04:54:27
 Current Service Hours: 314.41

And I have many such files every day. I'm trying as a real newbie with powershell to grab only information I need from these files and I do it like this:

$Location = "D:\terrain_dia\Diag01Jul2016_045427.dia"
$a = Get-Content D:\terrain_dia\Diag01Jul2016_045427.dia

#Common
$option = [System.StringSplitOptions]::RemoveEmptyEntries

#ID
$LineID = Select-String -Path $Location -Pattern "Machine ID:" | Select-Object -Expand LineNumber
$ID = ($a)[$LineID-1]
$ID -replace '(?:\s|\r|\n)',''

#Name
$LineName = Select-String -Path $Location -Pattern "Machine Name:" | Select-Object -Expand LineNumber
$Name = ($a)[$LineName-1]
$Name -replace '(?:\s|\r|\n)',''

#SN
$LineSN = Select-String -Path $Location -Pattern "Display S/N:" | Select-Object -Expand LineNumber
$SN = ($a)[$LineSN-1]
$SN -replace '(?:\s|\r|\n)',''

#Version
$LineVersion = Select-String -Path $Location -Pattern "Terrain version:" | Select-Object -Expand LineNumber
$Version = ($a)[$LineVersion-1]
$Version -replace '(?:\s|\r|\n)',''

#RAM
$LineRam = Select-String -Path $Location -Pattern "Total RAM" | Select-Object -Expand LineNumber
$RAM = ($a)[$LineRam-1]
$RAM -replace '(?:\s|\r|\n)',''

#GPSSN
$LineGPSSN = Select-String -Path $Location -Pattern "GPS Receiver SN:" | Select-Object -Expand LineNumber
$GPSSN = ($a)[$LineGPSSN-1]
$GPSSN -replace '(?:\s|\r|\n)',''

#GPSType
$LineGPSType = Select-String -Path $Location -Pattern "GPS Receiver Type:" | Select-Object -Expand LineNumber
$GPSType = ($a)[$LineGPSType-1]
$GPSType -replace '(?:\s|\r|\n)',''

#NAV
$LineNAV = Select-String -Path $Location -Pattern "NAV firmware version:" | Select-Object -Expand LineNumber
$NAV = ($a)[$LineNAV-1]
$NAV -replace '(?:\s|\r|\n)',''

#SIG
$LineSig = Select-String -Path $Location -Pattern "SIG firmware version:" | Select-Object -Expand LineNumber
$SIG = ($a)[$LineSig-1]
$SIG -replace '(?:\s|\r|\n)',''

#ROM
$LineROM = Select-String -Path $Location -Pattern "ROM firmware version:" | Select-Object -Expand LineNumber
$ROM = ($a)[$LineROM-1]
$ROM -replace '(?:\s|\r|\n)',''

I'm getting the output:

MachineID:6090
MachineName:WL6090
DisplayS/N:0253G020TG
Terrainversion:5.3.843.843
TotalRAMusedbyactivefiles:339.72kB
GPSReceiverSN:3351J508SP
GPSReceiverType:MS992
NAVfirmwareversion:00506
SIGfirmwareversion:00506
ROMfirmwareversion:00425
FreeDiskSpace:6758MB
TotalDiskSpace:7076MB

So I have a fixed delimiter But what I really need to get is:

MachineID     MachineName      DisplayS/N 
6090          WL6090           0253G020TG

etc.

So I need to format this output as a table within txt file and then I'm going to put it te result into MS SQL db..

Please advise how can I format all these lines with powershell?

Any help will be very useful.

Thank you

2 Answers
Related