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

Be the first to rate this post

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

Tags: , , , ,

Debugging | PowerShell | Wonderware

Related posts

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

9/7/2008 5:24:18 AM

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

<<  September 2008  >>
MoTuWeThFrSaSu
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

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 2008

Sign in