# ---------------------------------------------------------------------------
### <library name="Muegge_LogParser_Lib.ps1">
### <author>David Muegge</author>
### <createdate>20081108</createdate>
### <modifieddate>20081121</modifieddate>
### <description>
### Log Parser function library
### </description>
### <dependencies>
### Log Parser 2.2 COM component
### </dependencies>
### <usage>
### Dot source from calling script
### </usage>
### </library>
# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
### <function name="Get-LPInputFormat">
### <description>
### Returns Log Parser Input Format object based on passed string
### </description>
### <usage>
### Get-LPInputFormat -InputType <string>
### </string></usage>
### </function>
# ---------------------------------------------------------------------------
function Get-LPInputFormat{
param([String]$InputType)
switch($InputType.ToLower()){
"ads"{$inputobj = New-Object -comObject MSUtil.LogQuery.ADSInputFormat}
"bin"{$inputobj = New-Object -comObject MSUtil.LogQuery.IISBINInputFormat}
"csv"{$inputobj = New-Object -comObject MSUtil.LogQuery.CSVInputFormat}
"etw"{$inputobj = New-Object -comObject MSUtil.LogQuery.ETWInputFormat}
"evt"{$inputobj = New-Object -comObject MSUtil.LogQuery.EventLogInputFormat}
"fs"{$inputobj = New-Object -comObject MSUtil.LogQuery.FileSystemInputFormat}
"httperr"{$inputobj = New-Object -comObject MSUtil.LogQuery.HttpErrorInputFormat}
"iis"{$inputobj = New-Object -comObject MSUtil.LogQuery.IISIISInputFormat}
"iisodbc"{$inputobj = New-Object -comObject MSUtil.LogQuery.IISODBCInputFormat}
"ncsa"{$inputobj = New-Object -comObject MSUtil.LogQuery.IISNCSAInputFormat}
"netmon"{$inputobj = New-Object -comObject MSUtil.LogQuery.NetMonInputFormat}
"reg"{$inputobj = New-Object -comObject MSUtil.LogQuery.RegistryInputFormat}
"textline"{$inputobj = New-Object -comObject MSUtil.LogQuery.TextLineInputFormat}
"textword"{$inputobj = New-Object -comObject MSUtil.LogQuery.TextWordInputFormat}
"tsv"{$inputobj = New-Object -comObject MSUtil.LogQuery.TSVInputFormat}
"urlscan"{$inputobj = New-Object -comObject MSUtil.LogQuery.URLScanLogInputFormat}
"w3c"{$inputobj = New-Object -comObject MSUtil.LogQuery.W3CInputFormat}
"xml"{$inputobj = New-Object -comObject MSUtil.LogQuery.XMLInputFormat}
}
return $inputobj
}
# ---------------------------------------------------------------------------
### <function name="Get-LPOutputFormat">
### <description>
### Returns Log Parser Output Format object based on passed string
### </description>
### <usage>
### Get-LPOutputFormat -OutputType <string>
### </string></usage>
### </function>
# ---------------------------------------------------------------------------
function Get-LPOutputFormat{
param([String]$OutputType)
switch($OutputType.ToLower()){
"csv"{$outputobj = New-Object -comObject MSUtil.LogQuery.CSVOutputFormat}
"chart"{$outputobj = New-Object -comObject MSUtil.LogQuery.ChartOutputFormat}
"iis"{$outputobj = New-Object -comObject MSUtil.LogQuery.IISOutputFormat}
"sql"{$outputobj = New-Object -comObject MSUtil.LogQuery.SQLOutputFormat}
"syslog"{$outputobj = New-Object -comObject MSUtil.LogQuery.SYSLOGOutputFormat}
"tsv"{$outputobj = New-Object -comObject MSUtil.LogQuery.TSVOutputFormat}
"w3c"{$outputobj = New-Object -comObject MSUtil.LogQuery.W3COutputFormat}
"tpl"{$outputobj = New-Object -comObject MSUtil.LogQuery.TemplateOutputFormat}
}
return $outputobj
}
# ---------------------------------------------------------------------------
### <function name="Invoke-LPExecute">
### <description>
### Executes a Log Parser Query and returns a recordset
### </description>
### <usage>
### Invoke-LPExecute -query <string>
### </string></usage>
### </function>
# ---------------------------------------------------------------------------
function Invoke-LPExecute{
param([string] $query, $inputtype)
$LPQuery = new-object -com MSUtil.LogQuery
if($inputtype){
$LPRecordSet = $LPQuery.Execute($query, $inputtype)
}
else
{
$LPRecordSet = $LPQuery.Execute($query)
}
return $LPRecordSet
}
# ---------------------------------------------------------------------------
### <function name="Invoke-LPExecuteBatch">
### <description>
### Executes Log Parser batch query with passed input and output types
### </description>
### <usage>
### Invoke-LPExecuteBatch -query <string> -inputtype <logparserinputformat> -outputtype <logparseroutputformat>
### </logparseroutputformat></logparserinputformat></string></usage>
### </function>
# ---------------------------------------------------------------------------
function Invoke-LPExecuteBatch{
param([string]$query, $inputtype, $outputtype)
$LPQuery = new-object -com MSUtil.LogQuery
$result = $LPQuery.ExecuteBatch($query, $inputtype, $outputtype)
return $result
}
# ---------------------------------------------------------------------------
### <function name="Get-LPRecord">
### <description>
### Returns PowerShell custom object from Log Parser recordset for current record
### </description>
### <usage>
### Get-LPRecord -rs <recordset>
### </recordset></usage>
### </function>
# ---------------------------------------------------------------------------
function Get-LPRecord{
param($LPRecordSet)
$LPRecord = new-Object System.Management.Automation.PSObject
if( -not $LPRecordSet.atEnd())
{
$Record = $LPRecordSet.getRecord()
for($i = 0; $i -lt $LPRecordSet.getColumnCount();$i++)
{
$LPRecord | add-member NoteProperty $LPRecordSet.getColumnName($i) -value $Record.getValue($i)
}
}
return $LPRecord
}
# ---------------------------------------------------------------------------
### <function name="Get-LPRecordSet">
### <description>
### Executes a Log Parser Query and returns a LogRecordSet as a custom powershell object
### </description>
### <usage>
### Get-LPRecordSet -query <string>
### </string></usage>
### </function>
# ---------------------------------------------------------------------------
function Get-LPRecordSet{
param([string]$query)
# Execute Query
$LPRecordSet = Invoke-LPExecute $query
$LPRecords = new-object System.Management.Automation.PSObject[] 0
for(; -not $LPRecordSet.atEnd(); $LPRecordSet.moveNext())
{
# Add record
$LPRecord = Get-LPRecord($LPRecordSet)
$LPRecords += new-Object System.Management.Automation.PSObject
$RecordCount = $LPQueryResult.length-1
$LPRecords[$RecordCount] = $LPRecord
}
$LPRecordSet.Close();
return $LPRecords
}