【问题标题】:Tail a log file and if match trigger an actrion跟踪日志文件,如果匹配则触发操作
【发布时间】:2016-11-17 16:30:31
【问题描述】:

我目前正在使用以下代码行通过 PowerShell 返回日志的最新行

Get-Content -Path C:\folder\thisisalog.log -Tail 1 -Wait |
    Where {$_ -match "Remote_http"}

这可以正常工作,并且每次记录匹配“Remote_http”的日志时都会写入控制台。

但是我想做的是在返回时运行另一个脚本。 到目前为止,我已经尝试添加到一个变量并检查它是否为空但没有成功,我尝试使用 if 语句但没有成功。 尝试这两个脚本无限期地运行,没有输出到控制台或触发器。

我认为这可能与导致问题的-Wait 有关。

【问题讨论】:

  • Where改成ForEach-Object,把-match子句变成if语句,在if块里面运行代码/脚本

标签: powershell tail


【解决方案1】:

去做吧

Get-Content -Path C:\folder\thisisalog.log -Tail 1 -Wait | % {if ($_ -match "Remote_http") {write-host "run code here"}} 

或直接进入你的位置

Get-Content -Path C:\folder\thisisalog.log -Tail 1 -Wait | where {if ($_ -match "Remote_http") {write-host "run code here"}} 

【讨论】:

    猜你喜欢
    • 2016-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    • 2011-08-09
    相关资源
    最近更新 更多