View Single Post
Old 01-11-2007   #3 (permalink)
Keith Hill [MVP]


 
 

Re: Parse Log files into a strongly type Object

"Karlos" <kbeigan@gmail.com> wrote in message
news:1168531851.839808.105750@o58g2000hsb.googlegroups.com...
> 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


Try this:

$x = $(switch .... )

Apparently after the "=" of an assignment statement you can use a command
directly but if you use a language keyword like switch it has to appear in a
subexpression - $().

BTW you could simplify the above:

$obj = new-object psobject
add-member -inp $obj -MemberType NoteProperty Time $matches[2]
....

--
Keith


My System SpecsSystem Spec