I'm trying to setup a scheduled task that will read the content of a text file and look for a pattern (error messages) and then send an alert. The scheduled task will run every x mins and will read the same file. However, this script has to "remember" where it left off on the last read as to 1) avoid false positives (errors already reported) and 2) save time by reading only 'new' content.
Hoping that someone out there already has overcome this challenge and can point me in the right direction.
I found this piece of code (http://www.archivum.info/microsoft.public.windows.powershell/2008-04/00134/RE-Read-a-huge-text-file-from-bottom-up.html
) that uses size to avoid processing the entire file at every iteration, however not sure if size is a good option here.
You're input is appreciated. Thanks.
$TextFilePath = "C:\myfile.log"
$BytesToRead = 1024
$fs = [System.IO.File]::OpenRead($TextFilePath)
$fs.Position = $fs.Length - $BytesToRead
$sr = New-Object System.IO.StreamReader($fs)
$text = $sr.ReadToEnd()