{"id":16271,"date":"2010-12-09T00:01:00","date_gmt":"2010-12-09T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/12\/09\/use-the-powershell-wmi-event-module-to-quickly-monitor-events\/"},"modified":"2010-12-09T00:01:00","modified_gmt":"2010-12-09T00:01:00","slug":"use-the-powershell-wmi-event-module-to-quickly-monitor-events","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-the-powershell-wmi-event-module-to-quickly-monitor-events\/","title":{"rendered":"Use the PowerShell WMI Event Module to Quickly Monitor Events"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p><b>Summary:<\/b> Learn how to use a Windows PowerShell WMI module to create permanent event monitors. <\/p>\n<p>&nbsp; <\/p>\n<p><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Question\" border=\"0\" title=\"Hey, Scripting Guy! Question\" \/>Hey, Scripting Guy! Can you provide some concrete examples of using the Windows PowerShell permanent event consumer module? <\/p>\n<p>&#8212; TS <\/p>\n<p>&nbsp; <\/p>\n<p><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Answer\" border=\"0\" title=\"Hey, Scripting Guy! Answer\" \/>Hello TS, Microsoft Scripting Guy Ed Wilson here. Today Trevor Sullivan is back to <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/12\/08\/use-a-powershell-module-to-work-with-wmi-permanent-events.aspx\">finish out the week<\/a> on his permanent event consumer module. <\/p>\n<p>&nbsp; <\/p>\n<h2>Introduction <\/h2>\n<p>Hello. <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/12\/08\/use-a-powershell-module-to-work-with-wmi-permanent-events.aspx\">Yesterday<\/a> we introduced the new <b>PowerEvents<\/b> module, and explained how it helps you monitor information in Windows Management Instrumentation (WMI). In today&rsquo;s blog post, we will look at a few examples of useful things we can monitor. <\/p>\n<h2>Examples <\/h2>\n<h3><a href=\"http:\/\/gallery.technet.microsoft.com\/ScriptCenter\/en-us\/site\/search?f%5B0%5D.Type=RootCategory&amp;f%5B0%5D.Value=activedirectory&amp;f%5B0%5D.Text=Active%20Directory\"><span style=\"font-weight: normal\"><span style=\"color: #0000ff\">Active Directory<\/span><\/span><\/a> User Accounts <\/h3>\n<p>One useful thing to monitor, and respond to, could be the creation of Active Directory user accounts. For example, after a user has been created, you could fire off a script to automatically create a home folder with appropriate permissions, an Exchange mailbox, and some other tasks. <\/p>\n<p>On an Active Directory domain controller, there is a WMI namespace that does not exist on other systems, called <b>root\\directory\\ldap<\/b>. There exists a class named <b>ds_user<\/b> that represents user accounts in Active Directory. By monitoring for new instances of <b>ds_user<\/b>, we can effectively determine when a new user has been created. <\/p>\n<p>&nbsp; <\/p>\n<h4>Filter <\/h4>\n<p>First, we create an event filter to capture the creation events: <\/p>\n<p class=\"CodeBlock\" style=\"margin: 4pt 0in 7pt;padding-left: 30px\"><span style=\"font-family: Lucida Sans Typewriter\">$MyFilter = New-WmiEventFilter &ndash;Name ADUserCreated &ndash;Query &ldquo;select * from __InstanceCreationEvent within 5 where TargetInstance ISA &lsquo;ds_user&rsquo;&rdquo; &ndash;EventNamespace root\\directory\\ldap<\/span><\/p>\n<p>&nbsp; <\/p>\n<p>As you will see, we introduced a new Windows PowerShell parameter on the <b>New-WmiEventFilter<\/b> function called <b>EventNamespace<\/b>. By default, the function assumes that the events that you want to capture are in root\\cimv2, and a lot of the time this will be true. However, because the class (<b>ds_user<\/b>) we are targeting exists in <b>root\\directory\\ldap<\/b>, we must specify that namespace. <\/p>\n<p>We set the <i>Name<\/i> parameter to something self-describing, and pass in our event query to the <i>Query<\/i> parameter. The <b>New-WmiEventFilter<\/b> function writes the filter to the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/winpsh\/manual\/pipe.mspx\"><span style=\"color: #0000ff\">pipeline<\/span><\/a> , so we store it in a variable called $MyFilter for later use. Now we are ready to set up a consumer! <\/p>\n<p>&nbsp;<\/p>\n<h4>Consumer <\/h4>\n<p>For the consumer, we&rsquo;ll just log the event to a file, to inform us that a user has been created. We can do that using the LogFile event consumer type. There are only a handful of parameters we have to set in order to configure a LogFile event consumer. Here is an example: <\/p>\n<p class=\"CodeBlock\" style=\"margin: 4pt 0in 7pt;padding-left: 30px\"><span style=\"font-family: Lucida Sans Typewriter\">$MyConsumer = New-WmiEventConsumer &ndash;ConsumerType LogFile &ndash;Name ADUserCreated &ndash;FileName c:\\temp\\Users.txt &ndash;Text &ldquo;Active Directory user has been created: %TargetInstance.DS_distinguishedName%&ldquo; <\/span><\/p>\n<p>&nbsp; <\/p>\n<p>In our call to <b>New-WmiEventConsumer<\/b>, we first specify the <b>ConsumerType<\/b> as &ldquo;LogFile.&rdquo; This tells the function to log events to a text file. Next, we give the consumer a useful name that describes its purpose in the <i>Name<\/i> parameter. The <i>FileName<\/i> parameter is used to specify the file path which you would like a message logged to. Finally, the <b>Text<\/b> property determines the text that will be logged to the file. You can use a variable, known as a WMI &ldquo;standard string template&rdquo; to create dynamic messages that contain specific information about the event that was fired. This way, instead of being informed that *some* user was created, you know exactly which one it was, and where it is located in the directory. <\/p>\n<p>&nbsp;<\/p>\n<h4>Binding <\/h4>\n<p>Finally, now that we&rsquo;ve created the filter and consumer (the &ldquo;meat&rdquo; of the whole process), we just have to create a binding in WMI, that tells it which filter we&rsquo;d like to match up to which consumer. For this, we use the <b>New-WmiFilterToConsumerBinding<\/b> function. There are two parameters we have to specify: the <i>Filter<\/i> and the Consumer &mdash; that&rsquo;s it! Here&rsquo;s an example: <\/p>\n<p class=\"CodeBlock\" style=\"margin: 4pt 0in 7pt;padding-left: 30px\"><span style=\"font-family: Lucida Sans Typewriter\">New-WmiFilterToConsumerBinding &ndash;Filter $MyFilter &ndash;Consumer $MyConsumer <\/span><\/p>\n<p>&nbsp; <\/p>\n<p>That is all there is to it! When you create a new user account on your domain controller, you should now see these events being logged to the file path that is specified in the consumer. This is shown in the following figure. <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6404.HSG-12-09-10-01.jpg\" border=\"0\" \/><\/p>\n<p style=\"padding-left: 30px\"><b>Note:<\/b> This example will only work on an Active Directory domain controller, with the <b>root\\directory\\ldap<\/b> WMI namespace available. It has not been tested with Active Directory Lightweight Directory Services (LDS). <\/p>\n<h3>Detect ConfigMgr Software Updates <\/h3>\n<p>Consider that you want to be notified when new software updates are assigned to a workstation in Microsoft <a href=\"http:\/\/www.microsoft.com\/systemcenter\/en\/us\/default.aspx\"><span style=\"color: #0000ff\">System Center<\/span><\/a> Configuration Manager. Perhaps you want to investigate the situation, or you want to perform some action when software updates are detected, such as closing down certain programs. You can do this with a WMI permanent event Registration. <\/p>\n<p>On a Configuration Manager (ConfigMgr) client, updates deployments (&ldquo;Deployment Management&rdquo; objects) are seen as instances of the <b>CCM_UpdateCIAssignment<\/b> class in the <b>root\\ccm\\policy\\machine\\actualconfig<\/b> namespace. Therefore, we know that we can detect new software updates deployments by creating an event filter that detects new instances of this class. <\/p>\n<h4>Filter<\/h4>\n<p>First, we create a filter to capture the software updates deployments: <\/p>\n<p class=\"CodeBlock\" style=\"margin: 4pt 0in 7pt;padding-left: 30px\"><span style=\"font-family: Lucida Sans Typewriter\">$MyFilter = New-WmiEventFilter &ndash;Name NewSoftwareUpdatesAssignment &ndash;Query &ldquo;select * from __InstanceCreationEvent within 5 where TargetInstance ISA &lsquo;CCM_UpdateCIAssignment&rsquo;&rdquo; &ndash;EventNamespace root\\ccm\\policy\\machine\\actualconfig <\/span><\/p>\n<p>&nbsp; <\/p>\n<p>We give the filter a name that self-describes the kind of events we are trying to capture. Next, we specify our event query that looks for new instances (a.k.a., creation events) of the <b>CCM_UpdateCIAssigment<\/b> class. Finally, because the class does not exist in the <b>root\\cimv2<\/b> namespace, we specify the namespace where it does exist, which is <b>root\\ccm\\policy\\machine\\actualconfig<\/b>. <\/p>\n<p>The next part is optional, but goes a little deeper into WMI eventing. If you are not interested, please skip to the Consumer heading. <\/p>\n<p>We can test the event query before using it in our filter, by leveraging the <b>notification query<\/b> button on wbemtest.exe. Here&rsquo;s how to do that step-by-step: <\/p>\n<ol>\n<li>Start wbemtest.<\/li>\n<li>Connect to the root\\ccm\\policy\\machine\\actualconfig namespace.<\/li>\n<li>Select the &ldquo;<i>asynchronous<\/i>&rdquo; option in the main wbemtest window.<\/li>\n<li>Click Notification Query.<\/li>\n<li>Paste the event query from above (quotes may not copy correctly).<\/li>\n<li>Click <b>Apply<\/b>.<\/li>\n<\/ol>\n<p>Now, go to your ConfigMgr console, and create a new updates deployment targeting the computer you just performed the above steps on. You should see an event appear in the query window. Double-click it, and you&rsquo;ll see something like what is shown in the following figure. <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2388.HSG-12-09-10-02.jpg\" border=\"0\" \/><\/p>\n<p>This screenshot represents what you would see if you drilled down into the WMI event <a href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2009\/08\/04\/hey-scripting-guy-how-do-i-use-wmi-with-windows-powershell-to-return-information-about-properties.aspx\"><span style=\"color: #0000ff\">using wbemtest<\/span><\/a> . Basically, the event you receive is an instance of <b>__InstanceCreationEvent<\/b>. This event object has a <b>TargetInstance<\/b> property, which contains an embedded WMI object. This embedded object is the instance of <b>CCM_UpdateCIAssignment<\/b> that we were looking to capture. We can now explore the details of the new object that was just created! <\/p>\n<p>&nbsp; <\/p>\n<h4>Consumer <\/h4>\n<p>Now that we&rsquo;ve got our filter created, let&rsquo;s go ahead and create a consumer (responder) for the events. Let&rsquo;s say that we want to terminate certain programs when an updates assignment is received. We can do that using a VBscript. To run a VBscript in response to an event, we use the<b> Script<\/b> consumer type. Here&rsquo;s an example that assumes that you have already developed the VBscript file that you want to execute: <\/p>\n<p class=\"CodeBlock\" style=\"margin: 4pt 0in 7pt;padding-left: 30px\"><span style=\"font-family: Lucida Sans Typewriter\">$MyConsumer = New-WmiEventConsumer &ndash;ConsumerType Script &ndash;Name NewSoftwareUpdatesAssignment &ndash;ScriptFile c:\\scripts\\NewSoftwareUpdatesAssignment.vbs <\/span><\/p>\n<p>&nbsp; <\/p>\n<p>First we instruct <b>New-WmiEventConsumer<\/b> to create a consumer of type &ldquo;Script.&rdquo; Next, we give the consumer a self-describing name so that it is easy to recall its purpose in the future. Finally, we specify the script file we want to execute in response to the event. Pretty simple! <\/p>\n<p>&nbsp; <\/p>\n<h4>Binding <\/h4>\n<p>Now that we&rsquo;ve created our filter and consumer, all we have to do is bind them together. Like the first example, call New-WmiFilterToConsumerBinding as follows: <\/p>\n<p class=\"CodeBlock\" style=\"margin: 4pt 0in 7pt;padding-left: 30px\"><span style=\"font-family: Lucida Sans Typewriter\">New-WmiFilterToConsumerBinding &ndash;Filter $MyFilter &ndash;Consumer $MyConsumer <\/span><\/p>\n<p>&nbsp; <\/p>\n<p>That&rsquo;s all there is to it! Now when a new software updates assignment is published to your computer, your script will execute! <\/p>\n<p>&nbsp; <\/p>\n<h2>Conclusion <\/h2>\n<p>In today&rsquo;s article, we&rsquo;ve looked at an example of how to use <b>PowerEvents<\/b> to create WMI event filters, consumers, and bindings to respond to creation of Active Directory user accounts, and deployment of software updates to a Configuration Manager client. Although these may be niche situations, consider that you can apply the same event detection &amp; response concepts to a lot of other information in WMI. <\/p>\n<p>For more information about the <b>PowerEvents<\/b> module for Windows PowerShell, and to learn more about WMI permanent event registrations, please visit <a href=\"http:\/\/powerevents.codeplex.com\/\"><span style=\"color: #0000ff\">http:\/\/powerevents.codeplex.com<\/span><\/a> . <\/p>\n<p>&nbsp; <\/p>\n<p>TS, that is all there is to using the Windows PowerShell WMI event module to simplify monitoring. This also wraps up the permanent event consumer week. I want to thank you Trevor for your hard work on writing the module, and for your contributions to the blog. Tomorrow I reach into the virtual mail bag for Quick-Hits Friday. <\/p>\n<p>I invite you to follow me on <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguystwitter\"><span style=\"color: #0000ff\">Twitter<\/span><\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\"><span style=\"color: #0000ff\">Facebook<\/span><\/a> . If you have any questions, send email to me at <a target=\"_blank\" href=\"mailto:scripter@microsoft.com\"><span style=\"color: #0000ff\">scripter@microsoft.com<\/span><\/a> or post them on the <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingforum\"><span style=\"color: #0000ff\">Official Scripting Guys Forum<\/span><\/a> . See you tomorrow. Until then, peace. <\/p>\n<p>&nbsp;<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy <\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Summary: Learn how to use a Windows PowerShell WMI module to create permanent event monitors. &nbsp; Hey, Scripting Guy! Can you provide some concrete examples of using the Windows PowerShell permanent event consumer module? &#8212; TS &nbsp; Hello TS, Microsoft Scripting Guy Ed Wilson here. Today Trevor Sullivan is back to finish out the [&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":[42,56,3,4,211,45,6],"class_list":["post-16271","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-events-and-monitoring","tag-guest-blogger","tag-scripting-guy","tag-scripting-techniques","tag-trevor-sullivan","tag-windows-powershell","tag-wmi"],"acf":[],"blog_post_summary":"<p>&nbsp; Summary: Learn how to use a Windows PowerShell WMI module to create permanent event monitors. &nbsp; Hey, Scripting Guy! Can you provide some concrete examples of using the Windows PowerShell permanent event consumer module? &#8212; TS &nbsp; Hello TS, Microsoft Scripting Guy Ed Wilson here. Today Trevor Sullivan is back to finish out the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/16271","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=16271"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/16271\/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=16271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=16271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=16271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}