Hey PowerShell Guy !, how can I use get-content to analyze sql server logfile ?
I got a nice question the the comments, how can I use get-content to analyze sql server logfile ?
Ran has a SQL logfile like this :
2008-10-21 09:51:51.40 spid3 "ERROR:"
2008-10-22 09:51:52.04 spid3 "OK"
2008-10-22 01:51:52.04 spid5 "ERROR:"
2008-10-22 09:51:52.04 spid3 "OK"
2008-10-23 01:51:52.04 spid5 "ERROR:"
2008-10-23 09:51:52.04 spid5 "ERROR:"
2008-10-23 09:51:52.04 spid3 "OK"
2008-10-24 09:51:52.04 spid5 "ERROR:"
2008-10-24 09:51:52.04 spid5 "ERROR:"
2008-10-24 09:51:52.04 spid3 "OK"
2008-10-25 09:51:52.04 spid5 "ERROR:"
2008-10-25 09:51:52.04 spid5 "OK"
2008-10-25 09:51:52.04 spid5 "ERROR:"
and wanted want to be be able to search for errors ( "ERROR:" )only when the date is yesterday or today
the Quick and Dirty answer (as it is late early ) I made interactive in the PowerShell console :
(Get-Content C:\powershell\sample_ERRORLOG.txt) |? {[void]($_ -match '((.*)\d\d spid)');(get-date -date $matches[2]) -gt (get-date).AddDays(-2)} |? {$_ -match 'ERROR'}
the Result :
You can see it takes a literal 2 day's back filter up till second level, hence you can see 1 error record of 2009-10-13
As I do filtering on converted datetime objects al in this single one-liner it is a bit cryptic and the target calls for a bit more clear and robust solution for repeated usage.
So in a next post, I will follow up on this one and take this one-liner into it's parts to do some more explaining and build a small script to do this task.
Enjoy,
Greetings /\/\o\/\/