簡體   English   中英

Powershell 腳本不會繼續寫入事件日志

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

我在腳本中有以下塊檢查日志文件,如果它找到特定行,應該繼續。

$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

這會記錄事件,但永遠不會繼續執行腳本的 rest。 例如,如果我在if($_ -match "$word") {get-date}或其他任何內容中放入其他命令,它會起作用並繼續執行下一個命令。

這應該如何寫在事件查看器中並繼續?

您需要 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