{"id":75,"date":"2007-02-16T12:00:00","date_gmt":"2007-02-16T20:00:00","guid":{"rendered":"http:\/\/testblog.sapien.com\/index.php\/2007\/02\/16\/more-fast-furious-remote-event-logs\/"},"modified":"2007-02-16T12:00:00","modified_gmt":"2007-02-16T20:00:00","slug":"more-fast-furious-remote-event-logs","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2007\/02\/16\/more-fast-furious-remote-event-logs\/","title":{"rendered":"More Fast &#038; Furious: Remote Event Logs"},"content":{"rendered":"<p>The <a href=\"\/current\/2007\/2\/15\/powershell-quick-event-log.html\">other day<\/a>, I demonstrated a fast &amp;furious way to check eventlogs on the local computer. Unfortunately the Get-Eventlog cmdlet doesn&#8217;t have a remote computer option. To get event logs from remote computers, you need to use Get-WMIObject. Unfortunately,this requires a bit more work.<\/p>\n<p>Here&#8217;s the code you would need to run:<\/p>\n<p><font size=\"2\" face=\"Lucida Console\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">$computer=&#8221;MyServer&#8221;<br \/>foreach ($log in (Get-WmiObject win32_nteventlogfile -computer $computer | Select-Object logfilename)) {<br \/>Write-Host -fore Green -back Black $log.logfilename ;<br \/>$query=&#8221;Select Eventcode,Eventtype,Logfile,Message,TimeGenerated from win32_NTLogEvent where logfile=&#8217;&#8221;+$log.logfilename+&#8221;&#8217;&#8221;;<br \/>Get-WmiObject -query $query -computer $computer | Select-Object EventCode,EventType,LogFile,Message,TimeGenerated -first 10 | More<br \/>} <\/font><\/p>\n<p>Hardly a friendly one-liner. Parsing out the eventlog file names is a little more cumbersome:<\/p>\n<p><font size=\"2\" face=\"Lucida Console\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">Get-WmiObject win32_nteventlogfile -computer $computer | Select-Object logfilename<\/font><\/p>\n<p>But once I have them, I can build a query:<\/p>\n<p><font size=\"2\" face=\"Lucida Console\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">$query=&#8221;Select Eventcode,Eventtype,Logfile,Message,TimeGenerated from win32_NTLogEvent where logfile=&#8217;&#8221;+$log.logfilename+&#8221;&#8217;&#8221;;<\/font><br \/>And then run Get-WMIObject, again selecting just the information I want and piping it to More:<\/p>\n<p><font size=\"2\" face=\"Lucida Console\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">Get-WmiObject -query $query -computer $computer | Select-Object EventCode,EventType,LogFile,Message,TimeGenerated -first 10 | More<\/font><\/p>\n<p>I tried using the query directly, but the -query parameter didn&#8217;t like $log.logfilename. Since having a separate query worked, I moved on.<\/p>\n<p>If you run this, it will look fine at first glance. But then you&#8217;ll realize you didn&#8217;&nbsp;t see anything from Security eventlog. To access the Security Eventlog, you need to specify the Security Privilege. Unfortunately, the Get-WMIObject cmdlet has no such provision. The way around this is to use a WMISearcher object and enable privileges. You&#8217;ll also need to specify the remote computer in the scope.path:<\/p>\n<p><font size=\"2\" face=\"Lucida Console\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">$searcher=new-object WMISearcher<\/font><\/p>\n<p><font size=\"2\" face=\"Lucida Console\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">$searcher.Scope.Options.EnablePrivileges=1<\/font><\/p>\n<p><font size=\"2\" face=\"Lucida Console\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">$searcher.scope.path=&#92;$computer\\root\\cimv2<\/font><\/p>\n<p>Here&#8217;s what the modified code would look like:<\/p>\n<p><font size=\"2\" face=\"Lucida Console\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">$computer=&#8221;dc01&#8221;<br \/>$searcher=new-object WMISearcher<br \/>$searcher.Scope.Options.EnablePrivileges=1<br \/>$searcher.scope.path=&#8221;&#92;$computer\\root\\cimv2&#8221;<br \/>foreach ($log in (Get-WmiObject win32_nteventlogfile -computer $computer&nbsp;| Select-Object logfilename)) {<br \/>Write-Host -fore Green -back Black $log.logfilename ;<br \/>$query=&#8221;Select Eventcode,Eventtype,Logfile,Message,TimeGenerated from win32_NTLogEvent where logfile=&#8217;&#8221;+$log.logfilename+&#8221;&#8217;&#8221;<br \/>$searcher.query=$query<br \/>$searcher.get() | Select-Object EventCode,EventType,LogFile,Message,TimeGenerated -first 5 | More<br \/>} <\/font><\/p>\n<p>This should work, assuming you are running this with domain admin credentials. If you need to specify alternate credentials, then you not only have to pass them to the Get-WMIObject, but you also need to define them for the searcher object. But here&#8217;s the problem: the Get-WMIObject wants a secure string password, but the searcher object can only take plain text. I tried passing the results of ConvertFrom-SecureString as the searcher password, but no-go.<\/p>\n<p>Eventually this is what I came up with:<\/p>\n<p><font size=\"2\" face=\"Lucida Console\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">$computer=&#8221;dc01&#8221;<br \/>$username=&#8221;mydomain\\administrator&#8221;<br \/>#password has to be entered as clear text<br \/>$pw=Read-Host &#8220;Enter the password you&#8217;ll need&#8221; <br \/>#but credential requires secure string<br \/>$securepw=ConvertTo-SecureString $pw -AsPlainText -Force<br \/>$cred=new-object -typename System.Management.Automation.PSCredential -argumentlist $username,$securepw<br \/>$searcher=new-object WMISearcher<br \/>$searcher.Scope.Options.EnablePrivileges=1<br \/>$searcher.scope.path=&#8221;&#92;$computer\\root\\cimv2&#8221;<br \/>$searcher.scope.options.username=$cred.Username<br \/>$searcher.scope.options.password=$pw <\/font><\/p>\n<p><font size=\"2\" face=\"Lucida Console\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">foreach ($log in (Get-WmiObject win32_nteventlogfile -computer $computer -credential $cred| Select-Object logfilename)) {<br \/>Write-Host -fore Green -back Black $log.logfilename ;<br \/>$query=&#8221;Select Eventcode,Eventtype,Logfile,Message,TimeGenerated from win32_NTLogEvent where logfile=&#8217;&#8221;+$log.logfilename+&#8221;&#8217;&#8221;<br \/>$searcher.query=$query<br \/>$searcher.get() | Select-Object EventCode,EventType,LogFile,Message,TimeGenerated -first 5 | More<br \/>}<\/font>  <\/p>\n<p>As you can see, hardly a fast &amp; furious solution and far from elegant.&nbsp; The password you enter will be displayed as plain text. But this gets the job done. Although, if your logs are too big you might get a quota violation.&nbsp; I&#8217;ll have to deal with that later. <\/p>\n<p>This task would be a whole lot easier if Get-Eventlog could query remote computers, or Get-WMIObject could set privileges. Actually, it would be great if both cmdlets could be enhanced to povide these features.<\/p>\n<\/p>\n<div style=\"margin: 0px; padding: 0px; display: inline; float: none;\" id=\"0767317B-992E-4b12-91E0-4F059A8CECA8:d60200af-8279-4393-b8da-930cc863cbe5\" class=\"wlWriterSmartContent\">Technorati tags: <a rel=\"tag\" href=\"http:\/\/technorati.com\/tags\/PowerShell\">PowerShell<\/a>, <a rel=\"tag\" href=\"http:\/\/technorati.com\/tags\/Scripting\">Scripting<\/a>, <a rel=\"tag\" href=\"http:\/\/technorati.com\/tags\/Cmdlet\">Cmdlet<\/a>, <a rel=\"tag\" href=\"http:\/\/technorati.com\/tags\/Functions\">Functions<\/a>, <a rel=\"tag\" href=\"http:\/\/technorati.com\/tags\/EventLogs\">EventLogs<\/a>, <a rel=\"tag\" href=\"http:\/\/technorati.com\/tags\/WMI\">WMI<\/a><\/div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<div style=\"margin: 0px; padding: 0px; display: inline;\" id=\"0767317B-992E-4b12-91E0-4F059A8CECA8:62705512-2d4d-4c25-bf2a-a55240626373\" class=\"wlWriterSmartContent\">del.icio.us tags: <a rel=\"tag\" href=\"http:\/\/del.icio.us\/popular\/PowerShell\">PowerShell<\/a>, <a rel=\"tag\" href=\"http:\/\/del.icio.us\/popular\/Scripting\">Scripting<\/a>, <a rel=\"tag\" href=\"http:\/\/del.icio.us\/popular\/Cmdlet\">Cmdlet<\/a>, <a rel=\"tag\" href=\"http:\/\/del.icio.us\/popular\/Functions\">Functions<\/a>, <a rel=\"tag\" href=\"http:\/\/del.icio.us\/popular\/EventLogs\">EventLogs<\/a>, <a rel=\"tag\" href=\"http:\/\/del.icio.us\/popular\/WMI\">WMI<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The other day, I demonstrated a fast &#038;furious way to check eventlogs on the local computer. Unfortunately the Get-Eventlog cmdlet doesn&#8217;t have a remote computer option. To get event logs from remote computers, you need to use Get-WMIObject. Unfortunately,this requires a bit more work.<\/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":[25],"tags":[],"class_list":["post-75","post","type-post","status-publish","format-standard","hentry","category-windows-powershell"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/75","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=75"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/75\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=75"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=75"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=75"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}