Extracting errors and warnings from a log file using PowerShell

by Klaus Graefensteiner 7/21/2008 9:04:46 AM

Introduction

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

Procedure

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

Currently rated 2.0 by 1 people

  • Currently 2/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , ,

Debugging | PowerShell | Wonderware

Related posts

Comments are closed

Powered by BlogEngine.NET 1.3.0.0
Vanilla Theme by Klaus Graefensteiner

About Klaus Graefensteiner

GRAVATAR icon of Klaus Graefensteiner I enjoy the programming of machines.

E-mail me Send mail
Blogroll as OPML OPML LinkedIn Profile View Klaus Graefensteiner's LinkedIn profile

Calendar

<<  July 2009  >>
MoTuWeThFrSaSu
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

View posts in large calendar

Recent comments

Disclaimer

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

© Copyright 2009

Sign in