简体   繁体   English

Powershell 脚本不会继续写入事件日志

[英]Powershell script does not continue on write-eventlog

I have the following block in script that checks a log file and if it finds a specific line, should continue.我在脚本中有以下块检查日志文件,如果它找到特定行,应该继续。

$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

This logs the event but never continues with the rest of the script.这会记录事件,但永远不会继续执行脚本的 rest。 If I put some other command in if($_ -match "$word") {get-date} for example or anything else, it works and continues to the next command.例如,如果我在if($_ -match "$word") {get-date}或其他任何内容中放入其他命令,它会起作用并继续执行下一个命令。

How should this should be made to write in event viewer and continue?这应该如何写在事件查看器中并继续?

You need to output something for the Select -First 1 statement to react to:您需要 output一些东西才能使Select -First 1语句做出反应:

$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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM