我正在嘗試通過防火墻規則 (100 ) 的 .txt 轉儲進行決議。我想在每個規則中搜索特定引數(例如設定 ips-sensor)。如果該規則包含引數,我希望腳本列印“找到的值”。如果沒有,我希望腳本列印“未找到值”。我將每個規則視為一個段落,并在“下一個”上使用了分隔符。我無法讓 foreach 回圈作業。這是我所擁有的:
$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"
}
}
以下是確切格式的示例:
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
我的預期輸出應該是 -
value not found
value found
value found
任何幫助表示贊賞。
uj5u.com熱心網友回復:
好問題,我在代碼上添加了注釋,以便您可以遵循思考程序。
# Define the word we want to match
$find = 'ips-sensor'
# Set Paragraph counter
$z = 1
# $file will be an array with 3 elements / paragraphs given the example we got
foreach($paragraph in $file)
{
# Set the line counter (this will be reset on each paragraph)
$i = 0
# Set a result hashtable and assume we will not find the word we're looking for
$out = [ordered]@{
Paragraph = $z
Status = 'Not Found'
LineNumber = ''
Line = ''
}
# Split this paragraph on new lines and filter only those elements that are
# not empty strings and loop through it
foreach($line in $paragraph -split '\r?\n' -ne '')
{
# Increment the line counter
$i
# Self explanatory
if($line -match $find)
{
$out.Status = 'Found'
$out.LineNumber = $i
$out.Line = $line.Trim()
# Break the inner loop, if we are here we don't need to keep looping
# over this array
break
}
}
# Cast a pscustomobject which can be easily manipulated and exported
[pscustomobject]$out
# Increment Paragraph counter
$z
}
結果
Paragraph Status LineNumber Line
--------- ------ ---------- ----
1 Not Found
2 Found 12 set ips-sensor "default"
3 Found 10 set ips-sensor "default"
如果您想在轉儲檔案中找到多個單詞,則需要對代碼進行一些修改:
$find = 'ips-sensor', 'naughty' -join '|'
$z = 1
foreach($paragraph in $file)
{
$i = 0
$out = [ordered]@{
Paragraph = $z
Status = 'Not Found'
LineNumber = ''
Line = ''
}
foreach($line in $paragraph -split '\r?\n' -ne '')
{
$i
if($line -match $find)
{
$out.Status = 'Found'
$out.LineNumber = $i
$out.Line = $line.Trim()
[pscustomobject]$out
}
}
if($out.Status -eq 'Not Found')
{
[pscustomobject]$out
}
$z
}
結果
Paragraph Status LineNumber Line
--------- ------ ---------- ----
1 Not Found
2 Found 12 set ips-sensor "default"
3 Found 2 set name "Naughty IPs"
3 Found 10 set ips-sensor "default"
uj5u.com熱心網友回復:
由于您已經將文本分成塊,因此可以將它們傳遞給 foreach-object,您只需要測驗是否找到了所需的字串。
Get-Content C:\firewall_rules.txt -Delimiter 'next' | ForEach-Object {
if($_ -Match 'set ips-sensor'){
"Value found"
}
else{
"Value not found"
}
}
這會產生您想要的輸出。
Value not found
Value found
Value found
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/348779.html
標籤:电源外壳
