将文本文件中的IP与html文件进行比较

时间:2018-09-10 07:54:26

标签: powershell

我有一个包含许多IP的文本文件,我想将其与html文件进行比较。

我在Powershell中有一些脚本,但是输出不包含确切的IP,而是IP从最后一个八位字节开始。

$file1 = Get-Content H:\Downloads\hnames.txt
$file2 = Get-Content H:\Downloads\cpdb2web_B976151009_1\Outputs\test.html

$result = "H:\Downloads\names.txt"

$file1 | foreach {
    $match = $file2 -match $_
    if ( $match ) {
        $match | Out-File -Force $result -Append
    }
}

hnames.txt(示例文本文件)

10.66.94.16

output.txt

<a href="#network_object_Net_10.66.94.16_n28">Net_10.66.94.16_n28</a>
<a href="#network_object_Net_10.66.94.16_n28">Net_10.66.94.16_n28</a>
<td vAlign="top"><a name="network_object_Net_10.66.94.16_n28">Net_10.66.94.16_n28</a></td><td vAlign="top">
    </td><td vAlign="top">10.66.94.16<br>
    </td><td vAlign="top">10.66.94.168<br>
    </td><td vAlign="top">10.66.94.162<br>
    </td><td vAlign="top">10.66.94.164<br>

我尝试了另一个脚本,但是它也无法正常工作

$File1 = Get-Content H:\Downloads\hnames.txt
$File2 = Get-Content H:\Downloads\cpdb2web_B976151009_1\Outputs\test.html

$RegEx = "("
ForEach ($Line in $File1){
    $RegEx += "$Line|"
}

$RegEx = [regex]"$($RegEx.SubString(0,($RegEx.Length - 1))))"   #trim the last | out, we don't need it

$Search = $File2 | Select-String -Pattern $RegEx -AllMatches

ForEach($Match in $Search.Matches){
    Write-Output "$($Match.Value) was found in firewall"
}

1 个答案:

答案 0 :(得分:0)

只是稍作改动(未经测试),使用$_而不是match作为您想要的IP地址。

$file1 = Get-Content H:\Downloads\hnames.txt
$file2 = Get-Content H:\Downloads\cpdb2web_B976151009_1\Outputs\test.html

$result = "H:\Downloads\names.txt"

$file1 | Foreach-Object -Process {
    $match = $file2 -match $_
    if ( $match ) {
        $matches[0] | Out-File -Force $result -Append
    }
}