Hi Everyone
I've been working on a side project of mine that helps me learn powershell and incorporate other languages, and I've hit a bit of a roadblock.
My powershell script basically reads the latest entry (the last line) of a logfile everytime the logfile has been updated. When the logfile updates, the script reads the last line of the logfile and searches for 2 specific words; GameStart and GameEnd.
Once it finds 'GameStart' it is supposed to remember the LineNumber and continue to read the latest entries until it finds 'GameEnd' and remember its LineNumber.
The problem I'm having is no matter the line position, GameStart is always report LineNumber 0 and GameEnd is relevant to only the position of GameStart, and not the actual LineNumber. (So if GameEnd is found 77 lines down, it reports LineNumber 77 which is
not the actual LineNumber)
I'm using this section of code to monitor the logfile for updates:
http://dereknewton.com/2011/05/monitoring-file-system-changes-with-powershell/
For testing purposes, I just have the below lines to confirm if the position of GameStart and GameEnd reports back with the correct line number:
$changed = Register-ObjectEvent $watcher1 "Changed" -Action {
$GameStart = Get-Content 'c:\path-to-log\logfile.log' -Tail 1 | select-string -pattern '(GameStart)'
$GameEnd = Get-Content 'c:\path-to-log\logfile.log' -Tail 1 | select-string -pattern '(GameEnd)'
if ($GameStart)
{
Write-Host $(Get-Date -format T) "GameStart entry found on line "$GameStart.LineNumber
}
else ($GameEnd)
{
Write-Host $(Get-Date -format T) "GameStart entry found on line "$GameEnd.LineNumber
}
}
Any ideas to get the GameStart and GameEnd to report back the actual correct LineNumber?