{"id":69163,"date":"2005-08-16T20:19:00","date_gmt":"2005-08-16T20:19:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/08\/16\/how-can-i-monitor-the-event-logs-for-the-occurrence-of-a-specific-event\/"},"modified":"2005-08-16T20:19:00","modified_gmt":"2005-08-16T20:19:00","slug":"how-can-i-monitor-the-event-logs-for-the-occurrence-of-a-specific-event","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-monitor-the-event-logs-for-the-occurrence-of-a-specific-event\/","title":{"rendered":"How Can I Monitor the Event Logs for the Occurrence of a Specific Event?"},"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 monitor the event logs for the occurrence of a specific event?<BR><BR>&#8212; JP<\/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, JP. Why, you use an event log monitoring script, of course. (Yes, it\u2019s hard to believe, but they really <I>do<\/I> pay us to come up with brilliant answers like that.)<\/P>\n<P>OK, maybe we should be a little more specific: you use an event log monitoring script similar to this one:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:{(Security)}\\\\&#8221; &amp; _\n        strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colMonitoredEvents = objWMIService.ExecNotificationQuery _    \n    (&#8220;Select * from __InstanceCreationEvent Where &#8221; _\n        &amp; &#8220;TargetInstance ISA &#8216;Win32_NTLogEvent&#8217; &#8221; _\n            &amp; &#8220;and TargetInstance.EventCode = &#8216;0&#8217; &#8220;)<\/p>\n<p>Do\n    Set objLatestEvent = colMonitoredEvents.NextEvent\n    Wscript.Echo objLatestEvent.TargetInstance.User\n    Wscript.Echo objLatestEvent.TargetInstance.TimeWritten\n    Wscript.Echo objLatestEvent.TargetInstance.Message\n    Wscript.Echo\nLoop\n<\/PRE>\n<P>We won\u2019t spend any time in this column discussing the ins and outs of monitoring WMI events; if you\u2019d like more information about event monitoring you might want to view our <A href=\"http:\/\/msevents.microsoft.com\/cui\/eventdetail.aspx?EventID=1032268754&amp;culture=en-US\" target=\"_blank\"><B>Scripting Week 2<\/B><\/A> webcast on the subject. Instead, we\u2019ll just mention that what we\u2019re going to do is create a script that \u201csubscribes\u201d to a WMI event log event. Each time an event with a specific <B>EventCode<\/B> (in this case 0) is written to one of the event logs, our script will be notified and will report back values for the <B>User<\/B>, <B>TimeWritten<\/B>, and <B>Message<\/B> properties. The script will then slip back into suspended animation and patiently wait for the next event 0 to occur.<\/P>\n<P>By the way, we chose event 0 because that\u2019s the event code for Windows Script Host events. That means you can use a script like this one to write an event 0 to the Application log and thus test your monitoring script to ensure that it works:<\/P><PRE class=\"codeSample\">Const EVENT_SUCCESS = 0<\/p>\n<p>Set objShell = Wscript.CreateObject(&#8220;Wscript.Shell&#8221;)\nobjShell.LogEvent EVENT_SUCCESS, &#8220;Event written to an event log using a script.&#8221;\n<\/PRE>\n<P>As for the monitoring script, we begin by connecting to the WMI service. You might notice that when connecting to the WMI service we include the <B>{(Security)}<\/B> parameter. This allows us to subscribe to events written to <I>all<\/I> the event logs, including the Security log. Without this parameter we would receive events written from all the event logs <I>except<\/I> Security.<\/P>\n<P>Next we use the <B>ExecNotificationQuery<\/B> method to register for event log events. Our query itself looks like this:<\/P><PRE class=\"codeSample\">Set colMonitoredEvents = objWMIService.ExecNotificationQuery _    \n    (&#8220;Select * from __InstanceCreationEvent Where &#8221; _\n        &amp; &#8220;TargetInstance ISA &#8216;Win32_NTLogEvent&#8217; &#8221; _\n            &amp; &#8220;and TargetInstance.EventCode = &#8216;0&#8217; &#8221;\n<\/PRE>\n<P>What we\u2019re saying here is this: Show us all new instances of the <B>__InstanceCreationEvent<\/B> class, provided that the new instance happens to be a new entry to the event log (<B>Win32_NTLogEvent<\/B>) <I>and<\/I> the new entry has an EventCode of 0. If we wanted to monitor for different events (say, an event with the EventCode 528) all we\u2019d have to do is modify our query accordingly:<\/P><PRE class=\"codeSample\">Set colMonitoredEvents = objWMIService.ExecNotificationQuery _    \n    (&#8220;Select * from __InstanceCreationEvent Where &#8221; _\n        &amp; &#8220;TargetInstance ISA &#8216;Win32_NTLogEvent&#8217; &#8221; _\n            &amp; &#8220;and TargetInstance.EventCode = &#8216;528&#8217; &#8221;\n<\/PRE>\n<P>After that we set up a Do Loop with no exit condition (e.g., no <I>Do Until x = 1<\/I> kind of thing). This allows us to monitor events forever and ever: the script will continue to monitor until we reboot the computer or terminate the process under which the script runs. (Incidentally, you should run this script in a command window under CScript. If you run it under WScript, you\u2019ll have to click a bunch of message boxes any time an event 0 is written to the event log.)<\/P>\n<P>We then use this line of code to tell the script to sit there and wait for the next event to occur:<\/P><PRE class=\"codeSample\">Set objLatestEvent = colMonitoredEvents.NextEvent\n<\/PRE>\n<P>When a new event 0 <I>is<\/I> written to one of the event logs an exact copy of that event will be made available to our script; this replica object is known as the <B>TargetInstance<\/B>. At that point all we do is echo a few property values of this TargetInstance and then loop around and wait for the next event.<\/P>\n<P>In other words, to monitor the event logs for the occurrence of a specific event just use an event monitoring script. (If only we\u2019d said that in the first place\u2026.)<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I monitor the event logs for the occurrence of a specific event?&#8212; JP Hey, JP. Why, you use an event log monitoring script, of course. (Yes, it\u2019s hard to believe, but they really do pay us to come up with brilliant answers like that.) OK, maybe we should be a [&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,42,98,3,4,5],"class_list":["post-69163","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-event-logs","tag-events-and-monitoring","tag-logs-and-monitoring","tag-scripting-guy","tag-scripting-techniques","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I monitor the event logs for the occurrence of a specific event?&#8212; JP Hey, JP. Why, you use an event log monitoring script, of course. (Yes, it\u2019s hard to believe, but they really do pay us to come up with brilliant answers like that.) OK, maybe we should be a [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69163","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=69163"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69163\/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=69163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}