Powershell脚本未在写入事件日志时继续

siv3szwd  于 2023-01-17  发布在  Shell
关注(0)|答案(1)|浏览(94)

我在脚本中有以下代码块,用于检查日志文件,如果找到特定行,则应继续。

$keywords=Get-Content "C:\Users\user\desktop\keywords.txt"
Get-Content "C:\Users\user\desktop\some.log" -tail 1 -wait |
ForEach-object {
foreach($word in $keywords) {
if($_ -match "$word") {
Write-EventLog -LogName Application -EventID 2001 -EntryType Information -Source serviceCheck -Message "[SUCCESS] The service has been initialized"
Write-Host "[SUCCESS] The service has been initialized" 
}
}
} | Select -First 1

这记录了事件,但从不继续脚本的其余部分。如果我在if($_ -match "$word") {get-date}中放了一些其他命令,例如或其他任何命令,它会工作并继续下一个命令。
如何在事件查看器中写入并继续?

cgvd09ve

cgvd09ve1#

您需要输出 something,以便Select -First 1语句做出React:

$keywords = Get-Content "C:\Users\user\desktop\keywords.txt"
Get-Content "C:\Users\user\desktop\some.log" -tail 1 -wait |ForEach-object {
    foreach ($word in $keywords) {
        if ($_ -match "$word") {
            Write-EventLog -LogName Application -EventID 2001 -EntryType Information -Source serviceCheck -Message "[SUCCESS] The service has been initialized"
            Write-Host "[SUCCESS] The service has been initialized" 
            "literally any value will do!"
        }
    }
} | Select -First 1 |Out-Null

相关问题