Created
July 19, 2019 12:55
-
-
Save DrBob5188/841acef65713ba66e9d4a09b4405a1ce to your computer and use it in GitHub Desktop.
Answer for Powershell forum question by https://powershell.org/forums/topic/parsing-text-logic/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Split the records using a regex with lookahead. It's flexible, selective and doesn't consume the delimiter | |
$records = $SwitchInfo -split "(?m)(?=^.+-network)" | |
foreach ($record in $records){ | |
#the match operator fills the matches collection and saves us splitting/parsing | |
if ($record -match '([^ ]+)\s+cluster-network\s+([^ ]+)\s+(.+)'){ | |
$SwitchName = $matches[1] | |
$SwitchIP = $matches[2] | |
$SwitchModel = $matches[3] | |
Switch ($SwitchModel) { | |
'CN1610' { | |
$SwitchMake = 'Brocade' | |
} | |
'NX3132V' { | |
$SwitchMake = 'Cisco' | |
} | |
} | |
#we should always get a match, but it will only be useful for Brocade data | |
if ($record -match 'Serial Number: (.+)'){ | |
$SwitchSerial = $matches[1] | |
} | |
#cisco serial numbers are embedded between brackets in the serial number so use the match operator to test and grab it. | |
if ($SwitchName -match '\(([^)]+)\)') { | |
$SwitchSerial = $matches[1] | |
} | |
$switchname | |
$SwitchIP | |
$SwitchModel | |
$SwitchSerial | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment