McKenning
McKenning

Reputation: 641

PS: Create a multidimensional array from a text file - Cisco Switch Config

I'm attempting to create a script to process multiple cisco configs and port them to Excel via CSV with the following fields: Interface Name, Description, VLAN and Voice VLAN.

I can import the config file with the following code:

$importPath = ["PATH TO FILE"]
$text = Get-Content -path $importFile

I can then process the imported text to group interfaces together like this:

$regex = '(?ms)interface(.+?)!'
$text = $text -join "`n"
$matchedGroups = $text | Select-String $regex -AllMatches | ForEach-Object { $_.Matches.Value }

The resultant text is now in groups by interface (there is some junk in there, but it is minimal and manageable). However, I don't know how to then take these groups (lines of strings) and organize them in an array where each line is an element in the array. Once I have this, I can assign labels to sanitized and extracted information (as above). Example of text in the $matchedGroups variable:

PS: C:\$matchedGroups[0]
interface Port-channel1
 description Port-channel1 to DEVICE
 switchport access vlan ####
 switchport trunk native vlan ####
 switchport mode trunk

Desired array example:

$array = @("interface Port-channel1"," description Port-channel1 to DEVICE"," switchport access vlan ####"," switchport trunk native vlan ####"," switchport mode trunk")

Upvotes: 0

Views: 102

Answers (1)

Esperento57
Esperento57

Reputation: 17492

try this

$array =$matchedGroups[0] -split "`n"

#print first element
$array[0]

If you want trim your rows :

$array=$string -split "`n" |%{$_.trim()}

Upvotes: 3

Related Questions