我们希望在 powershell 中“实时”追踪特定字符串的几个日志文件。我们尝试使用“get-content”命令,但没有成功,因为每次结果我们只收到一个文件的结果。我认为这是由“-wait”参数引起的。是否还有其他方法可以尾部多个文件?我听说过运行空间,但仍然不知道如何实现它。
下面是我们的示例脚本
dir d:\test\*.txt -include *.txt | `
Get-Content -Wait | `
select-string "windows" | `
ForEach-Object {Write-EventLog -LogName Application -Source "Application error" -EntryType information -EventId 999 -Message $_}
任何帮助将不胜感激。
最佳答案
聚会可能会稍微迟到......
根据 here ,我们可以在 .ps1 文件中使用以下内容;
$ProgressPreference='SilentlyContinue'
Workflow My-Tail
{
Param([string[]] $Path)
foreach -parallel ($file in $path)
{
Get-Content -Path $file -Tail 1 -Wait
}
}
这让我们可以跟踪多个文件,在 powershell 中调用,例如;
My-Tail (dir C:\Users\User\Documents\*.log)
剩下的就是按所需的字符串进行过滤;
My-Tail (dir C:\Users\User\Documents\*.log) | Select-String -Pattern ".*ERROR.*"
这将跟踪文档中以 .log 结尾的所有文件,输出包含文本“ERROR”的行
关于powershell - 如何在 Powershell 中跟踪日志文件以查找特定字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26545353/