![](/img/trans.png)
[英]Search multiple text files for string and copy line by appending it to summary_[date].log
[英]How to search for a string in multiple text files in an active running log
我正在嘗試在多個文本文件中搜索字符串以觸發事件。 程序正在主動添加日志文件。 以下腳本成功實現了該目標,但一次僅適用於一個文本文件:
$PSDefaultParameterValues = @{"Get-Date:format"="yyyy-MM-dd HH:mm:ss"}
Get-Content -path "C:\Log 0.txt" -Tail 1 -Wait | ForEach-Object { If ($_ -match 'keyword') {
Write-Host "Down : $_" -ForegroundColor Green
Add-Content "C:\log.txt" "$(get-date) down"
不幸的是,這意味着我必須運行此腳本的 3 個實例來搜索 3 個日志文件(C:\log 0.txt、C:\log 1.txt 和 C:'log 2.txt)。
我想要做的是運行一個 powershell 腳本來在所有三個文本文件而不是三個文本文件中搜索該字符串。
我嘗試在路徑中使用通配符 ("C:\log*.txt)
我還嘗試添加一個 foreach 循環:
$PSDefaultParameterValues = @{"Get-Date:format"="yyyy-MM-dd HH:mm:ss"}
$LogGroup = ('C:\log 0.txt', 'C:\Log 1.txt', 'C:\Log 2.txt')
ForEach ($log in $LogGroup) {
Get-Content $log -Tail 1 -Wait | ForEach-Object { If ($_ -match 'keyword') {
Write-Host "Down: $_" -ForegroundColor Green
Add-Content -path "C:\log.txt" "$(get-date) down"
Add-Content -path "C:\log.txt" "$(get-date) down"
}
}
}
這讓我沒有錯誤,但它也沒有工作。
我看到其他人使用 Get-ChildItem 而不是 Get-Content 但由於這適用於一個文件......它不應該適用於多個文件嗎? 我認為這是我缺乏腳本能力。 任何幫助,將不勝感激。 謝謝。
這就是您可以將已有的相同邏輯應用於一個文件但同時用於多個日志的方式,其概念是生成與$LogGroup
數組中的日志路徑一樣多的PowerShell 實例。 每個實例都被分配並將監視1 個日志路徑,當關鍵字匹配時,它將附加到主日志文件。
這些實例被分配了相同的RunspacePool
,這有助於我們使用SemaphoreSlim
實例初始化所有實例,這有助於我們確保線程安全(一次只有1 個線程可以寫入主日志)。
using namespace System.Management.Automation.Runspaces
using namespace System.Threading
# get the log files here
$LogGroup = ('C:\log 0.txt', 'C:\Log 1.txt', 'C:\Log 2.txt')
# this help us write to the main log file in a thread safe manner
$lock = [SemaphoreSlim]::new(1, 1)
# define the logic used for each thread, this is very similar to the
# initial script except for the use of the SemaphoreSlim
$action = {
param($path)
$PSDefaultParameterValues = @{ "Get-Date:format" = "yyyy-MM-dd HH:mm:ss" }
Get-Content $path -Tail 1 -Wait | ForEach-Object {
if($_ -match 'down') {
# can I write to this file?
$lock.Wait()
try {
Write-Host "Down: $_ - $path" -ForegroundColor Green
Add-Content "path\to\mainLog.txt" -Value "$(Get-Date) Down: $_ - $path"
}
finally {
# release the lock so other threads can write to the file
$null = $lock.Release()
}
}
}
}
try {
$iss = [initialsessionstate]::CreateDefault2()
$iss.Variables.Add([SessionStateVariableEntry]::new('lock', $lock, $null))
$rspool = [runspacefactory]::CreateRunspacePool(1, $LogGroup.Count, $iss, $Host)
$rspool.ApartmentState = [ApartmentState]::STA
$rspool.ThreadOptions = [PSThreadOptions]::UseNewThread
$rspool.Open()
$res = foreach($path in $LogGroup) {
$ps = [powershell]::Create($iss).AddScript($action).AddArgument($path)
$ps.RunspacePool = $rspool
@{
Instance = $ps
AsyncResult = $ps.BeginInvoke()
}
}
# block the main thread
do {
$id = [WaitHandle]::WaitAny($res.AsyncResult.AsyncWaitHandle, 200)
}
while($id -eq [WaitHandle]::WaitTimeout)
}
finally {
# clean all the runspaces
$res.Instance.ForEach('Dispose')
$rspool.ForEach('Dispose')
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.