View Single Post
Old 01-11-2007   #1 (permalink)
Karlos


 
 

Parse Log files into a strongly type Object

Fellow humans,

I'm trying to process the following data from a log file into a
strongly typed object for further analysis:-

05-Nov-06 12:00:06.287 ( -1 4360 4356)
dual_flsrv.exe:dual_flsrv.c,v:267: connection accepted from client
SVRPCSA address 158.89.5.16
05-Nov-06 12:00:06.287 ( -1 4360 8220)
dual_flsrv.exe:rcv_u_fl.c,v:165: serving client SVRPCSA address
158.89.5.16

This is the script that I'm using to parse the data into an object:-

switch -file c:\svrlog.txt -regex {

"^\s*(\d\d-\w\w\w-\d\d)\s*(\d\d:\d\d:\d\d\.\d\d\d)\s*(.*[0-9]\))\s*(.*)"
{
$obj = New-Object -TypeName System.Management.Automation.PSObject
$prop = New-Object System.Management.Automation.PSNoteProperty 'Date',
$matches[1]
$obj.PSObject.Properties.Add($prop)
$prop = New-Object System.Management.Automation.PSNoteProperty 'Time',
$matches[2]
$obj.PSObject.Properties.Add($prop)
$prop = New-Object System.Management.Automation.PSNoteProperty 'Code',
$matches[3]
$obj.PSObject.Properties.Add($prop)
$prop = New-Object System.Management.Automation.PSNoteProperty 'Desc',
$matches[4]
$obj.PSObject.Properties.Add($prop)
$obj
}

}


The question is, how can I collect the output of the above script into
a collection of an object for further analysis. e.g say sort on code
and list.

say like $x = switch -file c:\svrlog.txt -regex { .........} (which
doesn't work.)

Any idea's

Many thanks in advance

My System SpecsSystem Spec