powershellout-gridview

Use Out-gridview to display Log


I'm pretty new to powershell scripts. I'd like to display a log file in a gridview using out-gridview.

Let's say I have a very simple script like

$logFile = "logs\myCoolLogFile.log";
gc -wait $logFile | Out-GridView -Wait

This displays each row in a single cell. My log is formated like this:

LEVEL TIMESTAMP LOGGERNAME [THREAD]: MESSAGE

Each entry is separated by one or more spaces (but I can change it easily to be separated by one tab character if it was more suitable). The message may contain additional spaces. The previous entries do never contain spaces.

I'd like to have different columns for LEVEL, TIMESTAMP etc. But I'm not sure how to achieve this. Any help is appreciated.

Edit: As @jisaak requested, here is an example of my log with a multi line message:

WARN    09:53:49.938    n.s.e.CacheManager  [StartStop-Thread]  Creating a new instance of CacheManager using the diskStorePath "C:\temp" which is already used by an existing CacheManager.
The source of the configuration was net.sf.ehcache.config.generator.ConfigurationSource$DefaultConfigurationSource@48666bf4.
The diskStore path for this CacheManager will be set to C:\temp\ehcache_auto_created_1461052429938.

Edit2: Now, that I think about it, I guess it would be acceptable if the lines which do not contain enough columns would be skipped altogether. But I do need help for that behavior as well.


Solution

  • You can use the ConvertFrom-Csv cmdlet to convert your log:

    $logFile = "logs\myCoolLogFile.log";
    gc $logFile |  ConvertFrom-Csv -Delimiter ' ' | Out-GridView
    

    Why are you using the -wait switch on the Get-Content cmdlet?