Skip to content

Instantly share code, notes, and snippets.

@DrBob5188
Created July 19, 2019 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DrBob5188/841acef65713ba66e9d4a09b4405a1ce to your computer and use it in GitHub Desktop.
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/
# 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