Summary

We have log files that always logs errors in our custom applications. However, when there is an entry that contains “ERROR123”, then we know it is something serious. Issue is that it occurs sporadically and no one checks logs 24/7 so I had built this script that checks when this occurs and it emails our team.

Requirements:

  • Logs
  • SMTP setup
  • Windows Server 2010 – 2013
  • PowerShell

Solution:

I actually created another text file to store the line number on when the error appears. When the script runs again, it reads through the log file, compares if the line number in the text file is less then the log file and updates the text file with the newest line number on the error. I also created a batch file to email us and a Windows service that runs this script every hour.

 

$lineNumberArray = @()

$lineNumberArray = get-content “D:\Logfile.txt” | Select-String -pattern “ERROR123” | select linenumber

[int]$lineNumberFromFile = Get-Content “D:\LineNumber.txt”
[int]$lineNumberFromLogFile = $lineNumberArray[$lineNumberArray.length-1].LineNumber

if($lineNumberFromFile -lt $lineNumberFromLogFile){
$lineNumberFromLogFile | out-file -filepath “D:\LineNumber.txt”
& “D:\emailLogIssue.cmd”
}