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.

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.

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