Extracting errors and warnings from a log file using PowerShell

by Klaus Graefensteiner 21. July 2008 10:04


This blog post shows a simple Powershell command that searches a log file for errors and warnings and adds the matched lines to either to the errors.txt file or to the warnings.txt files. It takes advantage of the most powerful operator in Powershell, which is the switch operator. In my example here I exported the log file from the Wonderware SMC logger.

Thermal image of a rocket engine

Figure 1: Thermal image of a rocket engine exhaust


Step 1: Export a logger text file

Open the Wonderware SMC, navigate to the \Log Viewer\Default Group\Local node, select it and choose the export log messages from the right mouse click menu.

Exporting Wonderware Logger messages as log.txt file

Figure 2: Exporting Wonderware Logger messages as log.txt file

Save the log messags as log.txt file on the desktop

Step 2: Fire up Powershell

Type the following command to search for errors and warnings and save them in their respective files.

   1: cd $home\desktop\
   2: switch -wildcard (${c:log.txt}) 
   3: {
   4:     *error*     {Add-Content -path errors.txt $_; continue;}
   5:     *warning*     {Add-Content -Path warnings.txt $_}
   6: }

That's it

If there are any errors or warnings, then you will find them in the errors.txt or warnings.txt files. The performance is not too bad. A 66.7 MB log file with 282080 message lines took about 25 seconds on my 4 year old laptop computer. The script file and the resulting errors.txt and warnings.txt files can be downloaded here: SearchLogForErrors.zip

kick it on DotNetKicks.com

Tags: , , , ,

Debugging | PowerShell | Wonderware

Comments are closed

About Klaus Graefensteiner

I like the programming of machines.

Add to Google Reader or Homepage

LinkedIn FacebookTwitter View Klaus Graefensteiner's profile on Technorati
Klaus Graefensteiner

Klaus Graefensteiner
works as Developer In Test and is founder of the PowerShell Unit Testing Framework PSUnit. More...

Open Source Projects

PSUnit is a Unit Testing framwork for PowerShell. It is designed for simplicity and hosted by Codeplex.
BlogShell is The tool for lazy developers who like to automate the composition of blog content during the writing of a blog post. It is hosted by CodePlex.



Powered by:

Creative Commons License

© Copyright 2015, Klaus Graefensteiner.

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Theme design:
This blog theme was designed and is copyrighted 2015 by Klaus Graefensteiner

Page rendered at 5/26/2015 8:33:25 PM (PST Pacific Standard Time UTC DST -7)