Using PowerShell, parse a multiple Cisco switch configs and:
Extract the hostname
Extract the name of all interfaces
If the interface description exists, extract
If the interface ip address exists, extract
If the ip helper exists, extract
Here's a sample string:
!
hostname SOME-HOSTNAME-01
!
…
!
interface Loopback0
ip address 192.168.1.1 255.255.255.255
ip ospf network point-to-point
!
interface FastEthernet0/0
description Some-Sample-DescriptionWithOdd/Char@s
ip address 192.168.1.2 255.255.255.0
ip helper-address 192.168.1.3
ip helper-address 192.168.1.4
duplex auto
speed auto
!
interface FastEthernet0/1
no ip address
shutdown
duplex auto
speed auto
!
When I return the value of $interfaceStatements, I was expecting to see a multiline string that ends right before the ‘!’, but I’m only seeing the first line in each:
The problem is that by default Get-Content reads each line of an input file into a new element in an array, so your $config variable is actually an array with each line as an element. You can verify this by adding
$config.Count
after the Get-Content line.
With your example input, this returns 23. Your Select-String regex is working correctly, but it is only getting the lines of the input file one at a time as separate array elements, rather than the entire block of text all at once.
You can fix this by adding the -Raw parameter to Get-Content:
$config = Get-Content -Path $configPath -Raw
In my testing, this returned the expected output (with no other changes to the script):
interface Loopback0
ip address 192.168.1.1 255.255.255.255
ip ospf network point-to-point
interface FastEthernet0/0
description Some-Sample-DescriptionWithOdd/Char@s
ip address 192.168.1.2 255.255.255.0
ip helper-address 192.168.1.3
ip helper-address 192.168.1.4
duplex auto
speed auto
interface FastEthernet0/1
no ip address
shutdown
duplex auto
speed auto