{"id":70733,"date":"2005-01-04T13:55:00","date_gmt":"2005-01-04T13:55:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/01\/04\/how-can-i-return-only-the-last-record-written-to-an-event-log\/"},"modified":"2005-01-04T13:55:00","modified_gmt":"2005-01-04T13:55:00","slug":"how-can-i-return-only-the-last-record-written-to-an-event-log","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-return-only-the-last-record-written-to-an-event-log\/","title":{"rendered":"How Can I Return Only the Last Record Written to an Event Log?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I read only the last record written to an event log? In other words, what is the WMI equivalent to the SQL statement <B>Select Top 1<\/B>?<BR><BR>&#8212; KM<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, KM. Well, as it turns out WMI doesn\u2019t <I>have<\/I> an equivalent to the Select Top command; for better or worse, the WMI Query Language (WQL) has only a small subset of the commands found in SQL. That doesn\u2019t mean we can\u2019t return only the last record written to an event log, it just means we have to be a bit sneaky about it.<\/P>\n<P>To get just the last record we need to do two things. To begin with, we need to figure out how many records are in an event log. That\u2019s important information, because events are numbered sequentially when they are added to an event log; in other words, the first event written to an event log is record 1, the second is record 2, etc. If there are 4,912 events in an event log, then the very last record written to the log has to be record number 4912. After we know the total number of records, we can then write a query that returns only events with a record number equal to the total number of records. That record is the last record written to the log.<\/P>\n<P>Here\u2019s a simple little script that determines the total number of records in the Application event log, and stores that value in the variable intRecords:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:&#8221; _\n    &amp; &#8220;{impersonationLevel=impersonate}!\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set objInstalledLogFiles = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_NTEventLogFile Where LogFileName = &#8216;Application'&#8221;)<\/p>\n<p>For Each objLogfile in objInstalledLogFiles\n    intRecords = objLogFile.NumberOfRecords\nNext\n<\/PRE>\n<P>Now we need a query that returns all events from the Application event log where the RecordNumber is equal to value of intRecords (and there will only be one such record, seeing as how record numbers are unique). Here\u2019s a query that does just that:<\/P><PRE class=\"codeSample\">Set colLoggedEvents = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_NTLogEvent Where Logfile = &#8216;Application&#8217; AND &#8221; &amp; _\n        &#8220;RecordNumber = &#8221; &amp; intRecords)\n<\/PRE>\n<P>All that\u2019s left now is to put these two script snippets together, and then add a For Each loop in which we echo the properties of this record:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:&#8221; _\n    &amp; &#8220;{impersonationLevel=impersonate}!\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set objInstalledLogFiles = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_NTEventLogFile Where LogFileName = &#8216;Application'&#8221;)<\/p>\n<p>For Each objLogfile in objInstalledLogFiles\n    intRecords = objLogFile.NumberOfRecords\nNext<\/p>\n<p>Set colLoggedEvents = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_NTLogEvent Where Logfile = &#8216;Application&#8217; AND &#8221; &amp; _\n        &#8220;RecordNumber = &#8221; &amp; intRecords)<\/p>\n<p>For Each objEvent in colLoggedEvents\n    Wscript.Echo &#8220;Category: &#8221; &amp; objEvent.Category\n    Wscript.Echo &#8220;Computer Name: &#8221; &amp; objEvent.ComputerName\n    Wscript.Echo &#8220;Event Code: &#8221; &amp; objEvent.EventCode\n    Wscript.Echo &#8220;Message: &#8221; &amp; objEvent.Message\n    Wscript.Echo &#8220;Record Number: &#8221; &amp; objEvent.RecordNumber\n    Wscript.Echo &#8220;Source Name: &#8221; &amp; objEvent.SourceName\n    Wscript.Echo &#8220;Time Written: &#8221; &amp; objEvent.TimeWritten\n    Wscript.Echo &#8220;Event Type: &#8221; &amp; objEvent.Type\n    Wscript.Echo &#8220;User: &#8221; &amp; objEvent.User\nNext\n<\/PRE>\n<P>That should do the trick. <\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I read only the last record written to an event log? In other words, what is the WMI equivalent to the SQL statement Select Top 1?&#8212; KM Hey, KM. Well, as it turns out WMI doesn\u2019t have an equivalent to the Select Top command; for better or worse, the WMI [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[97,98,3,5],"class_list":["post-70733","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-event-logs","tag-logs-and-monitoring","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I read only the last record written to an event log? In other words, what is the WMI equivalent to the SQL statement Select Top 1?&#8212; KM Hey, KM. Well, as it turns out WMI doesn\u2019t have an equivalent to the Select Top command; for better or worse, the WMI [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70733","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=70733"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70733\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=70733"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70733"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}