Thursday, January 21, 2016

Joe's Super Simple Windows Powershell Log Parser

Hello IT Pros!

I’ve got another really helpful post today. It is a very simple log file parser for Windows Powershell. This script was built when my colleagues and I needed a way to monitor the count of specific failures in an application by parsing through it’s log file for the error entries. Here is the code:

$logcount = Get-Content -path 'C:\PATH_TO_LOG_FILE\logfile.log' | Select-String -pattern "ERROR" –simplematch
If ($logcount -eq 0) { Write-Host 'Statistic: ' $logcount }
Else { Write-Host 'Statistic: ' $logcount.count }

As you can see, yes this is a very simple script and was designed to be used as a SolarWinds SAM template for monitoring purposes. But it can be adapted for a variety of use cases. It should work in Powershell 2.0 but 3.0 is recommended. Enjoy!

- Joe