{"id":344,"date":"2006-12-05T04:00:00","date_gmt":"2006-12-05T12:00:00","guid":{"rendered":"http:\/\/testblog.sapien.com\/index.php\/2006\/12\/05\/remote-event-log-filtering\/"},"modified":"2006-12-05T04:00:00","modified_gmt":"2006-12-05T12:00:00","slug":"remote-event-log-filtering","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2006\/12\/05\/remote-event-log-filtering\/","title":{"rendered":"Remote Event Log Filtering"},"content":{"rendered":"<p>Recently I showed how to use Get-EventLog to filter for recent eventlogs. I like this cmdlet because the output is easy to read. One drawback is that you can&#8217;t use the cmdlet to query a remote system. Right now, Get-Wmiobject is one of the few cmdlets that allows you to connect to a remote system. Which is great because we can use WMI to query for Win32_NTLogEvents. One primary challenge here is converting date time into a WMI recognized format.&nbsp; WMI uses timestamps in something called DMTF format that looks like 20061128160658.329710-300.&nbsp; PowerShell&#8217;s Get-Date cmdlet has a number of tricks it can do for manipulating and converting a datetime object. But it doesn&#8217;t have an option for converting to DMTF.&nbsp; However (you knew I wouldn&#8217;t bring this up if there wasn&#8217;t a solution) there is a .NET method called ToDMTFDateTime that is part of the System.Management.ManagementDateTimeConverter class.&nbsp; This method takes a PowerShell date-time object and converts it to DMTF.&nbsp; <\/p>\n<p>Let&#8217;s say I want to calculate a date that is 7 days ago.&nbsp; Try this in PowerShell:<br \/><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=[System.Management.ManagementDateTimeConverter]::ToDMTFDateTime($d.AddDays(-7)) <\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">$d<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">$recent<\/span><\/p>\n<p>The parameter of <span style=\"font-family: Courier New,Courier,mono;\">ToDMTFDateTime<\/span> is the current date minus 7 days.<\/p>\n<p>I won&#8217;t go into detail about using Get-WmiObject to connect to remote computers with alternate credentials. But one thing I did find handy was limiting the properties for querying and displaying. Otherwise, the cmdlet spits out all sorts of system properties that I usually don&#8217;t care about. I found it easiest to create a query string object like this:<\/p>\n<p><span style=\"font-family: Courier New,Courier,mono;\">$query=&#8221;SelectLogFile,TimeGenerated,Type,EventCode,Message from Win32_NTLogeventwhere (type=&#8217;Error&#8217; OR type=&#8217;Warning&#8217;) AND TimeGenerated &gt;=&#8217;$recent'&#8221;<\/span><\/p>\n<p>This is a long single line expression.&nbsp; I strongly recommend testing the query string in Wbemtest to make sure you get the results you are expecting. You&#8217;ll need to get a DMTF value for $recent but that&#8217;s easy enough with a Write-Host command.<\/p>\n<p>The last bit of business is organizing the output for presentation. By using Select-Object and specifying the properties, I can control how data is presented.<\/p>\n<p>Here&#8217;s the complete 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=[System.Management.ManagementDateTimeConverter]::ToDMTFDateTime($d.AddDays(-7)) <\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">$computer=&#8221;dc01&#8243;<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">$cred=Get-Credential(&#8220;mydomain\\administrator&#8221;)<\/span><br style=\"font-family: Courier New,Courier,mono;\"><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">$query=&#8221;Select LogFile,TimeGenerated,Type,EventCode,Message from Win32_NTLogevent where (type=&#8217;Error&#8217; OR type=&#8217;Warning&#8217;) AND TimeGenerated &gt;=&#8217;$recent'&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Write-Host $query<\/span><br style=\"font-family: Courier New,Courier,mono;\"><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Get-WmiObject&nbsp; -computer $computer -credential $cred -query $query |select -property LogFile,type,TimeGenerated,EventCode,Message<\/span><\/p>\n<p>Again, each expression is a single line. By separating out the query, I could turn this into a function, passing it a query string as the parameter. Actually, I&#8217;d also pass it computername and a credential object as well.<\/p>\n<p>Using WMI is not as neat as using Get-EventLog, but if you need access to remote eventlogs, right now it&#8217;s the only show in town.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently I showed how to use Get-EventLog to filter for recent eventlogs. I like this cmdlet because the output is easy to read. One drawback is that you can&#8217;t use the cmdlet to query a remote system. Right now, Get-Wmiobject is one of the few cmdlets that allows you to connect to a remote system. Which is great because we can use WMI to query for Win32_NTLogEvents. One primary challenge here is converting date time into a WMI recognized format.<\/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-344","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\/344","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=344"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/344\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=344"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}