[PowerShell] Working with CSVs

JodoFett

Contributor
Veteran XX
Trying to find a way to clear up some user error when it comes to creating network designs, the format of the table used to track this is basically:

Start Device Location, Start Device Name, Start Device Port, End Device Location, End Device Name, End Device Port

So it ends up looking like this in two separate locations on the spreadsheet:

Code:
IDF 1	Switch 01	E1/45	IDF 2	Switch 02	E1/45
IDF 1	Switch 01	E1/46	IDF 2	Switch 02	E1/46
IDF 1	Switch 01	E1/47	IDF 2	Switch 02	E1/47
IDF 1	Switch 01	E1/48	IDF 2	Switch 02	E1/48

IDF 2	Switch 02	E1/45	IDF 1	Switch 01	E1/45
IDF 2	Switch 02	E1/46	IDF 1	Switch 01	E1/46
IDF 2	Switch 02	E1/47	IDF 1	Switch 01	E1/47
IDF 2	Switch 02	E1/48	IDF 1	Switch 01	E1/48

So, in theory if I pipe the Start values (Columns 1-3) and the End values (Columns 4-6) into two tables, I should be able to compare them and kick out anything that doesn't match or have a "twin". I only really started looking at this today and I'll work out a good solution eventually but curious how people would approach it. I don't have a formal background in PowerShell at all so my solutions always end up being pretty janky.

Here is what I have so far, it gets me the separate CSV files but I haven't put in the compare logic yet.

Code:
$filepath = "..\DeviceInfo.csv"
$lines = Import-Csv $filepath

$fullTable = @()
$startTable = @()
$endTable = @()
$lineCount = 1

Foreach($line in $lines)
{

If($StartU -ne '') {
$startTable += New-Object psobject -Property @{StartPort=$line.'Start Port';StartDevice=$line.'Start Device';StartU=$line.'Start U';Line=$lineCount}
$endTable += New-Object psobject -Property @{EndPort=$line.'End Port';EndDevice=$line.'End Device';EndU=$line.'End U';Line=$lineCount}
$fullTable += New-Object psobject -Property @{StartPort=$line.'Start Port';StartDevice=$line.'Start Device';StartU=$line.'Start U';EndPort=$line.'End Port';EndDevice=$line.'End Device';EndU=$line.'End U';Line=$lineCount}
}
$lineCount++
}
$startTable | Export-Csv startTable.csv -Force
$endTable | Export-csv endTable.csv -Force
$fullTable |Export-csv fullTable.csv -Force
 
this is an excel spreadsheet? why not populate the fields with formulas so people only have to enter things once and then you don't have to sanity check it after the fact?
 
this is an excel spreadsheet? why not populate the fields with formulas so people only have to enter things once and then you don't have to sanity check it after the fact?

That is an eventual goal, but for now I want to error check what we have.
 
Back
Top