{"id":68973,"date":"2005-09-13T20:52:00","date_gmt":"2005-09-13T20:52:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/09\/13\/how-can-i-retrieve-just-audit-failures-warnings-and-errors-from-my-event-logs\/"},"modified":"2005-09-13T20:52:00","modified_gmt":"2005-09-13T20:52:00","slug":"how-can-i-retrieve-just-audit-failures-warnings-and-errors-from-my-event-logs","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-retrieve-just-audit-failures-warnings-and-errors-from-my-event-logs\/","title":{"rendered":"How Can I Retrieve Just Audit Failures, Warnings, and Errors from My Event Logs?"},"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 retrieve just audit failures, warnings, and errors from my event logs?<BR><BR>&#8212; OG<\/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, OG. You know, just for the heck of it, we decided to check the event logs on one of our computers to see whether this was a task worth doing. In the Security event log on this machine we had 42,815 events; of those, just 286 were failure events. If we assume that the failure events are the events we really care about then we have two choices here: we can either wade through 42,815 events, trying to pick out the relatively few failure events, or we can write a script that returns <I>only<\/I> failure events. Even for the Scripting Guys that wasn\u2019t a very tough decision to make.<\/P>\n<P>Here\u2019s a script that returns just audit failures, warnings, and errors from all your event logs:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:&#8221; _\n    &amp; &#8220;{(Security)}\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colLoggedEvents = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_NTLogEvent Where EventType &lt;&gt; 4 AND EventType &lt;&gt; 8&#8221;)<\/p>\n<p>For Each objEvent in colLoggedEvents\n    Wscript.Echo &#8220;Category: &#8221; &amp; objEvent.Category\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.EventType\nNext\n<\/PRE>\n<P>Having shown you the script we should point out that this one runs only on Windows XP and Windows Server 2003. But don\u2019t panic: we\u2019ll show you a modified version in a minute or two that will work on Windows 2000.<\/P>\n<P>Hey, no need to thank us; that\u2019s what we\u2019re here for. <\/P>\n<P>To begin with, we need to bind to the WMI service on the computer in question (in this sample script, that\u2019s the local computer). You might notice that, when making this connection, we included the <B>(Security)<\/B> privilege in the moniker string:<\/P><PRE class=\"codeSample\">Set objWMIService = GetObject(&#8220;winmgmts:&#8221; _\n    &amp; &#8220;{(Security)}\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\n<\/PRE>\n<P>Is that important? Well, one of the things we want to retrieve is audit failures. Audit failures are recorded in the Security event log; if you don\u2019t include the (Security) privilege you will <I>not<\/I> be able to retrieve events from the Security event log. Yes, we know you\u2019re an administrator on the machine. Doesn\u2019t matter: the (Security) privilege is still required in order to retrieve items from the Security event log.<\/P>\n<P>In other words, yes, it\u2019s important.<\/P>\n<P>Next we issue the following WQL query, which limits the records retrieved to audit failures, warnings, and errors:<\/P><PRE class=\"codeSample\">Set colLoggedEvents = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_NTLogEvent Where EventType &lt;&gt; 4 AND EventType &lt;&gt; 8&#8221;)\n<\/PRE>\n<P>What do you mean it\u2019s not obvious how the query does this? It &#8211; oh, right: it\u2019s <I>not<\/I> very obvious, is it? Well, as it turns out, the <B>EventType<\/B> property indicates the type of record being written to the event log; furthermore, EventType will always be one of the following values:<\/P>\n<TABLE id=\"EWD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Value<\/B><\/P><\/TD>\n<TD>\n<P class=\"lastInCell\"><B>Meaning<\/B><\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">1<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Error<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">2<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Warning<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">4<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Information<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">8<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Security audit success<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">16<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Security audit failure<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>In our query, we asked for all the events where the EventType did not equal 4 (information events) <I>and<\/I> where the EventType does not equal 8 (security audit success). That filters out information and security audit success events; for example, an information event equals 4, which causes it to be excluded from the returned data. In turn, that leaves just three event types to be retrieved: audit failures, warnings, and errors. Coincidentally, those just happen to be the three event types you\u2019re interested in.<\/P>\n<P>The rest of the script is nothing more than a For Each loop that echoes back pertinent information for each event. As we are wont to say: problem solved.<\/P>\n<P>Well, unless you\u2019re running on Windows 2000, that is. That\u2019s because the EventType property isn\u2019t found on Windows 2000; instead, the Win32_NTLogEvent class uses the <B>Type<\/B> property. On Windows 2000, Type will be one of the following string values:<\/P>\n<TABLE id=\"EKF\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Value<\/B><\/P><\/TD>\n<TD>\n<P class=\"lastInCell\"><B>Meaning<\/B><\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">error<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Error<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">warning<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Warning<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">information<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Information<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">audit success<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Security audit success<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">audit failure<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Security audit failure<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>To get our script to work on Windows 2000, we need to filter on the Type property, eliminating records of type <B>information<\/B> and type <B>audit success<\/B>. Our revised WQL query looks like this:<\/P><PRE class=\"codeSample\">Set colLoggedEvents = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_NTLogEvent Where Type &lt;&gt; &#8216;information&#8217; AND Type &lt;&gt; &#8216;audit success'&#8221;)\n<\/PRE>\n<P>And the revised script looks something like this:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:&#8221; _\n    &amp; &#8220;{(Security)}\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colLoggedEvents = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_NTLogEvent Where Type &lt;&gt; &#8216;information&#8217; AND Type &lt;&gt; &#8216;audit success'&#8221;)<\/p>\n<p>For Each objEvent in colLoggedEvents\n    Wscript.Echo &#8220;Category: &#8221; &amp; objEvent.Category\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\nNext\n<\/PRE>\n<P>There you go.<\/P>\n<P>Incidentally, the fact that we had 42,815 events doesn\u2019t mean we don\u2019t back up and clear our event logs on a regular basis. Instead, we let these records accumulate so that we\u2019d \u2026 have a better example to use for today\u2019s column. Now that the column is finished, we\u2019ll probably back up and clear the event log, just like you\u2019re supposed to.<\/P>\n<P>No, not today, mind you. But soon, very soon \u2026.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I retrieve just audit failures, warnings, and errors from my event logs?&#8212; OG Hey, OG. You know, just for the heck of it, we decided to check the event logs on one of our computers to see whether this was a task worth doing. In the Security event log on [&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-68973","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 retrieve just audit failures, warnings, and errors from my event logs?&#8212; OG Hey, OG. You know, just for the heck of it, we decided to check the event logs on one of our computers to see whether this was a task worth doing. In the Security event log on [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68973","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=68973"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68973\/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=68973"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68973"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68973"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}