Use Powershell to find SMS errors

I was working onsite with a client last week and we were testing SMS V4 (SCCM 2007) OS Deployment features.  While we were working we were having numerous client connectivity errors and often had to pull up good ol' notepad or SMS Trace to find the errors.

That got me thinking that there has to be a faster way to quickly identify errors in the SMS event log so I started playing around with Powershell.  Powershell has a concept called a filter that you can create to manipulate objects as they pass through the pipeline.  Wouldn't it be great if I could read the SMS log file and pull out any error messages?

Well this small bit of code does just that:

filter finderrors { if ($_.contains("error")) {$_}}

This creates a filter called finderors.  You can then pass this filter any log or text file and it will analyze the file line by line.  If a line contains an error it will return that to the screen.  Here is an example command:

get-content ccmexec.log | finderrors

If you want to output the errors to a file just run the command like this:

get-content ccmexec.log | finderrors | out-file errors.txt

You could also go ahead and open it up in notepad at the same time:

get-content ccmexec.log | finderrors | out-file errors.txt | notepad errors.txt

What's cool about this filter is you can now pipe in multiple log files like this:

dir *.log | foreach-object {get-content $_ | finderrors} | out-file errors.txt | notepad errors.txt

I know that this produces some pretty raw output but with a little string massaging you could have the filter return only the specific eror messages and time of the error.

Enjoy and have a great 4th!

Comments