I am trying to parse through a .txt dump of firewall rules (100+). I want to search each rule for a specific parameter (ex. set ips-sensor). If that rule contains the parameter, I want the script to print "value found". If not, I want the script to print "value not found". I have treated each rule as a paragraph and used a delimiter on "next". I cannot get the foreach loop to work. Here is what I have:
$paragraphs = Get-Content C:\firewall_rules.txt -Delimiter "next"
#$line = $paragraphs.Split([string[]]"`r`n", [StringSplitOptions]::None)
$exists = $paragraphs | Where-Object { $_ -like '*set ips-sensor*' }
$noexists = $paragraphs | Where-Object {$_ -notlike '*set ips-sensor*'}
$result = foreach ($line in $paragraphs) {$line.Split([string[]]"`r`n", [StringSplitOptions]::None)}
foreach ($result in $paragraphs)
{
if (?)
{
Write-Host "value found"
}
elseif (?)
{
Write-Host "value not found"
}
}
Here is an example of the exact format:
edit 168
set name "Office Stuff"
set srcintf "Office"
set dstintf "port11"
set srcaddr "overthere"
set internet-service enable
set action accept
set schedule "always"
set logtraffic all
set fsso disable
set nat enable
next
edit 174
set name "My Policy"
set srcintf "Office"
set dstintf "port1"
set srcaddr "overthere"
set dstaddr "all"
set action accept
set schedule "always"
set service "ALL"
set utm-status enable
set logtraffic all
set ips-sensor "default"
next
edit 68
set name "Naughty IPs"
set srcintf "Office"
set dstintf "port1"
set srcaddr "all"
set dstaddr "faraway"
set schedule "always"
set service "ALL"
set logtraffic all
set ips-sensor "default"
set fsso disable
my expected output should be -
value not found
value found
value found
Any help is appreciated.