0

I have a script that is supposed to look at a logfile that starts filling with several hundred lines. While the log is filling, I need to look for two different conditions at the same time. If I find "Completed Successfully" then it will break the while loop and continue with the rest of the script. If it finds "Error" in the script, it will restart the server. Unfortunately this isn't working for me. It just sits until the timeout of the stopwatch. I'm not quite sure what is wrong here. Is there a better way to do this?

$Comp="ServerA.domain"
$Logfile="\\$Comp\Logs\logfile1.txt"
$pattern1 = "Completed Successfully"
$Pattern2 = "Error"
$timeout = new-timespan -Minutes 5
$stopwatch = [diagnostics.stopwatch]::StartNew()
while ($stopwatch.elapsed -lt $timeout){
    try{
        $logContent2 = Get-Content -Path $Logfile -Tail 1000 | Select-String -Pattern $Pattern2 | Measure-Object | Select-Object -ExpandProperty Count
        $logContent1 = Get-Content -Path $Logfile -Tail 1000 | select-string -pattern $pattern1 | Measure-Object | Select-Object -ExpandProperty Count
        If ($logContent1 -gt 0) {
            Write-Host -Object "Compiled Successfully on $Comp" 
            #break;
        }
        ElseIf ($logContent2 -gt 0) {
            Write-Host -Object "Error Found on $Comp! Restart required. Rebooting..."
            Restart-Computer -ComputerName $Comp -Wait -For PowerShell -Force
        }
    }
    Catch{
        $Error[0] > \\$Comp\Logs\ErrorLog.txt
    }
}
If ($stopwatch.elapsed -ge $timeout){
    Write-Error -Message "$pattern1 did not appear" -ErrorAction Stop
    exit;
}
alsoszaa
  • 13
  • 5
  • Any error messages? Also: When using `try{}Catch{}`, be aware that only terminating errors are caught. In most cmdlets you have to force the cmdlets to throw terminating errors like so: `get-content -erroraction stop` – hcm Dec 17 '19 at 20:22
  • Your script above _should_ achieve what you are trying to accomplish. Is it possible that neither of the conditions are being met in the 1000 line tail limit? How quickly is this log file populating with new lines? – infosecb Dec 17 '19 at 20:22
  • Not from my tries at this to date. These should be one session per log file. I use tail on a regular basis for similar use cases, though, mine is always for different logs to monitor operational activity between the logs of a target slution. Even for separate files they must be separate sessions, at least from all my experiments / use case to date. In your case, it is a different challenge, since you are going after the same file. – postanote Dec 17 '19 at 20:26
  • hcm - Thank you. I will note that for Try/catch! Also, no errors occur here, just a timeout. infosecb - I checked, and one or the other conditions are met. Population of the lines are fairly quick, about 20 lines a second postanote - Could this possibly be that you can do this on a completed logfile (no longer growing) but not a growing logfile? – alsoszaa Dec 17 '19 at 20:31
  • @alsoszaa One or the other conditions are met? I don't understand. Is one of the conditions met? If I understand correctly, it isn't. So did you check if there actually are lines containing one of your patterns? Maybe create a test file with a few lines definitely containing your patterns... – hcm Dec 17 '19 at 20:34
  • I am running this in Powershell ISE with a breakpoint to see what is happening. Apparently I may have a "break;" in the wrong position. I'm still digging. – alsoszaa Dec 17 '19 at 22:21
  • @hcm If you want the default behavior to throw terminating errors, you can also set `$ErrorActionPreference = 'Stop'` in your script. – codewario Dec 17 '19 at 22:53

1 Answers1

0

Yes! I accidentally placed a break in the wrong place. This is resolved. The above code works but you need to break the loop (I had a break in my code in the wrong place) and I don't include the break in the code in this example.

alsoszaa
  • 13
  • 5