继续从文件中读取,寻找一些关键字

发布于 2025-02-11 19:12:58 字数 927 浏览 2 评论 0 原文

我有一个日志文件,需要继续监视该文件查找某些关键字,我创建了一个PowerShell脚本,并使用 get-content 命令使用 -Wait flag,但是, get-content 只是读取第一个关键字,如果我删除 -Wait 它可以正常工作并阅读所有关键字,但是它将在完成后退出,我希望文件以继续等待附加行。

请就此告知我。

这是我的代码:

$date = Get-date -Format "yyyyMMdd"
$MyError = @('error 1', 'error 2', 'error 3')
$file=$date.log"
foreach ($i in $MyError) {
Get-Content "$file" -Wait | Select-String -Pattern $i -SimpleMatch | ForEach-Object {
        if ($i -like 'error 1') {
            Write-EventLog -source Test -LogName Test -EventId 1001   -EntryType Error -Message " "
        }

        if ($i -like 'error 2') {
            Write-EventLog -source Test -LogName Test -EventId 1002   -EntryType Error -Message " "
        }

        if ($i -like 'error 3') {
            Write-EventLog -source Test -LogName Test -EventId 1003   -EntryType Error -Message " "
        }
    }
}

I have a log file that needs to keep monitored that file looking for some keywords, I created a PowerShell script and used the get-content command with the -wait flag but, get-content is just reading the first keyword, and If I remove -wait it will work fine and read all keywords, but it will exit after finished, and I want the file to keep waiting for the appended lines.

Please advise me on this.

This is my code:

$date = Get-date -Format "yyyyMMdd"
$MyError = @('error 1', 'error 2', 'error 3')
$file=$date.log"
foreach ($i in $MyError) {
Get-Content "$file" -Wait | Select-String -Pattern $i -SimpleMatch | ForEach-Object {
        if ($i -like 'error 1') {
            Write-EventLog -source Test -LogName Test -EventId 1001   -EntryType Error -Message " "
        }

        if ($i -like 'error 2') {
            Write-EventLog -source Test -LogName Test -EventId 1002   -EntryType Error -Message " "
        }

        if ($i -like 'error 3') {
            Write-EventLog -source Test -LogName Test -EventId 1003   -EntryType Error -Message " "
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

私藏温柔 2025-02-18 19:12:58

代码的问题是 -Wait 阻止主线程,无限期等到 ctrl+ctrl+c 。因此,您的外部 foreach 循环一次,因此 $ i 始终是错误1 。解决问题的一个简单解决方案是循环循环 foreach-object ,而且还使用a switch> switch -wildcard 将简化您的代码:

Get-Content path\to\file.txt -Wait | ForEach-Object {
    switch -Wildcard ($_) {
        '*error 1*' {
            # do something for error 1
        }
        '*error 2*' {
            # do something for error 2
        }
        '*error 3*' {
            # do something for error 3
        }
    }
}

The issue with your code is that -Wait blocks the main thread, waits indefinitely until CTRL+C. Because of this, your outer foreach loops only once, so $i is always error 1. An easy fix for your problem is to loop inside the ForEach-Object, but also, using a switch -Wildcard would simplify your code:

Get-Content path\to\file.txt -Wait | ForEach-Object {
    switch -Wildcard ($_) {
        '*error 1*' {
            # do something for error 1
        }
        '*error 2*' {
            # do something for error 2
        }
        '*error 3*' {
            # do something for error 3
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文