{"id":65433,"date":"2007-02-27T01:03:00","date_gmt":"2007-02-27T01:03:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/02\/27\/how-can-i-monitor-event-log-messages-for-specific-words\/"},"modified":"2007-02-27T01:03:00","modified_gmt":"2007-02-27T01:03:00","slug":"how-can-i-monitor-event-log-messages-for-specific-words","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-monitor-event-log-messages-for-specific-words\/","title":{"rendered":"How Can I Monitor Event Log Messages for Specific Words?"},"content":{"rendered":"<p><H2><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> <\/H2>\n<P>Hey, Scripting Guy! How can I monitor event log messages for a specific word or phrase?<BR><BR>&#8212; GH<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, GH. You know, now that the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/default.mspx\"><B>2007 Scripting Games<\/B><\/A> are over (have we mentioned that the 2008 Games are just a year away?) it\u2019s time for the Scripting Guys to rejoin the real world. Before we answer your question, however, could you give us a second to thumb through this pile of newspapers and see if we missed anything in the last month or so? Let\u2019s see: boring; boring; no one cares; boring. <\/P>\n<P>Oh, wait, here\u2019s something: allegedly a NASA astronaut went off the deep end, as they say, and set out to kidnap a romantic rival. Which leads to an obvious question: have the <I>Scripting Guys<\/I> ever gone off the deep end and tried to kidnap someone?<\/P>\n<P>Well, we\u2019re ashamed to admit it, but yes, we did. Shortly after <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/feb07\/hey0202.mspx\"><B>Peter Costantini<\/B><\/A> accepted his new position as a Microsoft Program Manager the remaining Scripting Guys went a tad bit crazy; in fact, we kidnapped Peter, tied him up in the basement of Scripting Guys Headquarters, and delivered an ultimatum: either rejoin the team, or bake us a batch of cupcakes. (The cupcakes were Dean\u2019s idea.) We gave him 24 hours to make up his mind.<\/P>\n<P>So, yes, we <I>did<\/I> go off the deep end \u2013 once. But we\u2019re back to normal now and, to prove it, we\u2019ll write you a script that monitors for specific words in an event log message. (Interestingly enough, that\u2019s the very same task Washington State Patrol officers use to test someone for drunk driving!)<\/P>\n<P>For example, suppose you have a script named Test.vbs. You\u2019d like to be notified any time an event written to the event log includes the term <I>Test.vbs<\/I>. How can you do that? Here\u2019s how:<\/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 colEvents = objWMIService.ExecNotificationQuery _    \n    (&#8220;Select * From __InstanceCreationEvent Where &#8221; _\n        &amp; &#8220;TargetInstance isa &#8216;Win32_NTLogEvent'&#8221;)<\/p>\n<p>Do\n    Set objEvent = colEvents.NextEvent\n    If InStr(LCase(objEvent.TargetInstance.Message), &#8220;test.vbs&#8221;) Then\n        Wscript.Echo Now\n        Wscript.Echo &#8220;Category: &#8221; &amp; objEvent.TargetInstance.Category\n        Wscript.Echo &#8220;Event Code: &#8221; &amp; objEvent.TargetInstance.EventCode\n        Wscript.Echo &#8220;Message: &#8221; &amp; objEvent.TargetInstance.Message\n        Wscript.Echo &#8220;Record Number: &#8221; &amp; objEvent.TargetInstance.RecordNumber\n        Wscript.Echo &#8220;Source Name: &#8221; &amp; objEvent.TargetInstance.SourceName\n        Wscript.Echo &#8220;Event Type: &#8221; &amp; objEvent.TargetInstance.Type\n        Wscript.Echo\n    End If\nLoop\n<\/PRE>\n<P>As you can see, we start out by connecting to the WMI service on the local computer (although this script can also monitor event logs on remote machines). Notice, however, that our WMI binding string includes the <B>Security<\/B> privilege: Set objWMIService = GetObject(&#8220;winmgmts:<B>{(Security)}<\/B>\\\\&#8221;. Is that important? You bet it is, provided that you want to monitor <I>all<\/I> the event logs, including the Security event log. If that\u2019s the case then you have to include the Security privilege. If you\u2019re not interested in the Security event log, however, then the answer is no, this isn\u2019t important at all. <\/P>\n<P>After connecting to the WMI service we issue the following query, a query that asks WMI to notify us any time a new event gets written to any of our event logs:<\/P><PRE class=\"codeSample\">Set colEvents = objWMIService.ExecNotificationQuery _    \n    (&#8220;Select * From __InstanceCreationEvent Where &#8221; _\n        &amp; &#8220;TargetInstance ISA &#8216;Win32_NTLogEvent'&#8221;)\n<\/PRE>\n<P>What we\u2019re doing here is monitoring for new object instances (<B>__InstanceCreationEvent<\/B>), but only if those new objects happen to be members of the <B>Win32_NTLogEvent<\/B> class. You might have noticed that we don\u2019t include a \u201cpolling interval\u201d (e.g., WITHIN 10) in this query. Why not? Because polling intervals are used only when monitoring WMI classes that don\u2019t have a custom event provider. That\u2019s not the case with the event logs; the event logs <I>do<\/I> have a custom event provider. Because of that we can get instant notification of any new events, without having to include code that instructs WMI to periodically go out and check for new events.<\/P>\n<TABLE class=\"dataTable\" id=\"EUE\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. Of course that doesn\u2019t make any sense; didn\u2019t you realize this is the <I>Hey, Scripting Guy!<\/I> column?!? But seriously, folks, if you could use a little more information about WMI monitoring scripts \u2013 as well as a definition of terms like polling interval \u2013 then you might want to view the Scripting Guys webcast <A href=\"http:\/\/msevents.microsoft.com\/cui\/eventdetail.aspx?EventID=1032268754&amp;culture=en-US\" target=\"_blank\"><B>An Ounce of Prevention: An Introduction to WMI Events<\/B><\/A>.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>After issuing our query we set up a Do loop that\u2019s designed to run forever and ever. That, by the way, is one reason why you should run this script in a command window under the CScript script host; if you ever decide that you <I>do<\/I> want to terminate the script you can do so simply by closing the command window in which the script is running.<\/P>\n<P>Inside that loop we use this block of code to instruct the script to sit around and wait until an event gets written to one of the event logs:<\/P><PRE class=\"codeSample\">Set objEvent = colEvents.NextEvent\n<\/PRE>\n<P>And what happens when an event <I>does<\/I> get written to one of the event logs? Well, when that happens we use the following line of code to see if the event message includes the term <I>test.vbs<\/I>: <\/P><PRE class=\"codeSample\">If InStr(LCase(objEvent.TargetInstance.Message), &#8220;test.vbs&#8221;) Then\n<\/PRE>\n<P>What are we doing here? Well, first of all we\u2019re grabbing the <B>Message<\/B> property from the <B>TargetInstance<\/B> object, an object that represents the record that was just written to the event log. We use the <B>LCase<\/B> function to convert all the characters in that message to lowercase, then use the <B>InStr<\/B> function to determine whether the value <I>test.vbs<\/I> can be found anywhere in that message. If <I>test.vbs<\/I> does <I>not<\/I> appear anywhere in the message then we simply loop around and wait for the next event to occur.<\/P>\n<P>But let\u2019s assume that the event message <I>does<\/I> include the text <I>test.vbs<\/I>. In that case we simply use this block of code to echo back information about that event:<\/P><PRE class=\"codeSample\">Wscript.Echo Now\nWscript.Echo &#8220;Category: &#8221; &amp; objEvent.TargetInstance.Category\nWscript.Echo &#8220;Event Code: &#8221; &amp; objEvent.TargetInstance.EventCode\nWscript.Echo &#8220;Message: &#8221; &amp; objEvent.TargetInstance.Message\nWscript.Echo &#8220;Record Number: &#8221; &amp; objEvent.TargetInstance.RecordNumber\nWscript.Echo &#8220;Source Name: &#8221; &amp; objEvent.TargetInstance.SourceName\nWscript.Echo &#8220;Event Type: &#8221; &amp; objEvent.TargetInstance.Type\n<\/PRE>\n<P>Needless to say, this is pretty standard WMI scripting: we simply echo back the current date and time (using the <B>Now<\/B> function), then echo back property values such as <B>Category<\/B>, <B>EventCode<\/B>, and <B>Message<\/B>. If you aren\u2019t sure what those property values are used for, well, too bad for you, huh?<\/P>\n<P>No, hey, just kidding. What we meant to say was this: if you\u2019d like more information about event logs and the Win32_NTLogEvent class then you might want to take a look at <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/guide\/sas_log_udqz.mspx\" target=\"_blank\"><B>this section<\/B><\/A> of the <I>Microsoft Windows 2000 Scripting Guide<\/I>.<\/P>\n<P>Oh: and if you\u2019d like to test today\u2019s script here\u2019s some code that will write an event to the Application event log, an event that \u2013 fortuitously enough, includes the term <I>test.vbs<\/I>:<\/P><PRE class=\"codeSample\">Const EVENT_SUCCESS = 0<\/p>\n<p>Set objShell = Wscript.CreateObject(&#8220;Wscript.Shell&#8221;)\nobjShell.LogEvent EVENT_SUCCESS, &#8220;Test.vbs started.&#8221;\n<\/PRE>\n<P>So what <I>did<\/I> happen with Peter? Believe it or not, we turned him loose after about 15 minutes; it only took that long for us to remember why we let him go in the first place. To add insult to injury, though, not only did Peter win his freedom, but he somehow also talked the rest of us into baking <I>him<\/I> cupcakes. But, then again, that\u2019s probably how Peter came to be such a powerful and important Program Manager while the rest of us are, well, Scripting Guys.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I monitor event log messages for a specific word or phrase?&#8212; GH Hey, GH. You know, now that the 2007 Scripting Games are over (have we mentioned that the 2008 Games are just a year away?) it\u2019s time for the Scripting Guys to rejoin the real world. Before we answer [&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-65433","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 monitor event log messages for a specific word or phrase?&#8212; GH Hey, GH. You know, now that the 2007 Scripting Games are over (have we mentioned that the 2008 Games are just a year away?) it\u2019s time for the Scripting Guys to rejoin the real world. Before we answer [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65433","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=65433"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65433\/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=65433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}