{"id":71143,"date":"2004-10-26T15:28:00","date_gmt":"2004-10-26T15:28:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2004\/10\/26\/hey-scripting-guy-can-i-retrieve-just-failure-events-from-the-security-event-log\/"},"modified":"2004-10-26T15:28:00","modified_gmt":"2004-10-26T15:28:00","slug":"hey-scripting-guy-can-i-retrieve-just-failure-events-from-the-security-event-log","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-can-i-retrieve-just-failure-events-from-the-security-event-log\/","title":{"rendered":"Hey, Scripting Guy! Can I Retrieve Just Failure Events from the Security Event Log?"},"content":{"rendered":"<p><H2><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\"> <\/H2>\n<P>Hey, Scripting Guy! Is there a way to retrieve just Failure Audit events from the Security event log?<BR><BR>&#8212; KA<\/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, KA. Interesting, isn\u2019t it: any time the subject is failure, people turn to the Scripting Guys. What makes you think we know anything about failure?<\/P>\n<P>Ok, you\u2019re right: silly question. As far as <I>your<\/I> question goes, it\u2019s very easy to retrieve just Security Failure Audit events from the Security event log; in fact, we just happened to have a script lying around that does that very thing:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:&#8221; _\n    &amp; &#8220;{impersonationLevel=impersonate,(Security)}!\\\\&#8221; &amp; _\n        strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colLoggedEvents = objWMIService.ExecQuery _\n    (&#8220;Select * FROM Win32_NTLogEvent WHERE Logfile = &#8216;Security&#8217; &#8221; &amp; _\n        &#8220;AND EventType = 5&#8221;)\nFor Each objEvent in colLoggedEvents\n    Wscript.Echo &#8220;===================================================&#8221;\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\n    Wscript.Echo\nNext\n<\/PRE>\n<P>A pretty simple little script, but there are at least two things you should take note of. First, notice that we included the <B>(Security)<\/B> parameter when connecting to WMI:<\/P><PRE class=\"codeSample\">Set objWMIService = GetObject(&#8220;winmgmts:&#8221; _\n    &amp; &#8220;{impersonationLevel=impersonate,(Security)}!\\\\&#8221; &amp; _\n        strComputer &amp; &#8220;\\root\\cimv2&#8221;)\n<\/PRE>\n<P>You must include this parameter any time you\u2019re working with the Security event log; leave it out, and the script won\u2019t work. And, yes, we <I>know<\/I> you\u2019re a local administrator and we <I>know<\/I> you have the right to read the Security event log. For better or worse, though, WMI doesn\u2019t care about that: you still have to include the <B>(Security)<\/B> parameter. <\/P>\n<P>Second, note the two parts of our WHERE clause:<\/P><PRE class=\"codeSample\">(&#8220;Select * from Win32_NTLogEvent WHERE Logfile = &#8216;Security&#8217; &#8221; &amp; _\n        &#8220;AND EventType = 5&#8221;)\n<\/PRE>\n<P>For this script, we only want to retrieve events that meet two criteria: they\u2019re recorded in the Security event log, and they have an EventType of 5. As you probably figured out, in WMI-speak an EventType of 5 means a Failure Audit. Alternatively, you could search for EventTypes of 1 (Error), 2 (Warning), 3 (Information), or 4 (Security Audit Success). Because we want Failure Audit events, we look for events in the Security <B>Logfile<\/B> with an <B>EventType<\/B> of 5. Thus:<\/P><PRE class=\"codeSample\">WHERE Logfile = &#8216;Security&#8217; AND EventType = 5\n<\/PRE>\n<P>Cool, huh? If you\u2019d like more information about working with event logs (including some sample queries you might find useful), check out the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/guide\/sas_log_overview.mspx\"><B>Logs chapter<\/B><\/A> in the Microsoft Windows 2000 Scripting Guide.<\/P>\n<P>And as long as we have your attention, we might want to add one more thing. The script, as it currently stands, will display the TimeWritten property (that is, the date and time that the event was recorded in the event log) using WMI\u2019s default Universal Time Coordinate format. In other words, you\u2019ll get back results similar to this:<\/P><PRE class=\"codeSample\">20041025124000.000000-420\n<\/PRE>\n<P>How \u2026 nice \u2026. But don\u2019t despair. Here\u2019s a modified version of the script that includes a function (WMIDateStringTodate) that will convert this UTC value to something a bit easier to read:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:&#8221; _\n    &amp; &#8220;{impersonationLevel=impersonate,(Security)}!\\\\&#8221; &amp; _\n        strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colLoggedEvents = objWMIService.ExecQuery _\n    (&#8220;Select * FROM Win32_NTLogEvent WHERE Logfile = &#8216;Security&#8217; &#8221; &amp; _\n        &#8220;AND EventType = 5&#8221;)\nFor Each objEvent in colLoggedEvents\n    Wscript.Echo &#8220;===================================================&#8221;\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    dtmEventDate = objEvent.TimeWritten\n    strTimeWritten = WMIDateStringToDate(dtmEventDate)\n    Wscript.Echo &#8220;Time Written: &#8221; &amp; strTimeWritten\n    Wscript.Echo &#8220;Event Type: &#8221; &amp; objEvent.Type\n    Wscript.Echo &#8220;User: &#8221; &amp; objEvent.User\n    Wscript.Echo\nNext<\/p>\n<p>Function WMIDateStringToDate(dtmEventDate)\n    WMIDateStringToDate = CDate(Mid(dtmEventDate, 5, 2) &amp; &#8220;\/&#8221; &amp; _\n        Mid(dtmEventDate, 7, 2) &amp; &#8220;\/&#8221; &amp; Left(dtmEventDate, 4) _\n            &amp; &#8221; &#8221; &amp; Mid (dtmEventDate, 9, 2) &amp; &#8220;:&#8221; &amp; _\n                Mid(dtmEventDate, 11, 2) &amp; &#8220;:&#8221; &amp; Mid(dtmEventDate, _\n                    13, 2))\nEnd Function\n<\/PRE>\n<P>We won\u2019t bother explaining how this works today, but if you have any questions about it, let us know. Maybe we\u2019ll go into it in more detail in a future column.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Is there a way to retrieve just Failure Audit events from the Security event log?&#8212; KA Hey, KA. Interesting, isn\u2019t it: any time the subject is failure, people turn to the Scripting Guys. What makes you think we know anything about failure? Ok, you\u2019re right: silly question. As far as your question [&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-71143","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! Is there a way to retrieve just Failure Audit events from the Security event log?&#8212; KA Hey, KA. Interesting, isn\u2019t it: any time the subject is failure, people turn to the Scripting Guys. What makes you think we know anything about failure? Ok, you\u2019re right: silly question. As far as your question [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71143","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=71143"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71143\/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=71143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=71143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=71143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}