So I have a array that I need to search through line by line and divide up by interfaces. My code loops through this file line by line. I want to split up the interfaces by the "!" character and add the the strings to an element in an array so I can do further parsing.
Here's what the content of the file looks like.
!
interface Loopback0
description MANAGEMENT
ip address 172.xxx.xxx.x
!
interface FastEthernet0/0
description m<1> A<LAN on chr-city>
no ip address
ip flow ingress
duplex auto
speed auto
!
interface FastEthernet0/0.50
description Management
encapsulation dot1Q 50 native
ip address 172.xxx.xxx.x
!
interface FastEthernet0/0.51
description Transit
encapsulation dot1Q 51
ip address 172.xxx.xxx.x
service-policy input mark
!
interface FastEthernet0/1
no ip address
shutdown
duplex auto
speed auto
!
interface Serial0/0/0
description m<1> WAN<> V<CL>
bandwidth 1536
ip address 172.xxx.xxx.x
ip flow ingress
no ip mroute-cache
service-module t1 timeslots 1-24
no cdp enable
service-policy output shape
!
router bgp 65052
search the config archive file code
for ($m=0; $m -lt $configFileContents.length; $m++) {
$index = 0
if($configFileContents[$m] -eq "interface Loopback0"){ #starting spot
$a = @()
While($configFileContents[$m] -notmatch "router bgp") { #ending spot
if($configFileContents[$m] -ne "!") { #divide the elements
$a[$index] += $configFileContents[$m]
$m++
} else {
$index++
$m++
}
}
Write-Host "interface archive section" -BackgroundColor Green
$a
Write-Host "end of interface archive section"
}`
Question: How do I add all the interface strings between the "!" to one element in my array and all the next ones to the second element and so on?
Updated Code
$raw = [IO.File]::ReadAllText("$recentConfigFile")
$myArr = @()
$raw.Split("!") | % {$myArr += ,$_.Split("`n")}
$i = 0
$myArr | % {
if ($_[0].Trim() -eq "interface Loopback0") {
$start = $i
} elseif ($_[0].Trim() -eq "router bgp 65052") {
$end = $i
}
$i++
}
$myArr | Select-Object -Skip $start -First ($end-$start)