Log Parser and PowerShell are both great tools and they work well when used together. Yes, you can do pretty much everything Log Parser does with PowerShell alone, but part of PowerShell’s mission is to better leverage current tools. I believe this is an excellent example. Also it has been my experience Log Parser performs better at the task. Steve Schofield also blogged about the performance of Log Parser and PowerShell for querying logs here.
There are two ways to interact with Log Parser from PowerShell. The first and the easiest to get started with is the command line version logparser.exe.
Here is the same command line query example from the last post but with PowerShell.
& ./logparser.exe “SELECT Top 10 cs-uri-stem, Count(*) FROM D:Logsex081110.log Group By cs-uri-stem Order by cs-uri-stem desc” –i:w3c
Big difference isn’t it. J
Here is a simple powershell script using Log Parser. This is a little easier to follow than the batch file example in the last post.
$infile = "D:TempLPTest.log"
$query = @"
SELECT Top 10 cs-uri-stem, Count(*)
Group By cs-uri-stem
Order by cs-uri-stem desc
$result = & C:LogParserlogparser.exe $query -i:w3c
The drawback to these examples is we are not really gaining the full benefit from PowerShell. We could write some functions in PowerShell and convert the text output from Log Parser into PowerShell objects, but there is an easier way to do this.
OK, now for the second way to interact with Log Parser from PowerShell, the Log Parser COM component (logparser.dll). This component installs with Log Parser and should be registered and ready to use if you have installed Log Parser.
The COM component exposes a simple object model consisting of only three main objects. The LogQuery, LogRecorset, and LogRecord objects. There are also a series of objects for the input and output formats.
The COM component is more difficult to use, but is an advantage because data can be returned in object form. We will look at this in detail starting in Log Parser and PowerShell – Part II