0

I would like to use PowerShell to parse a log, and output to a CSV file.

What is the basic way to accomplish this?

2
  • The do something in 4c74356b41's answer depends on the format of your log file. Eventually you can use import-csv your.log optionally with -header ColA ... Commented Nov 16, 2016 at 21:11
  • Yeah, I was concerned that formatting for each log file would have affect, thanks. Commented Nov 16, 2016 at 22:21

2 Answers 2

1

Each log format involves it own logic when doing pattern matching, but I would propose to you something like:

(test.log is something like: date - context - type - msg)

$file = get-content test.log
$pattern = '^(.*) - (.*) - (.*) - (.*)$'
$findings = $file | select-string -pattern $pattern
$lines = foreach($f in $findings) {
    if ($f -match $pattern) { 
      $line = @{
           'date' = $matches[1]
           'context' = $matches[2]
           'level' = $matches[3]
           'message' = $matches[4]
      }
      new-object -typename psobject -property $line
    }
}
$lines | export-csv -notypeinformation -delimiter ';' -path 'test.csv'
Sign up to request clarification or add additional context in comments.

Comments

0

Get-Content | do something | Export-CSV

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.