View Single Post
Old 01-12-2007   #4 (permalink)
Lee Holmes [MSFT]


 
 

Re: Parse Log files into a strongly type Object

You might find this helpful:
http://www.leeholmes.com/blog/parset...Vengeance.aspx


[C:\temp]
PS:10 > $parseExpression =
"^\s*(\d\d-\w\w\w-\d\d)\s*(\d\d:\d\d:\d\d\.\d\d\d)\s*(.*[0-9]\))\s*(.*)"

[C:\temp]
PS:11 > $propertyName = "Date","Time","Code","Description"

[C:\temp]
PS:12 > gc log.txt | Parse-TextObject -ParseExpression
$parseExpression -PropertyName $propertyName

Date Time Code
Description
---- ---- ----
-----------
05-Nov-06 12:00:06.287 ( -1 4360 4356)
dual_flsrv.exe:dual_flsrv....
05-Nov-06 12:00:06.287 ( -1 4360 8220)
dual_flsrv.exe:rcv_u_fl.c,...

--
Lee Holmes [MSFT]
Windows PowerShell Development
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.

"Keith Hill [MVP]" <r_keith_hill@no.spam.thank.u.hotmail.com> wrote in
message news:uY1pCKaNHHA.4000@TK2MSFTNGP06.phx.gbl...
> "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