简体   繁体   中英

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. 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.

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:

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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