{"id":70573,"date":"2005-01-26T12:00:00","date_gmt":"2005-01-26T12:00:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/01\/26\/how-can-i-retrieve-information-from-my-event-logs-regarding-unsuccessful-logons\/"},"modified":"2005-01-26T12:00:00","modified_gmt":"2005-01-26T12:00:00","slug":"how-can-i-retrieve-information-from-my-event-logs-regarding-unsuccessful-logons","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-retrieve-information-from-my-event-logs-regarding-unsuccessful-logons\/","title":{"rendered":"How Can I Retrieve Information From My Event Logs Regarding Unsuccessful Logons?"},"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 scan the event logs of my servers and return only information about unsuccessful logons?<BR><BR>&#8212; LC<\/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, LC. We\u2019ll assume that you have enabled security auditing on your servers. If you haven\u2019t, that\u2019s step one. What you\u2019ll want to do is &#8211; at a minimum &#8211; audit for logon event failures. That way every time someone tries &#8211; and fails &#8211; to log on to the computer, an event with a specific event code will be written to the Security event log. For member servers and workstations, the event code is 529; for domain controllers, the event code is 675. For our example, we\u2019ll use event code 529, simply because you referenced \u201cservers\u201d in your question rather than \u201cdomain controllers.\u201d<\/P>\n<P><B>Note<\/B>. There might be other event codes of interest to you as well; needless to say, the Scripting Guys are not security experts. For the sake of simplicity, though, we\u2019ll focus on 529 and 675, both of which represent \u201cunknown user name or bad password\u201d events. However, its\u2019 easy enough to modify the WQL query we\u2019ll be using to check for additional event codes.<\/P>\n<P>Budding crime scene investigators out there might have already spotted an important clue: unsuccessful logons are reported in the Security event log, and each one is branded with the same event code: 529. That suggests a solution to our problem: to get back information about unsuccessful logons, all we need to do is query the Security event log for all events that have an event code of 529.<\/P>\n<P>In other words:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:{(Security)}\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colEvents = objWMIService.ExecQuery _\n        (&#8220;Select * from Win32_NTLogEvent Where Logfile = &#8216;Security&#8217; and &#8221; _\n            &amp; &#8220;EventCode = &#8216;529&#8217;&#8221;)<\/p>\n<p>For Each objEvent in colEvents\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>Elementary, my dear Watson. The only thing out of the ordinary occurs when we connect to the WMI service. To do that, we must include the <B>(Security)<\/B> privilege (enclosed in square braces), like so:<\/P><PRE class=\"codeSample\">Set objWMIService = GetObject(&#8220;winmgmts:{(Security)}\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\n<\/PRE>\n<P>You need to include this privilege in your script any time you want to access the Security event log. On top of that you (or at least your user account) must already possess the right to access the Security event log; including the privilege in the script won\u2019t magically bestow this right on you. On the other hand, leaving the privilege out of the script will prevent you from accessing the Security event log, regardless of any rights, privileges, and permissions your user account might have.<\/P>\n<P>After making the connection we simply select all instances of the <B>Win32_NTLogEvent<\/B> class where the <B>Logfile<\/B> is equal to Security and the <B>EventCode<\/B> is equal to 529. That returns a collection of unsuccessful logon events, which we proceed to echo back to the screen.<\/P>\n<P>This particular script returns a list of <I>all<\/I> the unsuccessful logon events. It\u2019s possible that you might want to retrieve those events only for a specified time period (for example, only unsuccessful logons from yesterday or for last week). Likewise, you might want to set up a monitoring system that would allow for instant notification any time such an event occurred. That sort of things lies a bit outside the scope of this column, but for more information and for sample scripts you might take a look at the <A href=\"http:\/\/null\/technet\/scriptcenter\/guide\/sas_log_overview.mspx\" target=\"_blank\"><B>Logs chapter<\/B><\/A> in the <I>Microsoft Windows 2000 Scripting Guide<\/I>.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I scan the event logs of my servers and return only information about unsuccessful logons?&#8212; LC Hey, LC. We\u2019ll assume that you have enabled security auditing on your servers. If you haven\u2019t, that\u2019s step one. What you\u2019ll want to do is &#8211; at a minimum &#8211; audit for logon event [&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-70573","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 scan the event logs of my servers and return only information about unsuccessful logons?&#8212; LC Hey, LC. We\u2019ll assume that you have enabled security auditing on your servers. If you haven\u2019t, that\u2019s step one. What you\u2019ll want to do is &#8211; at a minimum &#8211; audit for logon event [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70573","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=70573"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70573\/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=70573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}