我正在编写一个脚本,该脚本将下载当前的Tor退出节点列表,并检查本地日志文件是否可以从Tor访问。
下面的代码将日志文件中的Tor it输出匹配到控制台,但我希望将foreach循环中的Tor输出输出到一个文件中(除了将其写入控制台)。
一旦我将结果输出到一个文件,我将需要检查结果,以确定是否有匹配。
任何关于如何将foreach Select-String输出到文件以及如何检查匹配的提示都将不胜感激。
$source = "https://check.torproject.org/cgi-bin/TorBulkExitList.py?ip=[serverip]&port=[port]"
$dest = "C:\tor.txt"
$wc = New-Object System.Net.WebClient
$wc.DownloadFile($source, $dest)
Get-Content "C:\tor.txt" | where { $_ -notmatch "^#" } | Set-Content "C:\filteredtor.txt"
Remove-Item "C:\tor.txt"
$list = get-content "C:\filteredtor.txt"
ForEach ($_ in $list) {Select-String "C:\logfile.log" -pattern $_}发布于 2014-06-18 08:17:01
Tee-Object cmdlet将将输入流的副本保存到文件中,同时将其传递给输出流。此外,通过使用DownloadString()而不是DownloadFile(),您可以将代码简化为单个管道,并消除对临时文件的需求:
$source = 'https://...'
$log = 'C:\logfile.log'
$results = 'C:\results.txt'
$wc = New-Object System.Net.WebClient
($wc.DownloadString($source)) -split "`n" | ? { $_ -notmatch '^(#|\s*$)' } | % {
Select-String $log -pattern $_ | Tee-Object $results -Append
}
if ((Test-Path -LiteralPath $results) -and (Get-Content $results)) {
# do something
}https://stackoverflow.com/questions/24277414
复制相似问题