Delyan
Delyan

Reputation: 85

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 be made to write in event viewer and continue?

Upvotes: 0

Views: 56

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174445

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

Upvotes: 2

Related Questions