我有一个powershell脚本,返回其信息将被Splunk摄取的行,以便监视DHCP Free Addresses。
输出已被大量“处理”,它看起来像这样(&&&是一个Splunk事件断路器):
Subnet=10.31.3.0
AddressesInUse=0
AddressesFree=249
&&&
Subnet=10.31.4.0
AddressesInUse=0
AddressesFree=249
&&&
Subnet=10.31.9.0
AddressesInUse=0
AddressesFree=249
&&&
Subnet=10.32.1.0
AddressesInUse=32
AddressesFree=177
&&&
Subnet=10.34.1.0
AddressesInUse=98
AddressesFree=111
&&&
虽然,我有一个包含子网值的例外列表,必须予以忽略。我正计划将它们写入文本文件,从中获取Get-Content,并将变量(从命令结果)中的子网值替换为-eq到任何文本文件行,将其转换为“”(然后我计划在Splunk中用“IfNull(SubnetValue)然后忽略事件”忽略它们。
预期结果应如下所示:
File SubnetExceptions.txt内容示例:
10.32.1.0
10.34.1.0
预期结果:
Subnet=10.31.3.0
AddressesInUse=0
AddressesFree=249
&&&
Subnet=10.31.4.0
AddressesInUse=0
AddressesFree=249
&&&
Subnet=10.31.9.0
AddressesInUse=0
AddressesFree=249
&&&
Subnet=
AddressesInUse=32
AddressesFree=177
&&&
Subnet=
AddressesInUse=98
AddressesFree=111
&&&
所以我想忽略的那些将作为Splunk的“null”值,解决我的问题:)
我在生活中做了一些替换,但从来没有从文本文件中取代。我的测试失败了。任何人都可以帮我做吗?
谢谢!
此代码将以正则表达式格式加入您的ip以替换ip
"$((Get-Content 'C:\Vincent\Test\IP.txt') -join '$|')$"
10.32.1.0$|10.34.1.0$
要么
"Subnet=$((Get-Content 'C:\Vincent\Test\IP.txt') -join '$|Subnet=')$",'Subnet='
Subnet=10.32.1.0$|Subnet=10.34.1.0$
替换IP的代码:
(Get-Content 'C:\Vincent\Test\Test.txt') -replace "$((Get-Content 'C:\Vincent\Test\IP.txt') -join '$|')$`"
要么
(Get-Content 'C:\Vincent\Test\Test.txt') -replace "Subnet=$((Get-Content 'C:\Vincent\Test\IP.txt') -join '$|Subnet=')$",'Subnet='
像这样的东西?
$ExceltpionList=get-content C:\temp\SubnetExceptions.txt
Get-Content C:\temp\spunkfile.txt | %{
$CurrentRow=$_
$ExceltpionList | %{
if ($CurrentRow -eq "Subnet=$_")
{
$CurrentRow=$CurrentRow -replace "Subnet=$_", "Subnet="
}
}
$CurrentRow
}
如果您的输出位于文件result.txt中,则可以执行以下操作:
$content = Get-Content "result.txt"
cat "subnetexceptions.txt" | %{ $content = $content -replace $_,"IGNORE"}
如果你需要保存到文件只需添加
$content > newfile.txt
我测试了它,它的工作原理。