{"id":342,"date":"2006-12-04T04:00:00","date_gmt":"2006-12-04T12:00:00","guid":{"rendered":"http:\/\/testblog.sapien.com\/index.php\/2006\/12\/04\/powershell-event-log-filtering\/"},"modified":"2006-12-04T04:00:00","modified_gmt":"2006-12-04T12:00:00","slug":"powershell-event-log-filtering","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2006\/12\/04\/powershell-event-log-filtering\/","title":{"rendered":"PowerShell Event Log Filtering"},"content":{"rendered":"<p>I recently helped a fellow scripting admin with a PowerShell problem in the ScriptingAnswers.com <a href=\"http:\/\/www.scriptinganswers.com\/forum\/forum_topics.asp?FID=13\">PowerShell <\/a>forum. He wanted to get Errors and Warnings that had happened in the last 30 minutes.&nbsp; Using the Get-EventLog cmdlet would seem like the write solution but it takes a little wrangling to get the information you want.&nbsp; One of the first challenges is that there does not appear to be a way to specify more than one logfile. The -logname parameter does not take a wildcard or multiple values. However, you can get a list of logfile names with -list.&nbsp; That might be promising.&nbsp; I can create an object that contains a list of all the logfiles on my system like this:<\/p>\n<p><span style=\"font-family: Courier New,Courier,mono;\">$logfiles=Get-EventLog -list -asString<\/span><\/p>\n<p>I use -asString because I want just the name of the logfile, not the logfile object.&nbsp; Now, what about filtering by the TimeWritten property? That is actually pretty easy. A datetime object has several methods such as AddDays,AddHours and AddMinutes. All I need to do is add a negative number of minutes from the current time like this:<\/p>\n<p><span style=\"font-family: Courier New,Courier,mono;\">$d=Get-Date<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">$lastweek= $d.AddDays(-7) <\/span><\/p>\n<p>This will create an object ($lastweek) that is a datetime value of 7 days ago from now.&nbsp; Next I need to filter out just Warnings or Errors. Each eventlog entry object has a property of EntryType.&nbsp; All I need to do is find entries where EntryType -eq &#8220;Error&#8221; or EntryType -eq &#8220;Warning.&#8221;<\/p>\n<p>Maybe you can see where I&#8217;m heading with this.&nbsp; Here&#8217;s script:<\/p>\n<p><span style=\"font-family: Courier New,Courier,mono;\">$d=Get-Date<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">$recent= $d.AddDays(-1)&nbsp;&nbsp; <\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">$logfiles=Get-EventLog -list -asString<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">foreach ($log in $logfiles) {<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Write-Host -foregroundcolor Red -backgroundcolor Yellow `<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">$log.ToUpper() &#8220;Event Log&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Get-EventLog -logname $log | where `<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">{($_.EntryType -eq &#8220;Warning&#8221; -OR $_.EntryType -eq &#8220;Error&#8221;) `<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">-AND ($_.TimeWritten -ge $recent)}<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">}<\/span><\/p>\n<p>This script will display all errors and warnings from all event logs on my system that occurred within the last day. The heavy lifting is done by this Where expression:<\/p>\n<p><span style=\"font-family: Courier New,Courier,mono;\">where <\/span><span style=\"font-family: Courier New,Courier,mono;\">{($_.EntryType -eq &#8220;Warning&#8221; -OR $_.EntryType -eq &#8220;Error&#8221;) <\/span><span style=\"font-family: Courier New,Courier,mono;\">-AND ($_.TimeWritten -ge $recent)<\/span><\/p>\n<p>The expression has some compound queries. First I need events where the type is either Warning or Error and then the TimeWritten must be greater or equal to a datetime value of 1 day ago.<\/p>\n<p>You can easily modify the script for a different time range, specific log files or specific log types. If you&#8217;re wondering how I knew what the eventlog properties were, you can see for yourself with a command like this:<\/p>\n<p><span style=\"font-family: Courier New,Courier,mono;\">&nbsp;get-eventlog -logname application -newest 1 |get-member<\/span><br style=\"font-family: Courier New,Courier,mono;\"><br \/>You can modify the query accordingly if you want to display additional information.<\/p>\n<p>The downside to Get-Eventlog is that it only works on the local system. I&#8217;ll show you how to take another approach if you want to query a remote server another day.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently helped a fellow scripting admin with a PowerShell problem in the ScriptingAnswers.com PowerShell forum. He wanted to get Errors and Warnings that had happened in the last 30 minutes.  Using the Get-EventLog cmdlet would seem like the write solution but it takes a little wrangling to get the information you want.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2,25],"tags":[],"class_list":["post-342","post","type-post","status-publish","format-standard","hentry","category-general","category-windows-powershell"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/342","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=342"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/342\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}