<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://powershell.com/cs/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results for 'app:weblogs' matching tags 'Scripting Guy!' and 'events and monitoring'</title><link>http://powershell.com/cs/search/SearchResults.aspx?q=app:weblogs&amp;tag=Scripting+Guy!,events+and+monitoring&amp;orTags=0&amp;o=DateDescending</link><description>Search results for 'app:weblogs' matching tags 'Scripting Guy!' and 'events and monitoring'</description><dc:language>en-US</dc:language><generator>CommunityServer 2008.5 (Build: 30929.2835)</generator><item><title>Use PowerShell to Perform an Orderly Shutdown of a Server Running Hyper-V</title><link>http://powershell.com/cs/blogs/hey-scriptingguy/archive/2013/02/21/use-powershell-to-perform-an-orderly-shutdown-of-a-server-running-hyper-v.aspx</link><pubDate>Thu, 21 Feb 2013 06:00:00 GMT</pubDate><guid isPermaLink="false">f421715f-7aba-45f0-8a8d-44de5318a3a7:21775</guid><dc:creator>Anonymous</dc:creator><description>&lt;p&gt;&lt;strong&gt;Summary:&lt;/strong&gt; Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to shut down all virtual machines on a server running Hyper-V prior to shutting down the server.&lt;/p&gt;
&lt;p&gt;&lt;img src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/qanda/q-sm.jpg" alt="Hey, Scripting Guy! Question" /&gt;&amp;nbsp;Hey, Scripting Guy! From time to time I need to shut down one of our servers that is running the Hyper-V role. The problem is that these servers have multiple virtual machines running, and I do not want to crash the virtual machines. So, right now, I use the Hyper-V Manager, target the server running Hyper-V, and right-click every running virtual machine and select &lt;strong&gt;Shut Down&lt;/strong&gt;. I do not mind doing this, but some of the virtual machines are running things like Exchange and it takes them a long time to shut down. So, what should be a simple task of shutting down one of our Hyper-V servers ends up taking nearly an hour&amp;mdash;an hour of very boring work, I might add.&lt;/p&gt;
&lt;p&gt;&amp;mdash;BB&lt;/p&gt;
&lt;p&gt;&lt;img src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/qanda/a-sm.jpg" alt="Hey, Scripting Guy! Answer" /&gt;&amp;nbsp;Hello BB,&lt;/p&gt;
&lt;p&gt;Microsoft Scripting Guy, Ed Wilson, is here. Well, the snow is all gone. Yep, that is right, we had snow in Charlotte, NC. I am sitting here, sipping a nice cup of tea. I have been experimenting a bit. Today my teapot contains the following recipe: 2 teaspoons (tsp.) of English Breakfast, 1 tsp. of generic green tea, &amp;frac12; tsp. of organic orange peel, &amp;frac12; tsp. of licorice root, 1 tsp. of lemon grass, and a crushed cinnamon stick. Let it steep for 5 minutes, and I have a very nice pot of tea. It is sweet enough that I feel it needs no sweetener whatsoever.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;strong&gt;Note&lt;/strong&gt; &amp;nbsp;&amp;nbsp;In this post, I am using the cmdlets from Windows Server&amp;nbsp;2012 and the Hyper-V module. I obtained this module on my computer running Windows&amp;nbsp;8 by downloading and installing the RSAT tools. &lt;a href="http://www.microsoft.com/en-us/download/details.aspx?id=28972" target="_blank"&gt;The Windows 8 RSAT tools&lt;/a&gt; are available from the Microsoft Download Center.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Find all running virtual machines&lt;/h2&gt;
&lt;p&gt;BB, the first thing you need to do is to use the &lt;strong&gt;Get-VM&lt;/strong&gt; cmdlet and find all virtual machines that are running on the remote host. To do this, use the &lt;strong&gt;Get-VM&lt;/strong&gt; and pipe the results to the &lt;strong&gt;Where-Object&lt;/strong&gt; cmdlet and filter out for a state that is equal to &lt;strong&gt;running&lt;/strong&gt;&lt;em&gt;. &lt;/em&gt;It is not as difficult as it may sound. The command is shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;$runningVM = Get-VM -ComputerName $vmhost| where state -eq &amp;#39;running&amp;#39;&lt;/p&gt;
&lt;p&gt;Because you more than likely have more than a single virtual machine running on your remote Hyper-V server, I use the &lt;strong&gt;ForEach&lt;/strong&gt; language statement to walk through the collection of virtual machines that I store in the &lt;strong&gt;$RunningVM&lt;/strong&gt; variable. Inside the loop, I create a WMI Event that uses the &lt;strong&gt;Win32_ComputerShutdownEvent&lt;/strong&gt; WMI class to let me know when each virtual machine shuts down. This portion of the code is shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;foreach ($cn in $runningVM)&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;{&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Write-Debug &amp;quot;registering shutdown event for $($cn.name)&amp;quot;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;Register-WmiEvent -Class win32_ComputerShutdownEvent -ComputerName $cn.name `&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp; -SourceIdentifier $cn.name.tostring()&lt;/p&gt;
&lt;p&gt;Once I have registered the event, then I call the &lt;strong&gt;Stop-Computer&lt;/strong&gt; cmdlet to shut down the virtual machine. This code is shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Write-debug &amp;quot;Shutting down $($cn.name)&amp;quot;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp; Stop-Computer -ComputerName $cn.name -Force&lt;/p&gt;
&lt;p&gt;Because I registered a win32_ComputerShutdownEvent for the virtual machine, an event triggers after the virtual machine shuts down. To pick up this event, I use the &lt;strong&gt;Wait-Event&lt;/strong&gt; cmdlet. Once the computer shuts down, the event triggers. This code is shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Write-Debug &amp;quot;Waiting for shutdown to complete&amp;quot;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp; Wait-Event -SourceIdentifier $cn.Name.ToString()}&lt;/p&gt;
&lt;p&gt;After all of the virtual machines are shut down, it is time to shut down the Hyper-V host computer (the one that hosts all of the virtual machines). To do this, I use the &lt;strong&gt;Stop-Computer&lt;/strong&gt; cmdlet. This is shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Write-Debug &amp;quot;Shuting down $vmhost&amp;quot;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Stop-Computer -ComputerName $vmhost -Force&lt;/p&gt;
&lt;h2&gt;Monitor progress of the shutdown&lt;/h2&gt;
&lt;p&gt;Because I am watching the shutdown of the systems remotely, and I want to know what is happening, I decided to add a series of &lt;strong&gt;Write-Debug&lt;/strong&gt; statements. This is extremely easy to use, and when the script runs without the &lt;strong&gt;&amp;ndash;debug&lt;/strong&gt; switch only the default output appears. But when the script runs with the &lt;strong&gt;&amp;ndash;debug&lt;/strong&gt; switch, it displays each statement and prompts for the action to take place. This is an interactive type of experience, and it may not be what you want. If you are just wanting more information about each statement without the prompt, then use the &lt;strong&gt;Write-Verbose&lt;/strong&gt; cmdlet instead of &lt;strong&gt;Write-Debug&lt;/strong&gt;. They both work the same&amp;mdash;I get them for free as long as I add &lt;strong&gt;[cmdletbinding()]&lt;/strong&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;strong&gt;Note&lt;/strong&gt;&amp;nbsp; &amp;nbsp;During testing, I noticed that sometimes the script would appear to hang. This happens when the virtual machine stops more quickly than I am able to press &amp;ldquo;y&amp;rdquo; to confirm the next step, and therefore, the &lt;strong&gt;Wait-Event&lt;/strong&gt; is waiting for an event that has already occurred.&lt;/p&gt;
&lt;p&gt;After testing, I decided I was tired of typing &amp;ldquo;y&amp;rdquo; all the time, and so I did a global find and replace of &lt;strong&gt;Write-Debug&lt;/strong&gt; with &lt;strong&gt;Write-Verbose&lt;/strong&gt;. I also decided I needed to remove lingering event objects. So I added the following code.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Get-Event -SourceIdentifier $cn.name.Tostring() | Remove-Event&lt;/p&gt;
&lt;p&gt;The revised script is shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;[cmdletbinding()]&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Param($vmhost = &amp;#39;hyperv2&amp;#39;)&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Write-Verbose &amp;quot;getting running VM&amp;#39;s on $vmhost&amp;quot;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;$runningVM = Get-VM -ComputerName $vmhost| where state -eq &amp;#39;running&amp;#39;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;foreach ($cn in $runningVM)&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;{&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Write-Verbose &amp;quot;registering shutdown event for $($cn.name)&amp;quot;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;Register-WmiEvent -Class win32_ComputerShutdownEvent -ComputerName $cn.name `&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp; -SourceIdentifier $cn.name.tostring()&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Write-Verbose &amp;quot;Shutting down $($cn.name)&amp;quot;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp; Stop-Computer -ComputerName $cn.name -Force&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Write-Verbose &amp;quot;Waiting for shutdown to complete&amp;quot;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp; Wait-Event -SourceIdentifier $cn.Name.ToString()&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp; Get-Event -SourceIdentifier $cn.name.Tostring() | Remove-Event}&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Write-Verbose &amp;quot;Shuting down $vmhost&amp;quot;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Stop-Computer -ComputerName $vmhost -Force&lt;/p&gt;
&lt;p&gt;When I run the script now, the following is shown.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/7002.HSG_2D00_2_2D00_21_2D00_13_2D00_01.png"&gt;&lt;img title="Image of command output" src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/7002.HSG_2D00_2_2D00_21_2D00_13_2D00_01.png" alt="Image of command output" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;BB, that is all there is to using Windows PowerShell to shut down your virtual machines and then to shut down your Hyper-V server.&amp;nbsp; Join me tomorrow when I will talk about more cool stuff.&lt;/p&gt;
&lt;p&gt;I invite you to follow me on &lt;a href="http://bit.ly/scriptingguystwitter" target="_blank"&gt;Twitter&lt;/a&gt; and &lt;a href="http://bit.ly/scriptingguysfacebook" target="_blank"&gt;Facebook&lt;/a&gt;. If you have any questions, send email to me at &lt;a href="mailto:scripter@microsoft.com" target="_blank"&gt;scripter@microsoft.com&lt;/a&gt;, or post your questions on the &lt;a href="http://bit.ly/scriptingforum" target="_blank"&gt;Official Scripting Guys Forum&lt;/a&gt;. See you tomorrow. Until then, peace.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Ed Wilson, Microsoft Scripting Guy&lt;/strong&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3553972" width="1" height="1" alt="" /&gt;</description></item><item><title>Use PowerShell to Create a Permanent WMI Event to Launch a VBScript</title><link>http://powershell.com/cs/blogs/hey-scriptingguy/archive/2012/07/20/use-powershell-to-create-a-permanent-wmi-event-to-launch-a-vbscript.aspx</link><pubDate>Fri, 20 Jul 2012 05:00:00 GMT</pubDate><guid isPermaLink="false">f421715f-7aba-45f0-8a8d-44de5318a3a7:17844</guid><dc:creator>Anonymous</dc:creator><description>&lt;p&gt;&lt;b&gt;Summary&lt;/b&gt;: Microsoft Scripting Guy, Ed Wilson, discusses creating a permanent WMI event registration to monitor for new files and clean up the file names.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Microsoft Scripting Guy, Ed Wilson, is here. I just booked the room for the Atlanta (Alpharetta) PowerShell Saturday. This will be PowerShell Saturday event #003, and it will be held on Saturday (of course) on October 27 at the Microsoft Office in Alpharetta, Georgia in the United States. The event is not even up on the &lt;a href="http://powershellsaturday.com/" target="_blank"&gt;PowerShell Saturday page&lt;/a&gt; yet, but I thought you might like to get it on your calendars. Of course, the &lt;a href="http://powershellsaturday.com/002/" target="_blank"&gt;PowerShell Saturday event in Charlotte, North Carolina&lt;/a&gt; page is up, as are the abstracts for the sponsors, the speakers, and presentations. Keep your eyes and ears open because the registration site will go live soon, and there are only 200 tickets available. PowerShell Saturday in Columbus Ohio sold out in 13 days, so you will need to be quick if you want to attend this high-profile event.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;h2&gt;Creating a permanent WMI event to launch a VBScript&amp;hellip;&lt;/h2&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&amp;hellip;that launches a Windows PowerShell script&amp;hellip;&lt;br /&gt; &amp;hellip;that cleans up a folder of file names with leading spaces upon their arrival&amp;hellip;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;b&gt;Note&amp;nbsp;&amp;nbsp;&lt;/b&gt;This is the fourth blog in a five part series about monitoring a folder for the creation of files that have leading spaces in the file names. On Monday, I wrote &lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2012/07/16/use-powershell-to-detect-and-fix-files-with-leading-spaces.aspx" target="_blank"&gt;Use PowerShell to Detect and Fix Files with Leading Spaces&lt;/a&gt;, the scripts from that blog will be used today and again on Friday. On Tuesday, I wrote &lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2012/07/17/use-powershell-to-monitor-for-the-creation-of-new-files.aspx" target="_blank"&gt;Use PowerShell to Monitor for the Creation of New Files&lt;/a&gt;. This blog talks about creating a temporary WMI event to monitor for the creation of files in a particular folder (a query that is crucial to Friday&amp;rsquo;s blog). On Wednesday, I wrote about using a VBScript script to launch a Windows PowerShell script in &lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2012/07/18/how-to-use-vbscript-to-run-a-powershell-script.aspx" target="_blank"&gt;How to Use VBScript to Run a PowerShell Script&lt;/a&gt;. The reason for this blog is that the WMI class that is used for the permanent event consumer uses a VBScript script and not a Windows PowerShell script.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;On Thursday, I took a step back and installed the WMI Administrative Tools, and I examined the parts of a permanent WMI event registration. The blog &lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2012/07/19/using-the-wmi-admin-tools-to-check-on-permanent-events.aspx" target="_blank"&gt;Using the WMI Admin Tools to Check on Permanent Events&lt;/a&gt; is a great tutorial. From a reference perspective, you should check out the &lt;a href="http://aka.ms/InsideWMIEvents" target="_blank"&gt;An Insider&amp;rsquo;s Guide to Using WMI Events and PowerShell&lt;/a&gt;. This guide is a great reference, and it provides great assistance for understanding this powerful technology.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;One thing you should monitor, if you will pardon the pun, when designing and implementing permanent WMI event registrations is the fact that they have a lot of moving parts, and they can be rather complicated. You must test your design and your implementation in a lab environment that closely emulates your actual production systems before implanting any of these techniques.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;When I was creating the Windows PowerShell script for today&amp;rsquo;s blog, I actually ended up writing five separate scripts. The scripts are listed here. For ease of access, all five scripts are uploaded to the &lt;a href="http://gallery.technet.microsoft.com/scriptcenter/Create-Permenant-WMI-Event-f67ce5c2" target="_blank"&gt;Script Center Script Repository&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The first script is one that removes the permanent event registrations.&lt;/li&gt;
&lt;li&gt;The second script is a stripped down script to create my test files.&lt;/li&gt;
&lt;li&gt;The third script is the VBScript that is called by the permanent event registration.&lt;/li&gt;
&lt;li&gt;The fourth script is the Windows PowerShell script that is launched to clean up the files.&lt;/li&gt;
&lt;li&gt;The fifth script (the most complicated of all) is the one that does the actual WMI permanent event registration.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;/p&gt;
&lt;h2&gt;Avoid setting a short &lt;i&gt;within &lt;/i&gt;value&lt;/h2&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;When creating your WMI event query, make sure that you do not set a value of less than 30 (seconds) when going into production. It is common in testing, to set this value to 5 (seconds); but for production, never go less than 30 (seconds). Here is the WMI query that is used in the Create Permanent Event Consumer script.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;$query = @&amp;quot;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;Select * from __InstanceCreationEvent within 30&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;where targetInstance isa &amp;#39;Cim_DirectoryContainsFile&amp;#39;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;and targetInstance.GroupComponent = &amp;#39;Win32_Directory.Name=&amp;quot;c:\\\\test&amp;quot;&amp;#39;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;quot;@&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;b&gt;Note&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/b&gt;I discussed this query and the use of the &lt;b&gt;Here-String&lt;/b&gt; for formatting the query in &lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2012/07/17/use-powershell-to-monitor-for-the-creation-of-new-files.aspx"&gt;Use PowerShell to Monitor for the Creation of New Files&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;What happens if you use &lt;b&gt;within 5&lt;/b&gt;&lt;i&gt; &lt;/i&gt;in your query? Well, for one thing, Windows PowerShell polls every five seconds to see if there is a change. To see this behavior, I enabled the WMI-Activity trace log in the Event Viewer. One of the events is shown here.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/4846.HSG_2D00_7_2D00_20_2D00_12_2D00_01.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/4846.HSG_2D00_7_2D00_20_2D00_12_2D00_01.png" alt="Image of menu" title="Image of menu" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;To see the impact, of this, I used the following Windows PowerShell query to review these events.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Get-WinEvent -LogName *wmi-activity* -Force -Oldest | where { $_.id -eq 1 -AND $_.message -match &amp;#39;select&amp;#39;} | select -Last 20 | ft timecreated, message &amp;ndash;AutoSize&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;By using Windows PowerShell, I can easily see that the WMI query is executing every 5 seconds. (This is NOT the sort of thing you want to do on a heavily loaded production server.) The query and the results from the query are shown here.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/4331.HSG_2D00_7_2D00_20_2D00_12_2D00_02.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/4331.HSG_2D00_7_2D00_20_2D00_12_2D00_02.png" alt="Image of command output" title="Image of command output" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;h2&gt;Creating the three essential parts to the script&lt;/h2&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;There are three essential parts to a permanent WMI event registration. These were discussed in yesterday&amp;rsquo;s Hey, Scripting Guy! Blog, &lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2012/07/19/using-the-wmi-admin-tools-to-check-on-permanent-events.aspx"&gt;Using the WMI Admin Tools to Check on Permanent Events&lt;/a&gt;. The first item required is the &lt;b&gt;__EventFilter&lt;/b&gt;. The following code does this. (Keep in mind that the new instance of the &lt;b&gt;__EventFilter&lt;/b&gt; is created in the root\subscription WMI namespace. But the arguments to this state that the &lt;b&gt;EventNameSpace&lt;/b&gt; is in root\cimv2. The reason is that the class being used, &lt;b&gt;Cim_DirectoryContainsFile&lt;/b&gt;, resides in root\cimv2.)&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;$filterPath = Set-WmiInstance -Class __EventFilter `&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;-ComputerName $computer -Namespace $wmiNS -Arguments `&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp; @{name=$filterName; EventNameSpace=$filterNS; QueryLanguage=&amp;quot;WQL&amp;quot;;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Query=$query}&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;The second part is the &lt;b&gt;ActiveScriptEventConsumer&lt;/b&gt;. This portion of the script fills out the properties of the &lt;b&gt;ActiveScriptEventConsumer&lt;/b&gt;. The three essential portions are the name of the consumer, the script file, and the script engine. Note that the only engine supported is the VBScript scripting engine.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;$consumerPath = Set-WmiInstance -Class ActiveScriptEventConsumer `&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;-ComputerName $computer -Namespace $wmiNS `&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;-Arguments @{name=&amp;quot;CleanupFileNames&amp;quot;; ScriptFileName=$scriptFileName;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp; ScriptingEngine=&amp;quot;VBScript&amp;quot;}&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Finally, the last part is the &lt;b&gt;__FilterToConsumerBinding&lt;/b&gt;. When this part is configured properly, the green check mark appears in the WMI Administrative Tools as shown yesterday. This portion of the script is really easy. All that is required is to bind the filter and the consumer together as shown here.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Set-WmiInstance -Class __FilterToConsumerBinding -ComputerName $computer `&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp; -Namespace $wmiNS -arguments @{Filter=$filterPath; Consumer=$consumerPath} |&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp; out-null&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;When the CreatePermenantEventToMonitorForNewFilesAndStartScript.ps1 script runs, no output appears. This is where using the WMI Administrative Tools comes in useful (see &lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2012/07/19/using-the-wmi-admin-tools-to-check-on-permanent-events.aspx"&gt;Using the WMI Admin Tools to Check on Permanent Events&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Now to test the script, I create some new files in my test folder by using the CreateTestFiles.ps1 script. The newly created files are shown here.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/4834.HSG_2D00_7_2D00_20_2D00_12_2D00_03.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/4834.HSG_2D00_7_2D00_20_2D00_12_2D00_03.png" alt="Image of menu" title="Image of menu" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I have to move rather quickly, because I only have a maximum of 30 seconds before the event fires. Here is the cleaned up folder after the event fires.&lt;span style="line-height:0px;"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/0474.HSG_2D00_7_2D00_20_2D00_12_2D00_04.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/0474.HSG_2D00_7_2D00_20_2D00_12_2D00_04.png" alt="Image of menu" title="Image of menu" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;Clean-up work&lt;/h2&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I have mentioned before, that when creating a script that makes changes to system state, it is always a good idea to also write a script to do the clean-up work. This is especially true when you are doing demos, or as an aid while you are composing the script. Here is my very simple clean-up script. The thing to keep in mind is that you MUST use a good filter to find your &lt;b&gt;__EventFilter&lt;/b&gt; and your &lt;b&gt;__FilterToConsumerBinding&lt;/b&gt;, or you will remove things your computer may very well need.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;gwmi __eventFilter -namespace root\subscription -filter &amp;quot;name=&amp;#39;NewFile&amp;#39;&amp;quot;| Remove-WmiObject&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;gwmi activeScriptEventConsumer -Namespace root\subscription | Remove-WmiObject&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;gwmi __filtertoconsumerbinding -Namespace root\subscription -Filter &amp;quot;Filter = &amp;quot;&amp;quot;__eventfilter.name=&amp;#39;NewFile&amp;#39;&amp;quot;&amp;quot;&amp;quot;&amp;nbsp; | Remove-WmiObject&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;h2&gt;Logging&lt;/h2&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I was actually hoping that the WMI-Activity trace log would let me know each time the VBScript ran, but alas, that was not the case. So I added a log to my Windows PowerShell clean-up script that writes the date to a log file. This line is shown here.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;quot;called cleanup script $((get-date).tostring())&amp;quot; &amp;gt;&amp;gt;c:\fso\mylogging.txt&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;By adding this line to the clean-up files in my Windows PowerShell script, an entry writes to the log file each time the Windows PowerShell script is called from the VBScript.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;This ends our WMI Events Week. Join me tomorrow when I will look at the differences in performance between using a literal WMI filter and a WMI wildcard filter. It should be pretty cool. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I invite you to follow me on &lt;a href="http://bit.ly/scriptingguystwitter" target="_blank"&gt;Twitter&lt;/a&gt; and &lt;a href="http://bit.ly/scriptingguysfacebook" target="_blank"&gt;Facebook&lt;/a&gt;. If you have any questions, send email to me at &lt;a href="mailto:scripter@microsoft.com" target="_blank"&gt;scripter@microsoft.com&lt;/a&gt;, or post your questions on the &lt;a href="http://bit.ly/scriptingforum" target="_blank"&gt;Official Scripting Guys Forum&lt;/a&gt;. See you tomorrow. Until then, peace.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Ed Wilson, Microsoft Scripting Guy&lt;/b&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3509836" width="1" height="1" alt="" /&gt;</description></item><item><title>Using the WMI Admin Tools to Check on Permanent Events</title><link>http://powershell.com/cs/blogs/hey-scriptingguy/archive/2012/07/19/using-the-wmi-admin-tools-to-check-on-permanent-events.aspx</link><pubDate>Thu, 19 Jul 2012 05:00:00 GMT</pubDate><guid isPermaLink="false">f421715f-7aba-45f0-8a8d-44de5318a3a7:17804</guid><dc:creator>Anonymous</dc:creator><description>&lt;p&gt;&lt;b&gt;Summary&lt;/b&gt;: Microsoft Scripting Guy, Ed Wilson, shows how to use the WMI Administrative tools to check on Permanent WMI events created by Windows PowerShell.&lt;/p&gt;
&lt;p&gt;Microsoft Scripting Guy, Ed Wilson, is here. We continue to get new sponsors for the second Windows PowerShell Saturday event that will be held in Charlotte, North Carolina in the United States. The event will occur on September 15, 2012, and registration will be opening soon. I am impressed with the lineup of sponsors. There should be some great giveaways, but most importantly, there are going to be some GREAT speakers&amp;mdash;including the Microsoft Scripting Guy (me). You will want to bookmark the &lt;a href="http://powershellsaturday.com/" target="_blank"&gt;PowerShell Saturday&lt;/a&gt; website because not only does it contain information about the Windows PowerShell Saturday event in Charlotte, but it will also have information about the event for October in Atlanta.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;b&gt;Note&lt;/b&gt; &amp;nbsp;&amp;nbsp;This is the fourth blog in a five part series about monitoring a folder for the creation of files that have leading spaces in the file names. On Monday, I wrote &lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2012/07/16/use-powershell-to-detect-and-fix-files-with-leading-spaces.aspx" target="_blank"&gt;Use PowerShell to Detect and Fix Files with Leading Spaces&lt;/a&gt;, the scripts from that blog will be used today and again on Friday. On Tuesday, I wrote &lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2012/07/17/use-powershell-to-monitor-for-the-creation-of-new-files.aspx" target="_blank"&gt;Use PowerShell to Monitor for the Creation of New Files&lt;/a&gt;. This blog talks about creating a temporary WMI event to monitor for the creation of files in a particular folder (a query that is crucial to Friday&amp;rsquo;s blog). On Wednesday, I wrote about using a VBScript script to launch a Windows PowerShell script in &lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2012/07/18/how-to-use-vbscript-to-run-a-powershell-script.aspx" target="_blank"&gt;How to Use VBScript to Run a PowerShell Script&lt;/a&gt;. The reason for this blog is that the WMI class that is used for the permanent event consumer uses a VBScript script and not a Windows PowerShell script. From a reference perspective, you should check out the &lt;a href="http://aka.ms/InsideWMIEvents" target="_blank"&gt;An Insider&amp;rsquo;s Guide to Using WMI Events and PowerShell&lt;/a&gt;. This guide is a great reference, and it provides great assistance for understanding this powerful technology.&lt;/p&gt;
&lt;p&gt;The first thing you need to do is to download and install the &lt;a href="http://www.microsoft.com/en-us/download/details.aspx?id=24045" target="_blank"&gt;WMI Administrative Tools&lt;/a&gt;. The tools are an old HTML application with a very small file size (4.7 MB). In fact, the package is so small, I do not even save it locally. Rather, I run it from the download page. Installation is a simple click, click, and you are done. The only default change I make is that I make the application available for everyone and not just for the installer. The splash screen is shown here.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/4606.HSG_2D00_7_2D00_19_2D00_12_2D00_01.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/400x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/4606.HSG_2D00_7_2D00_19_2D00_12_2D00_01.png" alt="Image of menu" title="Image of menu" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;After you install the tools, you will find them in the WMI Tools folder on your start menu. There are two tools for working with WMI events. The first is the WMI Event Registration tool, and the second is the WMI Event Viewer. Because these are old HTML applications, they use Active X controls that are blocked by default. Therefore, you need to unblock the control before the tool becomes useful. The &lt;b&gt;Allow blocked content&lt;/b&gt;&lt;i&gt; &lt;/i&gt;message is shown here.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/6712.HSG_2D00_7_2D00_19_2D00_12_2D00_02.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/6712.HSG_2D00_7_2D00_19_2D00_12_2D00_02.png" alt="Image of menu" title="Image of menu" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;After you open the WMI Event Registration tool and allow the blocked content, you need to select the WMI namespace with which to work. The tool defaults to &lt;b&gt;root\cimv2&lt;/b&gt;, but permanent events reside in the root\subscription WMI namespace, and so it is necessary to change that location to see the &lt;b&gt;ActiveScriptEventConsumer&lt;/b&gt;. I also create the &lt;b&gt;EventFilter&lt;/b&gt; in the root\subscription namespace, so it will not be necessary to switch WMI namespaces to see the &lt;b&gt;EventFilter&lt;/b&gt; registration.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;b&gt;Note&lt;/b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;Keep in mind that this is the WMI Event Registration tool, not the WMI Event Viewer tool. This means that you can edit, delete, and create WMI Event Registrations by using this tool. Unfortunately, there is no Read-only mode for this tool.&lt;/p&gt;
&lt;p&gt;The following three things must be present and associated correctly for a permanent WMI event registration to work:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;An Event Consumer must be registered.&lt;/li&gt;
&lt;li&gt;An Event Filter must be registered.&lt;/li&gt;
&lt;li&gt;The Event Consumer must be associated with the Event Filter.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;In the image that follows, the &lt;b&gt;ActiveScriptEventConsumer&lt;/b&gt; appears in the root\subscription WMI namespace. Notice in the right pane, a green check mark appears next to the &lt;b&gt;__EventFilter&lt;/b&gt; class with the instance name of &lt;b&gt;&amp;ldquo;NewFile&amp;rdquo;&lt;/b&gt;. The green check mark appears under the column that states that it is registered. This image illustrates the Event Consumer to Event Filter binding.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/5658.HSG_2D00_7_2D00_19_2D00_12_2D00_03.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/5658.HSG_2D00_7_2D00_19_2D00_12_2D00_03.png" alt="Image of menu" title="Image of menu" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;To dig into the details of the &lt;b&gt;ActiveScriptEventConsumer&lt;/b&gt;, right-click it in the WMI Event Registration pane. Check out the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The Script File Name. It should point to a VBScript file that is accessible to the event consumer.&lt;/li&gt;
&lt;li&gt;If you are not using a script file, you can instead type the text of the script command in the &lt;b&gt;Script&lt;/b&gt; text box. This is a great way to make a permanent event consumer portable (so that it does not rely on an external file).&lt;/li&gt;
&lt;li&gt;The name of the &lt;b&gt;ActiveScriptEventConsumer&lt;/b&gt; and the path and the relative path.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These properties are shown in the image that follows.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/2185.HSG_2D00_7_2D00_19_2D00_12_2D00_04.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/2185.HSG_2D00_7_2D00_19_2D00_12_2D00_04.png" alt="Image of menu" title="Image of menu" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;To review the Event Filter, use the &lt;b&gt;Select&lt;/b&gt; arrow to choose &lt;b&gt;Filters&lt;/b&gt;. Expand the &lt;b&gt;__EventFilter&lt;/b&gt; node and ensure that the &lt;b&gt;EventConsumerClass&lt;/b&gt; associates with the &lt;b&gt;__EventFilter&lt;/b&gt;. To do this, look for the green check mark under the &lt;b&gt;Reg&lt;/b&gt; column. In addition, make sure that the &lt;b&gt;Instance&lt;/b&gt; name matches the name of the A&lt;b&gt;ctiveScriptEventConsumer&lt;/b&gt; detailed earlier. This result is shown here.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/7607.HSG_2D00_7_2D00_19_2D00_12_2D00_05.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/7607.HSG_2D00_7_2D00_19_2D00_12_2D00_05.png" alt="Image of menu" title="Image of menu" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;To check the properties of the &lt;b&gt;__EventFilter&lt;/b&gt;, right-click &lt;b&gt;__EventFilter&lt;/b&gt; in the left column, and then click &lt;b&gt;Edit Instance Properties&lt;/b&gt;&lt;i&gt; &lt;/i&gt;from the &lt;b&gt;Action&lt;/b&gt; menu. From here, you will want to check the following items:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The event namespace&lt;/li&gt;
&lt;li&gt;The name of the Event Filter&lt;/li&gt;
&lt;li&gt;The query being utilized&lt;/li&gt;
&lt;li&gt;The namespace of the event filter, in addition to the &lt;b&gt;Path&lt;/b&gt; and the &lt;b&gt;RelPath&lt;/b&gt; properties&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These properties are shown in the image that follows.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/2526.HSG_2D00_7_2D00_19_2D00_12_2D00_06.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/2526.HSG_2D00_7_2D00_19_2D00_12_2D00_06.png" alt="Image of menu" title="Image of menu" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;When all three items related to permanent WMI events are checked, it is time to proceed to testing. This will be the subject of tomorrow&amp;rsquo;s blog.&lt;/p&gt;
&lt;p&gt;That is all there is to using the WMI Administrative Tools to monitor for new WMI events. I invite you to join me tomorrow when I wrap up this five part series and discuss creating a permanent WMI event via a Windows PowerShell script that will monitor for new files created in a folder. If the file name has spaces at the beginning, it will automatically rename the file. It will be an exciting conclusion to an exciting week. So stay tuned, same script time, same script station (yes, the Scripting Wife and I went to see Batman). Take care.&lt;/p&gt;
&lt;p&gt;I invite you to follow me on &lt;a href="http://bit.ly/scriptingguystwitter" target="_blank"&gt;Twitter&lt;/a&gt; and &lt;a href="http://bit.ly/scriptingguysfacebook" target="_blank"&gt;Facebook&lt;/a&gt;. If you have any questions, send email to me at &lt;a href="mailto:scripter@microsoft.com" target="_blank"&gt;scripter@microsoft.com&lt;/a&gt;, or post your questions on the &lt;a href="http://bit.ly/scriptingforum" target="_blank"&gt;Official Scripting Guys Forum&lt;/a&gt;. See you tomorrow. Until then, peace.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Ed Wilson, Microsoft Scripting Guy&lt;/b&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3509648" width="1" height="1" alt="" /&gt;</description></item><item><title>How to Use VBScript to Run a PowerShell Script</title><link>http://powershell.com/cs/blogs/hey-scriptingguy/archive/2012/07/18/how-to-use-vbscript-to-run-a-powershell-script.aspx</link><pubDate>Wed, 18 Jul 2012 05:00:00 GMT</pubDate><guid isPermaLink="false">f421715f-7aba-45f0-8a8d-44de5318a3a7:17768</guid><dc:creator>Anonymous</dc:creator><description>&lt;p&gt;&lt;b&gt;Summary&lt;/b&gt;: Microsoft Scripting Guy, Ed Wilson, shows you that it&amp;#39;s easier than you think to use VBScript to run a Windows PowerShell script.&lt;/p&gt;
&lt;p&gt;Microsoft Scripting Guy, Ed Wilson, is here. Things are really heating up around here&amp;mdash;and I am not just talking about the hot, humid weather down in Charlotte, North Carolina in the United States. First, I am busily getting ready for my trip to Seattle, Washington next week. I will be speaking about using Windows PowerShell&amp;nbsp;3.0 to manage the remote Windows 8 desktop to a bunch of Microsoft people from all over the world. The event (called TechReady&amp;nbsp;15) is like TechEd, only it is only for Microsoft employees. Nevertheless, in every other fashion, including the size and scope of the event, it is like TechEd. I really look forward to speaking at this event, because it is an honor to get to speak to so many smart people, and it is a great chance to see my friends from all over the world.&lt;/p&gt;
&lt;p&gt;The second thing that is exciting are Windows&amp;nbsp;8 (which I have been running on my production machine for some time) and Office&amp;nbsp;15 (which I have just installed on my production machine). It is sooo cool, and is powerful, simple to use, and fun. Often powerful and simple do not go together in the computing world. This time, I think we did it right. The Scripting Wife is absolutely chomping at the bit to get a new slate device. I agreed to get her one for the holidays, but she wants it NOW!&lt;/p&gt;
&lt;p&gt;The third thing that is super exciting is Windows PowerShell Saturday on September 15 at the Charlotte Microsoft office. We have just about finalized the schedule, and we have all the speakers lined up. It will be a super cool event. Keep watching, because we will be opening registration very soon, and expect it to sell out within days. We have to limit the attendance to 200 people, so you will want to ensure that you are watching for the announcement. The announcement of the opening of registration will take place on Twitter, then on Facebook on the Scripting Guys Facebook site, and then on the Scripting Guys blog, and finally on the Learn PowerShell page. So this would be a good time to get a twitter account and start following @ScriptingWife and @ScriptingGuys. By the way, I love the Rowi app on Windows 8.&lt;/p&gt;
&lt;h2&gt;Creating a VBScript to run Windows PowerShell&lt;/h2&gt;
&lt;p&gt;When creating a permanent WMI event consumer that uses the &lt;b&gt;ActiveScriptEventConsumer&lt;/b&gt; WMI class, you need to use VBScript as the script type. This is because &lt;b&gt;ActiveScriptEventConsumer&lt;/b&gt; does not know how to run a Windows PowerShell script. This is not a huge problem, however, because writing a VBScript script to launch a Windows PowerShell script is very easy when you know the secrets.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;b&gt;Note&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/b&gt;This is the third blog in a five part series about monitoring a folder for the creation of files that have leading spaces in the file names. On Monday, I wrote &lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2012/07/16/use-powershell-to-detect-and-fix-files-with-leading-spaces.aspx" target="_blank"&gt;Use PowerShell to Detect and Fix Files with Leading Spaces&lt;/a&gt;, and the scripts from that blog will be used today and again on Friday. On Tuesday, I wrote &lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2012/07/17/use-powershell-to-monitor-for-the-creation-of-new-files.aspx" target="_blank"&gt;Use PowerShell to Monitor for the Creation of New Files&lt;/a&gt;. This blog talks about creating a temporary WMI event to monitor for the creation of files in a particular folder. This query is crucial to Friday&amp;rsquo;s blog.&lt;i&gt;&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;There are two methods available from the &lt;a href="http://msdn.microsoft.com/en-us/library/aew9yb99(v=VS.84).aspx" target="_blank"&gt;WshShell Object&lt;/a&gt; to launch other programs. These methods are the &lt;b&gt;Exec&lt;/b&gt;&lt;i&gt; &lt;/i&gt;method and the &lt;b&gt;Run&lt;/b&gt;&lt;i&gt; &lt;/i&gt;method. For my purpose, I use the &lt;b&gt;Run&lt;/b&gt;&lt;i&gt; &lt;/i&gt;method. It takes two lines of VBscript code; therefore, I use Notepad to create the script. Remember to save it as a .vbs file. The script is shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;LaunchPowerShell.VBS&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Set objShell = CreateObject(&amp;quot;Wscript.shell&amp;quot;)&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;objShell.run(&amp;quot;powershell -noexit -file c:\fso\CleanupFiles.ps1&amp;quot;)&lt;/p&gt;
&lt;p&gt;The first line of code creates the &lt;strong&gt;WshShell&lt;/strong&gt; object, and it stores the returned object in the &lt;strong&gt;objShell&lt;/strong&gt; variable. This line is shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Set objShell = CreateObject(&amp;quot;Wscript.shell&amp;quot;)&lt;/p&gt;
&lt;p&gt;The second line of code runs the command. The syntax of this command is critical. It is a good idea to use the Start / Run command to practice the syntax before embedding it in the VBScript script. Here is an example of using the &lt;strong&gt;Run&lt;/strong&gt; command to practice the syntax.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/5141.HSG_2D00_7_2D00_18_2D00_12_2D00_01.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/400x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/5141.HSG_2D00_7_2D00_18_2D00_12_2D00_01.png" alt="Image of command" title="Image of command" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;If you need to bypass the execution policy, you would add that switch to the command as well. The syntax to bypass the execution policy is shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;powershell -executionpolicy bypass -noexit -file c:\fso\helloworld.ps1&lt;/p&gt;
&lt;p&gt;It is also possible to run a specific Windows PowerShell command or series of commands from the VBScript script. This technique is shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;objShell.run(&amp;quot;powershell -noexit -command &amp;quot;&amp;quot;&amp;amp;{0..15 | % {Write-Host -foreground $_ &amp;#39;Hello World&amp;#39; }}&amp;quot;&amp;quot;&amp;quot;)&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;b&gt;Note&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/b&gt;Keep in mind that you are writing in VBScript. Therefore, you need to escape the quotation marks with another pair of quotation marks. Also, remember that you use REM to comment out a line, and not the pound sign character (&lt;b&gt;#&lt;/b&gt;) that is used in Windows PowerShell.&lt;/p&gt;
&lt;p&gt;The &lt;i&gt;CleanupFiles.ps1 &lt;/i&gt;script referenced in the VBScript script is the &lt;b&gt;Get-FilesWithLeadingSpaces&lt;/b&gt; function from Monday&amp;rsquo;s blog, &lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2012/07/16/use-powershell-to-detect-and-fix-files-with-leading-spaces.aspx" target="_blank"&gt;Use PowerShell to Detect and Fix Files with Leading Spaces&lt;/a&gt;. I removed it from the function and placed it in a file to make it easier to call from within the VBScript script. The &lt;i&gt;CleanupFiles.ps1 &lt;/i&gt;file is shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;strong&gt;CleanupFiles.ps1&lt;/strong&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Param(&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [string]$path = &amp;quot;c:\test&amp;quot;,&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [switch]$rename = $true&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; )&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;Get-ChildItem -Path $path -Recurse |&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;foreach-object -Begin {$count = 0} -process {&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;&amp;nbsp; if($_.name.length -ne $_.name.trim().length)&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if($rename)&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Rename-Item -Path $_.fullname -NewName (&amp;quot;{0}{1}{2}&amp;quot; -f `&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; $_.basename.trim(),$count,$_.extension)&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; $count++&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&amp;quot;$($_.basename) contains a leading space&amp;quot;}} }&lt;/p&gt;
&lt;p&gt;By using the &lt;b&gt;New-FilesWithLeadingSpaces&lt;/b&gt; function from Monday, I create 10 new files with leading spaces in the names in a folder named &lt;i&gt;test. &lt;/i&gt;These newly created folders are shown here.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/0066.HSG_2D00_7_2D00_18_2D00_12_2D00_02.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/0066.HSG_2D00_7_2D00_18_2D00_12_2D00_02.png" alt="Image of menu" title="Image of menu" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Now, I want to try out my VBScript script to see if I can run it, and cause it to launch the Windows PowerShell script to clean up the folder. I open a command prompt, and drag the VBScript script to the command line. The command prompt is shown here.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/5850.HSG_2D00_7_2D00_18_2D00_12_2D00_03.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/5850.HSG_2D00_7_2D00_18_2D00_12_2D00_03.png" alt="Image of command output" title="Image of command output" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;When I run the script, a Windows PowerShell console appears, but it does not look like it really did anything. Here is the newly appearing Windows PowerShell console.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/3806.HSG_2D00_7_2D00_18_2D00_12_2D00_04.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/3806.HSG_2D00_7_2D00_18_2D00_12_2D00_04.png" alt="Image of command output" title="Image of command output" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;But when I go to the &lt;i&gt;c:\test&lt;/i&gt; folder, I see that all the files are fixed. This is shown here.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/6710.HSG_2D00_7_2D00_18_2D00_12_2D00_05.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/6710.HSG_2D00_7_2D00_18_2D00_12_2D00_05.png" alt="Image of menu" title="Image of menu" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;That is all there is to using VBScript to run a Windows PowerShell script. WMI Event Monitoring Week will continue tomorrow when I will talk about using the WMI admin tools to monitor for newly arriving events.&lt;/p&gt;
&lt;p&gt;I invite you to follow me on &lt;a href="http://bit.ly/scriptingguystwitter" target="_blank"&gt;Twitter&lt;/a&gt; and &lt;a href="http://bit.ly/scriptingguysfacebook" target="_blank"&gt;Facebook&lt;/a&gt;. If you have any questions, send email to me at &lt;a href="mailto:scripter@microsoft.com" target="_blank"&gt;scripter@microsoft.com&lt;/a&gt;, or post your questions on the &lt;a href="http://bit.ly/scriptingforum" target="_blank"&gt;Official Scripting Guys Forum&lt;/a&gt;. See you tomorrow. Until then, peace.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Ed Wilson, Microsoft Scripting Guy&lt;/strong&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3509459" width="1" height="1" alt="" /&gt;</description></item><item><title>Use PowerShell to Monitor for the Creation of New Files</title><link>http://powershell.com/cs/blogs/hey-scriptingguy/archive/2012/07/17/use-powershell-to-monitor-for-the-creation-of-new-files.aspx</link><pubDate>Tue, 17 Jul 2012 05:00:00 GMT</pubDate><guid isPermaLink="false">f421715f-7aba-45f0-8a8d-44de5318a3a7:17736</guid><dc:creator>Anonymous</dc:creator><description>&lt;p&gt;&lt;b&gt;Summary&lt;/b&gt;: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to monitor for the creation of new files.&lt;/p&gt;
&lt;p&gt;Microsoft Scripting Guy, Ed Wilson, is here. Yesterday&amp;rsquo;s email from KS about his problems with files that contain leading spaces in them got me thinking. Although running a script on demand to find and rename files in a folder might work, it would be better to use an event to monitor the folder for newly created files. Then if the files match the naming pattern discovered yesterday, rename them by using the procedure from the script I posted yesterday in &lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2012/07/16/use-powershell-to-detect-and-fix-files-with-leading-spaces.aspx" target="_blank"&gt;Use PowerShell to Detect and Fix Files with Leading Spaces&lt;/a&gt;.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;b&gt;Note&lt;/b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;For more information about WMI event driven scripts, see &lt;a href="http://www.aka.ms/insideWMIevents" target="_blank"&gt;An Insider&amp;rsquo;s Guide to Using WMI Events and PowerShell&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Today I am going to develop a WMI event query to detect newly created files in a particular folder. Then I will use this WQL event query tomorrow to create a permanent WMI event consumer. In fact, whenever I am creating a permanent WMI event consumer, I always test it out as a temporary event consumer first. Creating a temporary event consumer with Windows PowerShell&amp;nbsp;2.0 is really easy, so it only makes sense to take this first step.&lt;/p&gt;
&lt;h2&gt;Creating a WMI WQL event query&lt;/h2&gt;
&lt;p&gt;The hardest part of creating a WMI WQL event query is, well&amp;hellip;just about everything. This stuff does not make much sense. Luckily, if you have WQL event query from VBScript or some other language, it is not too difficult to migrate the query to Windows PowerShell.&lt;/p&gt;
&lt;p&gt;When you start trying to do this, however, you run into weird quoting rules that only make a confusing situation more confusing. Luckily, Windows PowerShell can bring some sanity to this part of the process. The secret is to use a here-string. Here-strings are really finicky (they make &lt;a href="http://en.wikipedia.org/wiki/Morris_the_Cat" target="_blank"&gt;Morris the Cat&lt;/a&gt; seem like an omnivore). The basic syntax is to use a variable to hold the resulting here-string. The here-string begins with an ampersand and an opening quotation mark: &lt;b&gt;@&amp;quot;&lt;/b&gt;. Everything inside the here-string is interpreted literally so you do not need to worry with escaping special characters or quotation marks or any of that stuff. The here-string closes with a closing quotation mark ampersand: &lt;b&gt;&amp;quot;@&lt;/b&gt;.&lt;/p&gt;
&lt;p&gt;There are two rules that you must follow:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Immediately after the opening tag &lt;strong&gt;@&amp;quot;&lt;/strong&gt;, hit ENTER. Do not press the spacebar and then ENTER; you need the return right after the &lt;b&gt;@&amp;quot;&lt;/b&gt;.&lt;/li&gt;
&lt;li&gt;The closing tag (&lt;b&gt;&amp;quot;@&lt;/b&gt;) must be in position 1 of its own line. You cannot place it at the end of your last line of text, nor can you indent to make things &amp;ldquo;line up.&amp;rdquo; It must be in the first position of its own line.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;I referred to an old Hey Scripting Guy! Blog, &lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2004/10/11/how-can-i-automatically-run-a-script-any-time-a-file-is-added-to-a-folder.aspx" target="_blank"&gt;How Can I Automatically Run a Script Any Time a File is Added to a Folder&lt;/a&gt;, which was written nearly eight years ago in VBScript. Guess what? The query was just the thing I needed to refresh my memory for creating my new query. Here is the VBScript query from that blog.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;(&amp;quot;SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE &amp;quot; _&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;amp; &amp;quot;Targetinstance ISA &amp;#39;CIM_DirectoryContainsFile&amp;#39; and &amp;quot; _&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;amp; &amp;quot;TargetInstance.GroupComponent= &amp;quot; _&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;amp; &amp;quot;&amp;#39;Win32_Directory.Name=&amp;quot;&amp;quot;c:\\\\scripts&amp;quot;&amp;quot;&amp;#39;&amp;quot;)&lt;/p&gt;
&lt;p&gt;You can see where the use of a here-string vastly simplifies things by allowing me to forget about line continuation and having to escape quotation marks and other things. But also you can see how having a nice reference query, even from an eight-year old VBScript script, is also beneficial.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;b&gt;Note&lt;/b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;This is ONE of the major reasons I insisted on migrating all of the old Hey Scripting Guy! Blogs to the new Hey, Scripting Guy! Blog format four years ago when I became the Scripting Guy. I knew that a lot of that old code was easily adaptable to Windows PowerShell and would be useful for years to come. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;The WQL query itself is not too horribly bad. It begins by selecting everything from the &lt;b&gt;__InstanceCreationEvent&lt;/b&gt; WMI class. This class is a generic event class, and it will monitor for new instances of &amp;ldquo;stuff.&amp;rdquo; It can be anything from a new entry in an event log to a new file. The problem with monitoring for a newly created file is that a file must reside somewhere&amp;mdash;for example, inside a directory. To find a file in a directory by using WMI means that we need to use an association WMI class.&lt;/p&gt;
&lt;p&gt;The &lt;b&gt;Cim_DirectoryContainsFile&lt;/b&gt; WMI class associates files and directories. When working with association classes, there is always a property that relates one to the other. Here we are looking for the &lt;b&gt;GroupComponent &lt;/b&gt;portion of the association. &lt;b&gt;GroupComponent &lt;/b&gt;is an instance of the &lt;b&gt;Win32_Directory&lt;/b&gt; WMI class. Because we are interested in a particular directory, we need to use the &lt;b&gt;Key&lt;/b&gt; property for &lt;b&gt;GroupComponent&lt;/b&gt;. Here, the key is the name of the folder. The name of the folder uses POSIX notation; therefore, it requires \\\\ (four back slashes). The query is shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;$query = @&amp;quot;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;Select * from __InstanceCreationEvent within 10&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;where targetInstance isa &amp;#39;Cim_DirectoryContainsFile&amp;#39;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;and targetInstance.GroupComponent = &amp;#39;Win32_Directory.Name=&amp;quot;c:\\\\test&amp;quot;&amp;#39;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;quot;@&lt;/p&gt;
&lt;h2&gt;Register the WMI event&lt;/h2&gt;
&lt;p&gt;Now I need register the WMI event. In Windows PowerShell&amp;nbsp;2.0 and Windows PowerShell&amp;nbsp;3.0, this is really easy. I use the &lt;b&gt;Register-WmiEvent&lt;/b&gt; cmdlet and specify the WQL query. I also need to create a value for the &lt;b&gt;SourceIdentifier&lt;/b&gt;&lt;i&gt; &lt;/i&gt;property so I can monitor the job. Here, I register the WMI event by using the query contained in the &lt;b&gt;$query&lt;/b&gt; variable, and I specify a &lt;b&gt;SourceIdentifier&lt;/b&gt;&lt;i&gt; &lt;/i&gt;of &lt;b&gt;MonitorFiles&lt;/b&gt;.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Register-WmiEvent -Query $query -SourceIdentifier &amp;quot;MonitorFiles&amp;quot;&lt;/p&gt;
&lt;p&gt;Upon registering the event, I can do any number of things. The easiest thing to do is to wait for the event to occur. The &lt;b&gt;Wait-Event&lt;/b&gt; cmdlet will wait for the event that is identified by the &lt;b&gt;SourceIdentifier&lt;/b&gt;&lt;i&gt; &lt;/i&gt;to trigger. After it does, I store the generated event in the &lt;b&gt;$fileEvent&lt;/b&gt; variable as shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;$fileEvent = Wait-Event -SourceIdentifier &amp;quot;MonitorFiles&amp;quot;&lt;/p&gt;
&lt;p&gt;Once again, I could do anything I want to do upon notification that an event triggers. Here, I simply display the complete path to the newly created file. I will use this information tomorrow in my follow-up to today&amp;rsquo;s blog. Notice that the &lt;b&gt;$fileEvent&lt;/b&gt; variable contains a rich object. You might want to play around with &lt;b&gt;Get-Member&lt;/b&gt; to explore this object.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;$fileEvent.SourceEventArgs.NewEvent.TargetInstance.PartComponent&lt;/p&gt;
&lt;p&gt;When the script runs, it waits for an event to trigger. This behavior is shown in the image that follows.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/6102.HSG_2D00_7_2D00_17_2D00_12_2D00_01.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/6102.HSG_2D00_7_2D00_17_2D00_12_2D00_01.png" alt="Image of command output" title="Image of command output" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;When I create a file in the c:\test folder, within 10 seconds the temporary event consumer detects the presence of the newly created file, and &lt;b&gt;Wait-Event&lt;/b&gt; returns the event to the &lt;b&gt;$fileEvent&lt;/b&gt; variable. The script then displays the path to the newly created file. The image that follows illustrates the Windows PowerShell ISE following the generation of the new event.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/6746.HSG_2D00_7_2D00_17_2D00_12_2D00_02.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/6746.HSG_2D00_7_2D00_17_2D00_12_2D00_02.png" alt="Image of command output" title="Image of command output" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;Clean up after creating a temporary event subscriber&lt;/h2&gt;
&lt;p&gt;If you attempt to run the script a second time, you will more than likely receive errors. The error is because the event &lt;b&gt;SourceIdentifier&lt;/b&gt;&lt;i&gt; &lt;/i&gt;&amp;ldquo;MonitorFiles&amp;rdquo; already exists. The way to correct this is to unregister the event. You can do this by name, by specifying the &lt;b&gt;SourceIdentifier&lt;/b&gt;&lt;i&gt; &lt;/i&gt;property of the &lt;b&gt;Unregister-Event&lt;/b&gt; cmdlet. But the easier way to do this is to use the &lt;b&gt;Get-EventSubscriber&lt;/b&gt; cmdlet, and pipe the event subscriber to the &lt;b&gt;Unregister-Event&lt;/b&gt; cmdlet as shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Get-EventSubscriber | Unregister-Event&lt;/p&gt;
&lt;p&gt;The unpredictable results portion of the scenario is that one WMI event already exists&amp;mdash;the one that generated during testing. It is certainly possible to work with multiple events, but it is also easier to just clean up. The easiest way to do this is to find all of the WMI events by using the &lt;b&gt;Get-Event&lt;/b&gt; cmdlet, and then pipe all the found WMI events to the &lt;b&gt;Remove-Event&lt;/b&gt; cmdlet. This command is shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Get-Event | Remove-Event&lt;/p&gt;
&lt;p&gt;If you are writing a temporary WMI event consumer script, it makes sense to place the two previous commands into a function called something like &lt;b&gt;Remove-WMIEventAndSubscriber&lt;/b&gt;. Such a function is shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Function Remove-WMIEventAndSubscriber&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;{&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;Get-EventSubscriber | Unregister-Event&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;Get-Event | Remove-Event&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;} #end function Remove-WmiEventAndSubscriber&lt;/p&gt;
&lt;p&gt;A function such as &lt;b&gt;Remove-WMIEventAndSubscriber&lt;/b&gt; makes testing your script inside the Windows PowerShell ISE much easier, and it saves a lot of typing because you reset the environment each time you decide to run an additional test.&lt;/p&gt;
&lt;p&gt;That is all there is to using a temporary WMI event to monitor a folder for the creation of a new file. Join me tomorrow for more Windows PowerShell cool stuff.&lt;/p&gt;
&lt;p&gt;I invite you to follow me on &lt;a href="http://bit.ly/scriptingguystwitter" target="_blank"&gt;Twitter&lt;/a&gt; and &lt;a href="http://bit.ly/scriptingguysfacebook" target="_blank"&gt;Facebook&lt;/a&gt;. If you have any questions, send email to me at &lt;a href="mailto:scripter@microsoft.com" target="_blank"&gt;scripter@microsoft.com&lt;/a&gt;, or post your questions on the &lt;a href="http://bit.ly/scriptingforum" target="_blank"&gt;Official Scripting Guys Forum&lt;/a&gt;. See you tomorrow. Until then, peace.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Ed Wilson, Microsoft Scripting Guy&lt;/b&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3509198" width="1" height="1" alt="" /&gt;</description></item><item><title>The Easy Way to Monitor for an IP Address by Using PowerShell</title><link>http://powershell.com/cs/blogs/hey-scriptingguy/archive/2012/06/20/the-easy-way-to-monitor-for-an-ip-address-by-using-powershell.aspx</link><pubDate>Wed, 20 Jun 2012 05:00:00 GMT</pubDate><guid isPermaLink="false">f421715f-7aba-45f0-8a8d-44de5318a3a7:17137</guid><dc:creator>Anonymous</dc:creator><description>&lt;p&gt;&lt;b&gt;Summary&lt;/b&gt;: The Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to monitor for acquiring an IP address.&lt;/p&gt;
&lt;p&gt;&lt;img src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/qanda/q-sm.jpg" alt="Hey, Scripting Guy! Question" /&gt;&amp;nbsp;Hey, Scripting Guy! We have a problem. It seems that when people with laptops come into the office, it takes forever for them to obtain network access. On the tool bar, a blue circle of death spins in an infinite loop. When I hover over the circle, it says &amp;ldquo;identifying network.&amp;rdquo; There is nothing to identify&amp;mdash;it is our corporate network. I recently figured out that the circle is lying to me. It is not really trying to identify the network; but rather, it is waiting on an Internet Protocol (IP) address. I know you can use a &lt;a href="http://en.wikipedia.org/wiki/Ping" target="_blank"&gt;ping&lt;/a&gt; command to ping forever, but I can never remember the syntax. In addition, it does not really tell me what I want to know. What I really want to know is if the computer has obtained an IP address. Can I use Windows PowerShell to do this?&lt;/p&gt;
&lt;p&gt;&amp;mdash;MH&lt;/p&gt;
&lt;p&gt;&lt;img src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/qanda/a-sm.jpg" alt="Hey, Scripting Guy! Answer" /&gt;&amp;nbsp;Hello MH,&lt;/p&gt;
&lt;p&gt;Microsoft Scripting Guy, Ed Wilson, is here. Often when one thinks about monitoring, one turns ones attention to &lt;a href="http://msdn.microsoft.com/en-us/library/aa394582(VS.85).aspx" target="_blank"&gt;Windows Management Instrumentation&lt;/a&gt; (WMI) events. In fact, I recently published &lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2012/06/08/an-insider-s-guide-to-using-wmi-events-and-powershell.aspx" target="_blank"&gt;Insider&amp;rsquo;s Guide to Using WMI Events and PowerShell&lt;/a&gt;&lt;i&gt; &lt;/i&gt;that listed Hey, Scripting Guy! resources for working with this powerful and cool technology. But at times, such an approach is a bit like using a &lt;a href="http://en.wikipedia.org/wiki/Steamroller" target="_blank"&gt;steamroller&lt;/a&gt; to make hamburger patties&amp;mdash;it might work, but is not necessarily the easiest way to do things. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;MH, one easy way to monitor for acquisition of an Internet Protocol (IP) address is to use the range operator and pair it with the &lt;b&gt;ForEach-Object&lt;/b&gt; cmdlet. In fact, this technique is one of my &lt;a href="http://blogs.technet.com/search/searchresults.aspx?q=top%20ten%20tricks" target="_blank"&gt;top ten favorite Windows PowerShell tricks&lt;/a&gt; because it is so flexible and so powerful. My approach here is to do something really easy, really quick, and with minimal typing. My approach is not efficient, elegant, or even &amp;ldquo;correct&amp;rdquo; (as far as Windows PowerShell purists go). I would venture there are even easier ways to do this. But the advantage here is that the command is easy to understand, easy to remember, and easy to type.&lt;/p&gt;
&lt;p&gt;The command that follows begins by using the range operator to create an array of numbers from 1 through 500. These numbers cross the &lt;a href="http://www.microsoft.com/technet/scriptcenter/topics/winpsh/manual/pipe.mspx" target="_blank"&gt;pipeline&lt;/a&gt; one at a time. The &lt;b&gt;ForEach-Object&lt;/b&gt; cmdlet calls the &lt;b&gt;ipconfig&lt;/b&gt; command once for each number. The results of &lt;b&gt;ipconfig&lt;/b&gt; pipe to the &lt;b&gt;Select-String&lt;/b&gt; cmdlet. &lt;b&gt;Select-String&lt;/b&gt; displays only the line of output that contains the letters &lt;b&gt;ipv4&lt;/b&gt;. By default, there is no alias for the &lt;b&gt;Select-String&lt;/b&gt; cmdlet, but remember that by using Tab Expansion, you can greatly reduce your typing load. The following command illustrates how to use &lt;b&gt;Select-String&lt;/b&gt; to retrieve only the line of text containing &lt;b&gt;ipv4&lt;/b&gt;.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\&amp;gt; ipconfig | &lt;b&gt;Select-String&lt;/b&gt; ipv4&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;&amp;nbsp; IPv4 Address. . . . . . . . . . . : 192.168.0.54&lt;/p&gt;
&lt;p&gt;OK MH, so I can now find my Internet Protocol (IP) address from &lt;b&gt;ipconfig&lt;/b&gt; by using the &lt;b&gt;Select-String&lt;/b&gt; cmdlet. The next thing, I need to do is to wait for a couple of seconds and clear the Windows PowerShell console host. To do this, I use the &lt;b&gt;Start-Sleep&lt;/b&gt; cmdlet (&lt;b&gt;sleep&lt;/b&gt; is an alias) and &lt;b&gt;cls&lt;/b&gt; (an alias for the &lt;b&gt;Clear-Host&lt;/b&gt; function). The complete command is shown here.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;1..500 | % {ipconfig | &lt;b&gt;Select-String&lt;/b&gt; ipv4 ; sleep 2; cls }&lt;/p&gt;
&lt;p&gt;When I run the command, for the first two seconds it displays both the command and the output of the command as shown here.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/8875.hsg_2D00_6_2D00_20_2D00_12_2D00_01.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/8875.hsg_2D00_6_2D00_20_2D00_12_2D00_01.png" alt="Image of command output" title="Image of command output" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;After the first two seconds of run time, the Windows PowerShell console clears, and only the IPv4 Address displays as shown here.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/2746.hsg_2D00_6_2D00_20_2D00_12_2D00_02.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/2746.hsg_2D00_6_2D00_20_2D00_12_2D00_02.png" alt="Image of command output" title="Image of command output" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;MH, that is all there is to using Windows PowerShell to monitor for changes in the IP address. Join me tomorrow for more Windows PowerShell cool stuff.&lt;/p&gt;
&lt;p&gt;I invite you to follow me on &lt;a href="http://bit.ly/scriptingguystwitter" target="_blank"&gt;Twitter&lt;/a&gt; and &lt;a href="http://bit.ly/scriptingguysfacebook" target="_blank"&gt;Facebook&lt;/a&gt;. If you have any questions, send email to me at &lt;a href="mailto:scripter@microsoft.com" target="_blank"&gt;scripter@microsoft.com&lt;/a&gt;, or post your questions on the &lt;a href="http://social.technet.microsoft.com/Forums/en/ITCG/threads/" target="_blank"&gt;Official Scripting Guys Forum.&lt;/a&gt;&amp;nbsp;See you tomorrow. Until then, peace.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Ed Wilson, Microsoft Scripting Guy&lt;/b&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3504730" width="1" height="1" alt="" /&gt;</description></item><item><title>An Insider’s Guide to Using WMI Events and PowerShell</title><link>http://powershell.com/cs/blogs/hey-scriptingguy/archive/2012/06/08/an-insider-s-guide-to-using-wmi-events-and-powershell.aspx</link><pubDate>Fri, 08 Jun 2012 05:00:00 GMT</pubDate><guid isPermaLink="false">f421715f-7aba-45f0-8a8d-44de5318a3a7:16947</guid><dc:creator>Anonymous</dc:creator><description>&lt;p&gt;&lt;b&gt;Summary&lt;/b&gt;: Microsoft Scripting Guy, Ed Wilson, reviews and discusses Hey, Scripting Guy! Blog posts about WMI events and Windows PowerShell.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Microsoft Scripting Guy, Ed Wilson, is here. Tickets for the &lt;a href="http://itprocamp.com/jacksonville/" target="_blank"&gt;Jacksonville IT Pro Camp&lt;/a&gt; are rapidly disappearing. If you are anywhere near Jacksonville, Florida on June 16, 2012, you definitely check it out. There are sessions about Windows PowerShell Best Practices, Windows PowerShell Remoting, and general Windows PowerShell admin. Not to mention sessions about Hyper_V, SharePoint, SQL Server, Team Foundation Server, and more. It will be an awesome opportunity to learn from some of the best people in the field. The presenters are Microsoft MVPs, Microsoft PFEs, community leaders, and of course, the Microsoft Scripting Guy.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Today, I want to look at the Hey, Scripting Guy! Blog posts that discuss WMI eventing.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;h1&gt;All about WMI eventing&lt;/h1&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;There are two types of WMI events: temporary event consumers and permanent event consumers. The temporary event consumers are events that you set up that will last until you exit the script. A good way to use a temporary event consumer is to start a script that watches something, make a change that changes what is being watched, and then when the event fires, you capture the event and do whatever you wanted to do. Permanent event consumers are written to the WMI repository, survive reboots, and run inside the WMI processes on your computer. There is no script to close, so they run as if they were services. These are used by SCOM and other applications. They are pretty complex, but they are not beyond the realm of people who are experienced in scripting and the use of WMI.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I have been writing about WMI eventing since back in the VBScript days. (In fact, I have an entire chapter about it in my WMI book). I have posts on the Hey, Scripting Guy! Blog from Windows PowerShell&amp;nbsp;1.0 days. These blogs are not necessarily obsolete because they talk about the underlying eventing .NET Framework classes. In addition, they illustrate the use of generic WMI event classes (the query is the same in Windows PowerShell&amp;nbsp;2.0 and Windows PowerShell&amp;nbsp;3.0).&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;There are two types of WMI event classes: implicit and generic. An implicit event class is really easy to use in Windows PowerShell&amp;nbsp;2.0 because the class already knows how to do events. A generic WMI event class is more difficult to use because the query is more complex. The nice thing is that the query is basically the same in Windows PowerShell and in VBScript, so you have tons of resources for these types of queries.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;h2&gt;Temporary WMI event consumers&lt;/h2&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;All of the following blogs talk about working with temporary WMI event consumers. These are typically set up to monitor a specific item, or items, for only a short period of time.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;h3&gt;Using generic WMI classes&lt;/h3&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2009/01/19/how-can-i-be-notified-when-a-process-begins.aspx" target="_blank"&gt;How Can I Be Notified When a Process Begins?&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;In this blog, I talk about using a generic WMI class, &lt;b&gt;__InstanceCreationEvent&lt;/b&gt;, to monitor for a new process to begin. The blog was written in Windows PowerShell&amp;nbsp;1.0. It is valuable because I discuss the objects that are involved and show how to create a query by using a generic WMI event class. Please note that there is a &lt;b&gt;Win32_ProcessStartTrace&lt;/b&gt; WMI class that makes it easier to monitor for a process to begin. The real value of the blog is in showing how to query a generic class.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2009/01/20/how-do-i-display-a-message-and-the-time-a-process-was-terminated.aspx" target="_blank"&gt;How Do I Display a Message and the Time a Process Was Terminated?&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;This blog also uses a generic WMI class, &lt;b&gt;__InstanceDeletionEvent&lt;/b&gt;, to monitor when a process goes away. Again, this blog is written in Windows PowerShell&amp;nbsp;1.0, so you will not need to manually create the &lt;b&gt;EventWatcher&lt;/b&gt; class. In addition, there is a &lt;b&gt;Win32_ProcessStopTrace&lt;/b&gt; WMI class that is an intrinsic event class. The value is in working with the generic WMI class.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2009/01/21/how-can-i-back-up-a-database-s-data-folder-while-the-database-is-running.aspx" target="_blank"&gt;How Can I Back Up a Database&amp;rsquo;s Data Folder While the Database Is Running?&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;This is an interesting post. It also uses a generic WMI event class, &lt;b&gt;_InstanceModificationEvent&lt;/b&gt;. The query is very useful, as is the discussion of the technique actually involved. The technique illustrates doing something that is not easily accomplished via other methods. The script could be simplified by updating to Windows PowerShell&amp;nbsp;2.0, but this blog is still good.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;h3&gt;Overview of writing an event driven script&lt;/h3&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2009/11/02/hey-scripting-guy-november-1-2009.aspx" target="_blank"&gt;How Can I Write an Event-Driven Script?&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;This post uses the &lt;b&gt;Register-WmiEvent&lt;/b&gt; cmdlet that was introduced in Windows PowerShell&amp;nbsp;2.0. This blog illustrates the steps involved in writing an event-driven script, and it is a great introduction to the topic.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2010/04/13/hey-scripting-guy-april-13-2010.aspx" target="_blank"&gt;How Can I Be Notified When a USB Drive Is Plugged into My Computer?&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;This short overview talks about the difference between intrinsic and generic WMI event classes. It illustrates using the &lt;b&gt;Register-WmiEvent&lt;/b&gt; and the &lt;b&gt;Get-Event&lt;/b&gt; Windows PowerShell cmdlets.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2010/04/14/hey-scripting-guy-april-14-2010.aspx" target="_blank"&gt;How Can I Retrieve Information About Laptops Changing from Full Power to Minimal Power Usage?&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;This blog follows the prior blog in a series, and it goes into more information about WMI events. The background information presented in the blog is useful for understanding the application of the techniques illustrated in the blog. The use of WMI eventing to monitor changes in power states is also a very good application of the technology.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;h3&gt;Specific application useful examples&lt;/h3&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;There are several Hey, Scripting Guy! Blog posts that illustrate using temporary event consumers in a variety of ways. These are intended as &amp;ldquo;food for thought&amp;rdquo; types of blogs, and not as specific monitoring solutions.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2009/11/03/hey-scripting-guy-november-3-2009.aspx" target="_blank"&gt;Can I Be Informed When a Portable Drive Is Added by My Computer?&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;This blog illustrates using the &lt;b&gt;Register-WmiEvent&lt;/b&gt; and the &lt;b&gt;Get_Event&lt;/b&gt; cmdlets. A temporary event consumer is created that monitors for plugging a USB drive into a computer. The script could be the basis of a more involved script, and the action you decide to take when the drive is inserted or removed is up to you. There are a number of great tips and tricks in this blog.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2009/11/04/hey-scripting-guy-november-4-2009.aspx" target="_blank"&gt;Can I Format a Portable Drive When It Is Inserted Into a Computer?&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;This blog builds on the previous blog, and it contains a number of extremely useful functions, such as the &lt;b&gt;Test-IsAdministrator&lt;/b&gt; function that I wrote for the Windows&amp;nbsp;7 Resource Kit (admin rights are required to format a drive). The script associated with this blog is quite extensive, and this blog illustrates a number of tips and tricks for working with temporary WMI event consumers.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2009/11/05/hey-scripting-guy-november-5-2009.aspx" target="_blank"&gt;Can I Start an Event Based on When a Registry Value Is Changed?&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;This blog uses the &lt;b&gt;RegistryValueChangeEvent&lt;/b&gt; WMI class to monitor a specific registry key and generate an event when a change takes place. The script also uses the &lt;b&gt;Register-WmiEvent&lt;/b&gt; and the &lt;b&gt;Wait-Event&lt;/b&gt; cmdlets that were introduced in Windows PowerShell&amp;nbsp;2.0. This is a great technique when you want to take an action when something in the registry changes. There is also a discussion about the other &lt;b&gt;RegistryEvent&lt;/b&gt; WMI classes, and it includes a helpful table to let you know which class you might need.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2010/04/15/hey-scripting-guy-april-15-2010.aspx" target="_blank"&gt;Can I Use WMI to Determine When Someone Logs Off a User or Shuts Down a Server?&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;This blog uses the &lt;b&gt;Register-WmiEvent&lt;/b&gt; cmdlet and the &lt;b&gt;Get-Event&lt;/b&gt; cmdlet to monitor the &lt;b&gt;Win32_ShutdownEvent&lt;/b&gt; intrinsic WMI class to provide notification. This is an interesting idea that can provide food for thought for some very useful applications.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;h2&gt;WMI permanent event consumers&lt;/h2&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I have five blogs that discuss working with WMI permanent event consumers. The first two are foundational in that I discuss the technology and the parts that are involved in working with event consumers. The last three blogs were written by Trevor Sullivan, and he discusses a module he wrote to make working with permanent event consumers a bit easier.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2010/12/06/learn-how-to-use-vbscript-to-create-permanent-wmi-events.aspx" target="_blank"&gt;Learn How to Use VBScript to Create Permanent WMI Events&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;This blog began the Permanent Event Consumer Week. It is foundational, and should be read because it explains the different pieces: the consumer, the event filter, and the filter to consumer binding. You should review this blog carefully if you want to work with WMI permanent event consumers.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2010/12/07/use-powershell-to-monitor-and-respond-to-events-on-your-server.aspx" target="_blank"&gt;Use PowerShell to Monitor and Respond to Events on Your Server&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;The series continues by discussing the different consumer classes. The blog has two extremely powerful scripts: the first script creates a permanent event consumer, the second script reports on permanent event consumers. The blog also illustrates how to remove the event consumers and event filters. The cmdlets used in this blog are &lt;b&gt;Remove-WmiObject&lt;/b&gt; and &lt;b&gt;Set-WmiInstance&lt;/b&gt;.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2010/12/08/use-a-powershell-module-to-work-with-wmi-permanent-events.aspx" target="_blank"&gt;Use a PowerShell Module to Work with WMI Permanent Events&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;This blog is written by Trevor Sullivan, and it discusses using his PowerEvents module.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2010/12/09/use-the-powershell-wmi-event-module-to-quickly-monitor-events.aspx" target="_blank"&gt;Use the PowerShell WMI Module to Quickly Monitor Events&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;This blog is the second part of using the PowerEvents module.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2011/08/16/monitor-and-respond-to-windows-power-events-with-powershell.aspx" target="_blank"&gt;Monitor and Respond to Windows Power Events with PowerShell&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;In this blog, Trevor talks about using the &lt;b&gt;Win32_PowermanagementEvent&lt;/b&gt; intrinsic eventing class and creating a permanent event consumer by using the PowerEvents module. If you do not have the module, you could modify the script in &lt;i&gt;Use PowerShell to Monitor and Respond to Events on Your Server&lt;/i&gt;, but it will be more work, and the PowerEvents module works great.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;These are the main Hey, Scripting Guy! Blog posts that discuss working with WMI events. I did not review a few others (for examples entries from the Scripting Games) here. To see all the blogs that come up about WMI events, simply &lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/tags/windows+powershell/events+and+monitoring/" target="_blank"&gt;click this tag cloud&lt;/a&gt;. Join me tomorrow when I will talk about more Windows PowerShell coolness.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I invite you to follow me on &lt;a href="http://bit.ly/scriptingguystwitter" target="_blank"&gt;Twitter&lt;/a&gt; and &lt;a href="http://bit.ly/scriptingguysfacebook" target="_blank"&gt;Facebook&lt;/a&gt;. If you have any questions, send email to me at &lt;a href="mailto:scripter@microsoft.com" target="_blank"&gt;scripter@microsoft.com&lt;/a&gt;, or post your questions on the &lt;a href="http://bit.ly/scriptingforum" target="_blank"&gt;Official Scripting Guys Forum&lt;/a&gt;. See you tomorrow. Until then, peace.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Ed Wilson, Microsoft Scripting Guy&lt;/b&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3502308" width="1" height="1" alt="" /&gt;</description></item><item><title>Monitor and Respond to Windows Power Events with PowerShell</title><link>http://powershell.com/cs/blogs/hey-scriptingguy/archive/2011/08/16/monitor-and-respond-to-windows-power-events-with-powershell.aspx</link><pubDate>Tue, 16 Aug 2011 05:00:00 GMT</pubDate><guid isPermaLink="false">f421715f-7aba-45f0-8a8d-44de5318a3a7:11856</guid><dc:creator>Anonymous</dc:creator><description>&lt;p&gt;&lt;strong&gt;Summary&lt;/strong&gt;: Guest Blogger Trevor Sullivan shows how to monitor and to respond to Windows Power events using Windows PowerShell.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Microsoft Scripting Guy Ed Wilson here. Today&amp;rsquo;s guest blogger is Trevor Sullivan, and he has a fascinating article about responding to power management events. First, a little bit about Trevor.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/1460.HSG_2D00_8_2D00_16_2D00_11_2D00_1.jpg"&gt;&lt;img style="border:0px;" title="Photo of Trevor Sullivan" alt="Photo of Trevor Sullivan" src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/1460.HSG_2D00_8_2D00_16_2D00_11_2D00_1.jpg" width="362" height="474" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Trevor Sullivan is a passionate, experienced, and Microsoft-certified IT pro with more than seven years in the industry. Although he is interested in nearly all Microsoft technologies, his primary specialties include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Design, implementation, and troubleshooting of Microsoft System Center Configuration Manager 2007.&lt;/li&gt;
&lt;li&gt;Automation using Windows PowerShell, Windows Management Instrumentation (WMI), Active Directory Services Interface (ADSI), and the Microsoft .NET Framework.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Trevor is a Microsoft-recognized Community Contributor (MCC) and is active in several online communities:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Microsoft TechNet and MSDN discussion forums (he is a moderator on the TechNet Scripting Guys forum).&lt;/li&gt;
&lt;li&gt;&lt;a href="http://myitforum.com/"&gt;myITforum&lt;/a&gt; mailing lists.&lt;/li&gt;
&lt;li&gt;Blogging on WordPress (&lt;a href="http://trevorsullivan.net/"&gt;http://trevorsullivan.net&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;Twitter (&lt;a href="http://twitter.com/pcgeek86"&gt;@pcgeek86&lt;/a&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;One of his major achievements is the development and public release of an open-source Windows PowerShell module called &lt;a title="PowerEvents  " href="http://powerevents.codeplex.com"&gt;PowerEvents&lt;/a&gt;. During his personal time, he enjoys studying theology, spending time with his girlfriend, being outdoors, shooting photos, playing video games, testing software, and learning about all sorts of new things.&lt;/p&gt;
&lt;p&gt;Take it away, Trevor!&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:small;"&gt;&lt;b&gt;Introduction&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Oftentimes, people want to be able to respond to events automatically on their computers: &amp;ldquo;When &amp;lt;X&amp;gt; happens, I want &amp;lt;Y&amp;gt; to happen in response.&amp;rdquo; An example of this might be: &amp;ldquo;If SomeProcess.exe exceeds 50 percent processor utilization for 60 seconds, kill it&lt;i&gt;.&lt;/i&gt;&amp;rdquo; Usually this would require some custom systems monitoring software, but what if I told you that your computer had this functionality built into it already? That&amp;rsquo;s right, little known to most people is the WMI background service, which provides a robust eventing and event response model.&lt;/p&gt;
&lt;p&gt;Although power management hasn&amp;rsquo;t always been a highlight of the Microsoft Windows operating system (OS), it&amp;rsquo;s certainly come a long way in Windows 7 and is now quite robust. Sleeping and hibernating in Windows 7 are both quite fast, and resuming from both states is likewise very quick. But what if you want to do something when your computer wakes up? Though this may not be a terribly common scenario, sometimes people have the need to subscribe to this event and perform an action in response to it.&lt;/p&gt;
&lt;p&gt;In the remainder of this article, we will take a look at how to subscribe for system-level power management events, and how to respond to them. We will be working with the following technologies:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://www.scriptingguys.com/learnpowershell"&gt;Windows PowerShell&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://powerevents.codeplex.com/"&gt;PowerEvents&lt;/a&gt; for Windows PowerShell&lt;/li&gt;
&lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/aa394582(v=vs.85).aspx"&gt;Windows Management Instrumentation&lt;/a&gt; (WMI)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;span style="font-size:small;"&gt;&lt;b&gt;&lt;br /&gt;WMI Power Management events&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Microsoft has built a robust power management provider into &lt;a href="http://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;cd=1&amp;amp;ved=0CHYQFjAA&amp;amp;url=http%3A%2F%2Fwindows.microsoft.com%2Fen-US%2Fwindows7%2Fproducts%2Fhome&amp;amp;ei=sooYTqakM6OtsAL1mpjCBw&amp;amp;usg=AFQjCNHe-GPYcoB9fJE-wztciRkk9VHnEQ"&gt;Windows 7&lt;/a&gt;, and thankfully for us, they have exposed its functionality via the &lt;a href="http://msdn.microsoft.com/en-us/library/aa394582(v=vs.85).aspx"&gt;WMI&lt;/a&gt; service. WMI provides a standards-based interface in the operating system and applications that extend it. Although WMI has suffered from reliability and performance problems in the past&amp;mdash;primarily on Windows XP&amp;mdash;modern-day hardware combined with the newest Windows 7 operating system is quite reliable. Microsoft has resolved a lot of WMI bugs such that it is a very dependable service.&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:small;"&gt;&lt;b&gt;&lt;br /&gt;Power Management WMI Provider&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;All WMI providers (extensions to WMI) are registered in a particular WMI namespace under the &lt;b&gt;__Win32Provider&lt;/b&gt; class. We can ensure that the Windows Power Management provider is registered by running this WMI query from Windows PowerShell:&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;@(&lt;b&gt;Get-WmiObject&lt;/b&gt; &lt;i&gt;-Namespace&lt;/i&gt; root\cimv2 &lt;i&gt;-Query&lt;/i&gt; &amp;quot;select * from __Win32Provider where Name = &amp;#39;MS_Power_Management_Event_Provider&amp;#39;&amp;quot;).Count&lt;/p&gt;
&lt;p&gt;If this query returns a result of &amp;ldquo;1,&amp;rdquo; we know that the provider is registered.&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:small;"&gt;&lt;b&gt;&lt;br /&gt;Win32_PowerManagementEvent class&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The power management provider exposes a single WMI class called &lt;b&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/aa394362(v=vs.85).aspx"&gt;Win32_PowerManagementEvent&lt;/a&gt;&lt;/b&gt;, which is an extrinsic event class. Extrinsic event classes differ from intrinsic event classes in that the events they provide come from an external provider (the Power Management WMI provider), rather than them representing a change to a WMI object.&lt;/p&gt;
&lt;p&gt;The &lt;b&gt;Win32_PowerManagementEvent&lt;/b&gt; class only has one property that we really care about, which is the &lt;b&gt;EventType&lt;/b&gt; property. The possible values for this property are:&lt;/p&gt;
&lt;table style="width:209px;" cellspacing="0" cellpadding="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p&gt;&lt;b&gt;&lt;span style="text-decoration:underline;"&gt;Value&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p&gt;&lt;b&gt;&lt;span style="text-decoration:underline;"&gt;Meaning&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p&gt;4&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p&gt;Entering suspend&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p&gt;7&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p&gt;Resume from suspend&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p&gt;10&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p&gt;Power status change&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p&gt;11&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p&gt;OEM event&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p&gt;18&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p&gt;Resume automatic&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;As you might gather, we are interested in events that have a value of &amp;quot;7,&amp;quot; which represents a system resume.&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:small;"&gt;&lt;b&gt;&lt;br /&gt;Example Scenario&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example scenario, we are going to take a look at how to restart a Windows service when the system resumes. Specifically, I recently noticed that the &lt;a href="http://code.google.com/p/ps3mediaserver/"&gt;PS3 Media Server&lt;/a&gt; software has an &lt;a href="http://code.google.com/p/ps3mediaserver/issues/detail?id=163&amp;amp;colspec=ID%20Type%20Status%20Priority%20Milestone%20Owner%20Summary%20Stars"&gt;issue with power management&lt;/a&gt; in that it does not listen for connections upon system resume from Standby/Hibernate. This has &lt;a href="http://code.google.com/p/ps3mediaserver/issues/detail?id=163#c18"&gt;reportedly been a problem&lt;/a&gt; with Windows 7 Ultimate Edition 64-bit.&lt;/p&gt;
&lt;p&gt;To work around this issue, we&amp;rsquo;ll look at how to restart the PS3 Media Server service each time the computer resumes from a low power state.&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:small;"&gt;&lt;b&gt;&lt;br /&gt;Using PowerEvents&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The use of the PowerEvents model follows a three-step process:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Create WMI event filter using WQL.&lt;/li&gt;
&lt;li&gt;Create an event consumer (response to the event occurrence).&lt;/li&gt;
&lt;li&gt;Create a WMI binding between the event filter and the event consumer.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;We will cover these three steps individually below.&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:small;"&gt;&lt;b&gt;&lt;br /&gt;WQL event filter&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;First, we need to build an WMI event filter using the WMI Query Language (WQL). WQL is similar to Structured Query Language (SQL), but is much more limited in scope. WQL does not support &lt;b&gt;INSERT&lt;/b&gt;, &lt;b&gt;UPDATE&lt;/b&gt;, or &lt;b&gt;DELETE&lt;/b&gt; statements; it only supports &lt;b&gt;SELECT&lt;/b&gt; queries. We&amp;rsquo;re going to follow the event query template:&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;select * from &amp;lt;&lt;i&gt;WmiClass&lt;/i&gt;&amp;gt; WITHIN &amp;lt;&lt;i&gt;PollInterval&lt;/i&gt;&amp;gt; where &amp;lt;&lt;i&gt;Criteria&lt;/i&gt;&amp;gt;&lt;/p&gt;
&lt;p&gt;In this case, we&amp;rsquo;re going to use the following values for our event query:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;i&gt;WmiClass&lt;/i&gt; = Win32_PowerManagementEvent&lt;/li&gt;
&lt;li&gt;&lt;i&gt;PollInterval&lt;/i&gt; = 5&lt;/li&gt;
&lt;li&gt;&lt;i&gt;Criteria&lt;/i&gt; = &amp;quot;EventType = 7&amp;quot;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Our resulting query will look like this:&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;select * from Win32_PowerManagementEvent WITHIN 5 where EventType = 7&lt;/p&gt;
&lt;p&gt;The command we&amp;rsquo;ll use to create our WMI event filter using the PowerEvents module for Windows PowerShell looks like this:&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;$Filter = New-WmiEventFilter &lt;i&gt;-Name&lt;/i&gt; SystemResumed &lt;i&gt;-Query&lt;/i&gt; &amp;quot;select * from Win32_PowerManagementEvent where EventType = 7&amp;quot;&lt;/p&gt;
&lt;p&gt;We store the filter object in a Windows PowerShell variable for later use in the event binding.&lt;/p&gt;
&lt;p&gt;More information about WMI event queries can be found in the PowerEvents documentation. The PDF is located in the Documentation folder of the &lt;a href="http://powerevents.codeplex.com/"&gt;PowerEvents&lt;/a&gt; download. This document includes information about how to test your WMI event query using the wbemtest.exe utility, before creating the permanent event registration to reduce troubleshooting hassle.&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:small;"&gt;&lt;b&gt;&lt;br /&gt;Event consumer&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Now that we have created (and tested, right?) the event filter, we need to create an event consumer. In this example, we&amp;rsquo;ll use a Windows PowerShell script to stop and start the PS3 Media Server service (short name: &lt;b&gt;PS3 Media Server&lt;/b&gt;). The script itself contains this code:&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;$ServiceName = $args[0]&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;b&gt;Add-Content&lt;/b&gt; &lt;i&gt;-Path&lt;/i&gt; &amp;#39;c:\Restart Service.log&amp;#39; &lt;i&gt;-Value&lt;/i&gt; &amp;quot;Service name is: $ServiceName&amp;quot;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;$Service = @(&lt;b&gt;Get-WmiObject&lt;/b&gt; &lt;i&gt;-Namespace&lt;/i&gt; root\cimv2 &lt;i&gt;-Class&lt;/i&gt; Win32_Service &lt;i&gt;-Filter&lt;/i&gt; &amp;quot;Name = &amp;#39;$ServiceName&amp;#39;&amp;quot;)&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;b&gt;Add-Content&lt;/b&gt; &lt;i&gt;-Path&lt;/i&gt; &amp;#39;C:\Restart Service.log&amp;#39; &lt;i&gt;-Value&lt;/i&gt; &amp;quot;Found $($Service.Count) instances of &amp;#39;$ServiceName&amp;#39; service&amp;quot;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;$Result = $Service[0].StopService()&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;b&gt;Add-Content&lt;/b&gt; &lt;i&gt;-Path&lt;/i&gt; &amp;#39;c:\Restart Service.log&amp;#39; &lt;i&gt;-Value&lt;/i&gt; &amp;quot;Stopped service with result: $($Result.ReturnValue)&amp;quot;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;b&gt;Start-Sleep&lt;/b&gt; 4&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;$Result = $Service[0].StartService()&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;b&gt;Add-Content&lt;/b&gt; &lt;i&gt;-Path&lt;/i&gt; &amp;#39;c:\Restart Service.log&amp;#39; &lt;i&gt;-Value&lt;/i&gt; &amp;quot;Started service with result: $($Result.ReturnValue)&amp;quot;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;b&gt;Add-Content&lt;/b&gt; &lt;i&gt;-Path&lt;/i&gt; &amp;#39;c:\Restart Service.log&amp;#39; &lt;i&gt;-Value&lt;/i&gt; &amp;quot;Exiting restart service script&amp;quot;&lt;/p&gt;
&lt;p&gt;Save this code in a file called c:\windows\temp\Restart Windows Service.ps1.&lt;/p&gt;
&lt;p&gt;To create the event consumer object in WMI, we&amp;rsquo;ll use the following command:&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;$Consumer = New-WmiEventConsumer &lt;i&gt;-Verbose&lt;/i&gt; &lt;i&gt;-Name&lt;/i&gt; SystemResumedRestartService &lt;i&gt;-ConsumerType&lt;/i&gt; CommandLine &lt;i&gt;-CommandLineTemplate&lt;/i&gt; &amp;quot;powershell.exe -command `&amp;quot;. &amp;#39;$($env:WinDir)\temp\Restart Windows Service.ps1&amp;#39; &amp;#39;PS3 Media Server&amp;#39;`&amp;quot;&amp;quot;&lt;/p&gt;
&lt;p&gt;This command creates a command-line consumer &amp;mdash; that is to say, we want to call a command-line utility in response to the event that occurs. We give it a friendly name so that we know what it runs in response to, and what it does in response to the event: &lt;b&gt;SystemResumedRestartService.&lt;/b&gt; Then we use the &lt;i&gt;CommandLineTemplate&lt;/i&gt; parameter to specify the command line we want to execute in response to the event. In this case, we&amp;rsquo;re calling Windows PowerShell and passing it our script file via the &lt;i&gt;-command&lt;/i&gt; switch along with an argument to the script file. We use script arguments to make our script dynamic. All we have to do to change the service that gets restarted is change the parameter that we&amp;rsquo;re passing to it. We don&amp;rsquo;t have to touch the script itself at all.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;b&gt;Important&lt;/b&gt;&amp;nbsp; &amp;nbsp;Make sure you have configured your Windows PowerShell execution policy to allow execution of script files; otherwise, the event consumer will fail. Run Windows PowerShell with your administrative token and use this command: &lt;b&gt;Set-ExecutionPolicy Unrestricted&lt;/b&gt;.&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:small;"&gt;&lt;b&gt;&lt;br /&gt;WMI event binding&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Finally, now that we have created our event filter and event consumer, all we have to do to initiate the flow of events is bind them together. We&amp;rsquo;ve got the filter and consumer stored in variables called &lt;b&gt;$Filter&lt;/b&gt; and &lt;b&gt;$Consumer&lt;/b&gt;, so all we have to do is call this command:&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;New-WmiFilterToConsumerBinding &lt;i&gt;-Filter&lt;/i&gt; $Filter &lt;i&gt;-Consumer&lt;/i&gt; $Consumer&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:small;"&gt;&lt;b&gt;&lt;br /&gt;Testing&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;And that&amp;rsquo;s it! We&amp;rsquo;re done. Now that all the WMI objects have been created, all we have to do is suspend and resume our workstation to test the process. After the system is restarted, we should see a c:\Restart Service.log file created. Check this log to ensure that the service you specified in the event consumer command-line was properly stopped and started.&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:small;"&gt;&lt;b&gt;&lt;br /&gt;Conclusion&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;This article has demonstrated the use of the &lt;a href="http://powerevents.codeplex.com/"&gt;PowerEvents&lt;/a&gt; module for Windows PowerShell to create an event listener (filter)/responder (consumer) for wake-from-low-power-state events. Although this particular example restarts a Windows service in response to such an event, you can use your creativity to come up with other tasks you might need to fire off at the same occurrence.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;b&gt;Note&lt;/b&gt;&amp;nbsp; &amp;nbsp;For more information about working with permanent and temporary WMI events, see &lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/tags/windows+powershell/wmi/events+and+monitoring/"&gt;&amp;nbsp;this collection of Hey, Scripting Guy! Blog posts&lt;/a&gt;. This collection includes a post about using &lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2010/12/06/learn-how-to-use-vbscript-to-create-permanent-wmi-events.aspx"&gt;VBScript to create permanent WMI events&lt;/a&gt;. This post is important because it discusses the basics of permanent WMI events. Next, I talk about using Windows PowerShell &lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2010/12/07/use-powershell-to-monitor-and-respond-to-events-on-your-server.aspx"&gt;to monitor and to respond to events on the server&lt;/a&gt;. This post continues the discussion about permanent WMI events. This is followed by the first of two articles from Trevor that talk about his &lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2010/12/08/use-a-powershell-module-to-work-with-wmi-permanent-events.aspx"&gt;Windows PowerShell module to work with WMI permanent events&lt;/a&gt;. The second Trevor article in the series talks about &lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2010/12/09/use-the-powershell-wmi-event-module-to-quickly-monitor-events.aspx"&gt;using the Windows PowerShell WMI event module to quickly monitor events&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Thanks Trevor for an interesting article, and for writing your Windows PowerShell module for working with WMI Permanent Events.&lt;/p&gt;
&lt;p&gt;I invite you to follow me on &lt;a href="http://bit.ly/scriptingguystwitter" target="_blank"&gt;Twitter&lt;/a&gt; and &lt;a href="http://bit.ly/scriptingguysfacebook"&gt;Facebook&lt;/a&gt;. If you have any questions, send email to me at &lt;a href="mailto:scripter@microsoft.com" target="_blank"&gt;scripter@microsoft.com&lt;/a&gt;, or post your questions on the &lt;a href="http://bit.ly/scriptingforum" target="_blank"&gt;Official Scripting Guys Forum&lt;/a&gt;. See you tomorrow. Until then, peace.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Ed Wilson, Microsoft Scripting Guy&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3445910" width="1" height="1" alt="" /&gt;</description></item><item><title>Use PowerShell to Troubleshoot Software Installation</title><link>http://powershell.com/cs/blogs/hey-scriptingguy/archive/2011/07/13/use-powershell-to-troubleshoot-software-installation.aspx</link><pubDate>Wed, 13 Jul 2011 05:00:00 GMT</pubDate><guid isPermaLink="false">f421715f-7aba-45f0-8a8d-44de5318a3a7:11367</guid><dc:creator>Anonymous</dc:creator><description>&lt;p&gt;&lt;strong&gt;Summary&lt;/strong&gt;: Use Windows PowerShell to troubleshoot software installation.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;img title="Hey, Scripting Guy! Question" border="0" alt="Hey, Scripting Guy! Question" align="left" src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/qanda/q-sm.jpg" width="34" height="34" /&gt;Hey, Scripting Guy! I am having a problem troubleshooting the installation of an MSI package. I am using Group Policy to deploy the MSI package, and on some computers, it seems to work, but on other computers it fails. After having read your most recent series of articles about troubleshooting Windows, I thought I could use a trace log, but after spending more than an hour trying to click all of those little folders (why is there no search on the log name?), I could not find a trace log that seemed to make sense. Anyway, I guess I am asking you how to troubleshoot remote installation of a MSI package. Hope you can help.&lt;/p&gt;
&lt;p&gt;&amp;mdash;LT&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;img title="Hey, Scripting Guy! Answer" border="0" alt="Hey, Scripting Guy! Answer" align="left" src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/qanda/a-sm.jpg" width="34" height="34" /&gt;Hello LT,&lt;/p&gt;
&lt;p&gt;Microsoft Scripting Guy Ed Wilson here. This morning, the Scripting Wife and I got to do something we have been looking forward to for nearly two months. When we were at Tech∙Ed 2011 in Atlanta, we got to meet a couple of scripters that work for a company that has a headquarters in the Charlotte area. We exchanged email, and this morning the Scripting Wife and I went to their office and made a three-hour &amp;ldquo;Introduction to Windows PowerShell&amp;rdquo; presentation. It was a lot of fun. One of the questions they had was related to troubleshooting remote systems.&lt;/p&gt;
&lt;p&gt;LT, you are not alone in your queries. In addition to the customer I was talking to this morning, there was also &lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2011/07/11/use-dates-types-to-filter-event-trace-logs-in-powershell.aspx#comments"&gt;a comment on Monday&amp;rsquo;s blog post&lt;/a&gt; from &lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2011/05/08/lessons-learned-from-the-2011-scripting-games.aspx"&gt;Klaus Schulte&lt;/a&gt; (winner of the Beginner division of the 2011 Scripting Games) asking about troubleshooting installation packages.&lt;/p&gt;
&lt;p&gt;LT, there is a search for trace logs. It is called Windows PowerShell. I have given up attempting to navigate through the hundreds of logs in all the different folders (there are 492 logs on my Windows 7 Ultimate workstation). Instead, if I am searching for a log related to something, I use Windows PowerShell.&lt;/p&gt;
&lt;p&gt;In &lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2011/07/09/use-powershell-to-troubleshoot-windows.aspx"&gt;Saturday&amp;rsquo;s Weekend Scripter article&lt;/a&gt;, I talked about working with Event Tracing for Windows (ETW) logs. I discussed how to enable and disable the logs, and how to use the &lt;b&gt;Get-WinEvent&lt;/b&gt; cmdlet to find and to read the trace. &lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2011/07/11/use-dates-types-to-filter-event-trace-logs-in-powershell.aspx"&gt;Monday, I continued the ETW discussion&lt;/a&gt; by examining the &lt;b&gt;datetime&lt;/b&gt; stamp that is generated for each event. &lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2011/07/12/parse-windows-trace-logs-by-using-powershell.aspx"&gt;Yesterday, I explored parsing the message property&lt;/a&gt; of the WMI Activity Trace log.&lt;/p&gt;
&lt;p&gt;If I do not supply a value to the &lt;i&gt;listlog &lt;/i&gt;parameter, an error appears. If I provide the name of a specific log, certain information about the log returns. If I use the &lt;b&gt;*&lt;/b&gt; wildcard character, information about every log on the system is displayed in the Windows PowerShell console. If I use a more comprehensive wildcard character pattern, I can limit the number of logs that return. An example of searching for trace logs that relate to &lt;b&gt;install&lt;/b&gt;&lt;i&gt; &lt;/i&gt;is shown here:&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\Windows\system32&amp;gt; Get-WinEvent -ListLog *install* -force | select logname&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;LogName&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;-------&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Microsoft-Windows-AxInstallService/Log&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Microsoft-Windows-WPD-ClassInstaller/Analytic&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Microsoft-Windows-WPD-ClassInstaller/Operational&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I can use the same technique to search for logs that relate to &lt;b&gt;msi&lt;/b&gt;&lt;i&gt;.&lt;/i&gt; In the following output, only one log relates to MSI, but it is associated with AppLocker. Therefore, it will not pick up any trace information from a generic MSI installation:&lt;i&gt;&lt;/i&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\Windows\system32&amp;gt; Get-WinEvent -ListLog *msi* -force | select logname&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;LogName&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;-------&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Microsoft-Windows-AppLocker/MSI and Script&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;At times, either I cannot find a trace log that I like, or I grow impatient from the search and then use one of my favorite tricks: query all the logs at once. Sure, it is inefficient, but if I am working locally, it is not a big deal. However, it can take a &lt;i&gt;very&lt;/i&gt; long time for the command to complete (on my workstation, it takes nearly four minutes to complete the command). The thing to keep in mind is that after the first portion of time&amp;mdash;it might be seconds or a minute or so depending on how much data you are returning and how recent your time filter is&amp;mdash;new information will no longer be returned to the screen. This is the time when the command is continuing to process log files, but there is no longer any data to return even though the filter coming after the command to return all the event logs is still working. The (inefficient) command to return log files that have a timestamp that occurs later than &amp;quot;7/11/11 10:35:08 pm&amp;quot; follows this paragraph. To make the information display a bit better, I send the information to a table. In the following command the &lt;b&gt;Get-WinEvent&lt;/b&gt; cmdlet returns all information from all log files. The returned entries are piped to the &lt;b&gt;Where-Object&lt;/b&gt; cmdlet (&lt;b&gt;?&lt;/b&gt; Is an alias for the &lt;b&gt;Where-Object&lt;/b&gt; cmdlet), which filters log entries after a specific time. The results are piped to the &lt;b&gt;Format-Table&lt;/b&gt; cmdlet (&lt;b&gt;ft&lt;/b&gt; is an alias for &lt;b&gt;Format-Table&lt;/b&gt; cmdlet), and three properties are selected. The command is shown here:&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Get-WinEvent | ? {$_.TimeCreated -gt &amp;quot;7/11/11 10:35:08 pm&amp;quot; } | ft logname, id, message&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The following figure illustrates running the command in the Windows PowerShell ISE and displays the associated output.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/0361.HSG_2D00_7_2D00_13_2D00_11_2D00_01.png"&gt;&lt;img style="border:0px;" title="Image of command running in Windows PowerShell ISE and associated output" alt="Image of command running in Windows PowerShell ISE and associated output" src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/0361.HSG_2D00_7_2D00_13_2D00_11_2D00_01.png" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;If I want to tighten up the output and create a more efficient use of the space in my output pane in the Windows PowerShell ISE, I can add the &lt;i&gt;autosize &lt;/i&gt;parameter to the &lt;b&gt;Format-Table&lt;/b&gt; cmdlet. In addition, I can display the entire message if I use the &lt;i&gt;wrap&lt;/i&gt; parameter. However, when I add these parameters to the previous command, it will take the most of the time (five minutes or so) the command runs before displaying output. This is because to calculate the amount of space to allocate for the columns, Windows PowerShell needs to look at all of the data. This reduces the efficiency of the streaming behavior that I took advantage of earlier. The revised command is shown here:&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Get-WinEvent | ? {$_.TimeCreated -gt &amp;quot;7/11/11 10:35:08 pm&amp;quot; } | ft logname, id, message -AutoSize &amp;ndash;wrap&lt;/p&gt;
&lt;p&gt;Clearly, a more efficient method of working with log files is required.&amp;nbsp;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;For more information about using the &lt;i&gt;FilterHashTable&lt;/i&gt; parameter, see &lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2011/01/24/use-powershell-cmdlet-to-filter-event-log-for-easy-parsing.aspx"&gt;Use a PowerShell Cmdlet to Filter Event Log for Easy Parsing&lt;/a&gt; and &lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2011/01/25/use-powershell-to-parse-saved-event-logs-for-errors.aspx"&gt;Use PowerShell to Parse Saved Event Logs for Errors&lt;/a&gt;. For more information about improving the performance of event log queries, see &lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2011/03/08/how-to-improve-the-performance-of-a-powershell-event-log-query.aspx"&gt;How to Improve the Performance of a PowerShell Event Log Query&lt;/a&gt;. For issues surrounding working remotely with Windows Vista and Windows XP event logs, refer to &lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2011/03/09/discover-how-to-filter-remote-event-log-entries-in-windows-vista.aspx"&gt;Discover How to Filter Remote Event Log Entries in Windows Vista&lt;/a&gt;.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The following table is copied from my &lt;a href="http://blogs.technet.com/b/heyscriptingguy/archive/2011/01/25/use-powershell-to-parse-saved-event-logs-for-errors.aspx"&gt;Use PowerShell to Parse Saved Event Logs for Errors&lt;/a&gt; Hey, Scripting Guy! Blog post from January 2011.&lt;/p&gt;
&lt;table cellspacing="0" cellpadding="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p class="TableHead"&gt;&lt;strong&gt;Event Log Viewer name&lt;/strong&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p class="TableHead"&gt;&lt;strong&gt;FilterHashTable parameter key name&lt;/strong&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;Log Name&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;LogName&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;Source&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;ProviderName&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;Event ID&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;ID&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;Level&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;Level&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;User&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;UserID&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;Op Code&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;*&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;Logged&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;*&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;Task Category&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;*&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;Keywords&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;*&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;Computer&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;N/A use &amp;ndash;&lt;i&gt;ComputerName&lt;/i&gt; parameter&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;Details&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p class="TableText"&gt;Data&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If I attempt to use my trick of using the &lt;b&gt;Get-Winevent&lt;/b&gt; cmdlet to list all log entries, and I use a &lt;b&gt;FilterHashTable&lt;/b&gt; to attempt to filter based on time at the &lt;b&gt;Get-Winevent&lt;/b&gt; cmdlet instead of on the other side of the pipeline, an error returns that states I must specify either a log, provider, or path. The command and associated error appear here:&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;PS C:\Windows\system32&amp;gt; Get-WinEvent -FilterHashTable @{StartTime = &amp;quot;7/11/11 10:35:08 pm&amp;quot;}&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Get-WinEvent : You must specify at least one Log, Provider or Path key-value pair.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;At line:1 char:13&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;+ Get-WinEvent &amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;nbsp; -FilterHashTable @{StartTime = &amp;quot;7/11/11 10:35:08 pm&amp;quot;}&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; + CategoryInfo&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; : InvalidArgument: (:) [Get-WinEvent], Exception&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; + FullyQualifiedErrorId : LogProviderOrPathNeeded,Microsoft.PowerShell.Commands.GetWinEventCommand&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I decide to modify the command to use a wildcard character for the &lt;b&gt;logname&lt;/b&gt; key for the &lt;b&gt;FilterHashTable&lt;/b&gt;&lt;i&gt;.&lt;/i&gt; The command works great and returns data nearly immediately:&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Get-WinEvent -FilterHashtable @{StartTime = &amp;quot;7/11/11 10:35:08 pm&amp;quot;; LogName = &amp;quot;*&amp;quot;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The nice thing about the above command is it returns information from multiple logs and multiple providers. This is useful, for example, when troubleshooting installation problems that may be unrelated to the actual installer. To check a specific installation, it may be useful to filter based on not only the time, but also on the provider. For MSI installed software, the provider is the &lt;b&gt;msiInstaller&lt;/b&gt; provider. The following command is broken at the pipe character for readability purposes. In reality it is a single command:&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Get-WinEvent -FilterHashtable @{StartTime = &amp;quot;7/11/11 10:35:08 pm&amp;quot;; ProviderName = &amp;quot;msiInstaller&amp;quot;} |&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;ft logname, id, message -AutoSize &amp;ndash;wrap&lt;/p&gt;
&lt;p&gt;The command and associated output appear in the following figure.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/6428.HSG_2D00_7_2D00_13_2D00_11_2D00_02.png"&gt;&lt;img style="border:0px;" title="Image of command and associated output" alt="Image of command and associated output" src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/6428.HSG_2D00_7_2D00_13_2D00_11_2D00_02.png" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;LT, that is all there is to using Windows PowerShell to look at Windows Installer logging. Troubleshooting Windows week will continue tomorrow.&lt;/p&gt;
&lt;p&gt;I invite you to follow me on &lt;a href="http://bit.ly/scriptingguystwitter" target="_blank"&gt;Twitter&lt;/a&gt; and &lt;a href="http://bit.ly/scriptingguysfacebook"&gt;Facebook&lt;/a&gt;. If you have any questions, send email to me at &lt;a href="mailto:scripter@microsoft.com" target="_blank"&gt;scripter@microsoft.com&lt;/a&gt;, or post your questions on the &lt;a href="http://bit.ly/scriptingforum" target="_blank"&gt;Official Scripting Guys Forum&lt;/a&gt;. See you tomorrow. Until then, peace.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Ed Wilson, Microsoft Scripting Guy&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3440927" width="1" height="1" alt="" /&gt;</description></item><item><title>Manage Event Subscriptions with PowerShell</title><link>http://powershell.com/cs/blogs/hey-scriptingguy/archive/2011/06/17/manage-event-subscriptions-with-powershell.aspx</link><pubDate>Fri, 17 Jun 2011 05:00:00 GMT</pubDate><guid isPermaLink="false">f421715f-7aba-45f0-8a8d-44de5318a3a7:10887</guid><dc:creator>Anonymous</dc:creator><description>&lt;p&gt;&lt;b&gt;Summary&lt;/b&gt;: Bruce Payette shows how to manage event subscriptions with Windows PowerShell.&lt;/p&gt;
&lt;p&gt;Microsoft Scripting Guy, Ed Wilson, here. I am really excited about the idea I had for this week, and I hope you will be too. I asked Candace Gillhoolley at Manning Press about posting some sample works from some of the Manning Press library of books. She responded enthusiastically and shared five samples that we will post this week. Today is part two of two parts from Bruce Payette and &lt;i&gt;Windows PowerShell in Action&lt;/i&gt;. See &lt;a target="_blank" href="http://blogs.technet.com/b/heyscriptingguy/archive/2011/06/16/use-asynchronous-event-handling-in-powershell.aspx"&gt;yesterday&amp;rsquo;s blog post&lt;/a&gt; for part 1. &lt;/p&gt;
&lt;h1&gt;&lt;a href="http://www.manning.com/payette2/"&gt;Windows PowerShell in Action, Second Edition&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18-metablogapi/5086.hsg_2D00_6_2D00_17_2D00_11_2D00_1_5F00_2E3845F1.jpg"&gt;&lt;img height="190" width="154" src="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18-metablogapi/4010.hsg_2D00_6_2D00_17_2D00_11_2D00_1_5F00_thumb_5F00_78BF83CB.jpg" alt="Image of book cover" border="0" title="Image of book cover" style="background-image:none;padding-left:0px;padding-right:0px;display:inline;padding-top:0px;border:0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;By Bruce Payette&lt;/p&gt;
&lt;p&gt;The key difference between event-based scripting and traditional procedural scripting is that, instead of an activity being executed as a result of an action in the script, a script (or at least a portion of it) is executed as a result of an action by the system.. In this article based on chapter 20 of &lt;a target="_blank" href="http://www.manning.com/payette2/"&gt;Windows PowerShell in Action, Second Edition&lt;/a&gt;, author Bruce Payette discusses asynchronous event-handling models in PowerShell. To save 35% on your next purchase use Promotional Code &lt;strong&gt;payette22035&lt;/strong&gt; when you check out at &lt;a target="_blank"&gt;www.manning.com&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18-metablogapi/2867.hsg_2D00_6_2D00_16_2D00_11_2D00_1_5F00_0FA45293.jpg"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18-metablogapi/5417.manning_5F00_534AC468.png"&gt;&lt;img height="26" width="154" src="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18-metablogapi/8535.manning_5F00_thumb_5F00_7A18DAA8.png" alt="manning" border="0" title="manning" style="background-image:none;padding-left:0px;padding-right:0px;display:inline;padding-top:0px;border-width:0px;" /&gt;&lt;/a&gt;&lt;/b&gt;&lt;/p&gt;
&lt;h2&gt;&lt;b&gt;Managing event subscriptions&lt;/b&gt;&lt;/h2&gt;
&lt;p&gt;In this section, you&amp;rsquo;ll see how to find your event subscriptions and how to remove them when you&amp;rsquo;re done with them. Being able to remove them is important because event subscriptions persist in the session until explicitly removed.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Listing event subscriptions&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Of course, before you can remove a subscription, you have to find it. Windows PowerShell provides the &lt;b&gt;Get-EventSubscriber&lt;/b&gt; to do this. Let&amp;rsquo;s use it to look at the subscription you registered in the previous section (see &lt;a target="_blank" href="http://blogs.technet.com/b/heyscriptingguy/archive/2011/06/16/use-asynchronous-event-handling-in-powershell.aspx"&gt;yesterday&amp;rsquo;s Hey! Scripting Guy blog&lt;/a&gt;):&lt;/p&gt;
&lt;blockquote&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;PS (1) &amp;gt; Get-EventSubscriber&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;SubscriptionId&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;: 1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;SourceObject&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;: System.Timers.Timer&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;EventName&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;: Elapsed&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;SourceIdentifier : fca4b869-8d5a-4f11-8d45-e84af30845f1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;Action&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;: System.Management.Automation.PSEventJob&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;HandlerDelegate&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;:&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;SupportEvent&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;: False&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;ForwardEvent&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;: False&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The &lt;b&gt;Get-EventSubscriber&lt;/b&gt; cmdlet returns &lt;b&gt;PSEventSubscriber&lt;/b&gt; objects, which have complete information about the registration: the object generating the event, the action to execute, and so on. There are a couple of interesting properties to note in this output. Because you didn&amp;rsquo;t give the subscription a friendly name using &lt;b&gt;-Source-Identifier&lt;/b&gt; when you created it, the &lt;b&gt;Register-ObjectEvent&lt;/b&gt; generated one for you. This autogenerated name is the string representation of a GUID, so you know it&amp;rsquo;s unique (but not very friendly). The other thing to notice is that the action shows up as a PowerShell &lt;b&gt;Job&lt;/b&gt; object. &lt;/p&gt;
&lt;p&gt;&lt;b&gt;Removing event subscriptions&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Now that you can list the event subscriptions, you can set about removing them. The cmdlet to do this is not &lt;b&gt;Unsubscribe-Event&lt;/b&gt; because unsubscribe isn&amp;rsquo;t on the approved verbs list and it&amp;rsquo;s not what you want to do anyway. You registered event subscriptions with &lt;b&gt;Register-ObjectEvent&lt;/b&gt;, so what you need to do is unregister the subscription, which you&amp;rsquo;ll do with Unregister-Event. The cmdlet noun in this case is Event, not &lt;b&gt;ObjectEvent&lt;/b&gt;, because you can use a common mechanism to unregister any kind of event. It&amp;rsquo;s only the registration part that varies. The rest of the eventing cmdlets remain the same.&lt;/p&gt;
&lt;p&gt;When you&amp;rsquo;re unregistering an event subscription, there are two ways of identifying the event to unregister: by the &lt;b&gt;SubscriptionId&lt;/b&gt; property or by the &lt;b&gt;Source-Identifier&lt;/b&gt;. The subscription ID is simply an integer that&amp;rsquo;s incremented each time an event subscription is created. Because you didn&amp;rsquo;t give your event registration a friendly name, you&amp;rsquo;ll use the &lt;b&gt;SubscriptionId&lt;/b&gt; to unregister it:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;PS (4) &amp;gt; Unregister-Event -SubscriptionId 1 -Verbose&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;VERBOSE: Performing operation &amp;quot;Unsubscribe&amp;quot; on Target &amp;quot;Event&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;subscription &amp;#39;timertest2&amp;#39;&amp;quot;.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;PS (5) &amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Note that you included the &lt;b&gt;-Verbose&lt;/b&gt; flag in this command so that you could see something happening. Let&amp;rsquo;s try running the command again &lt;/p&gt;
&lt;blockquote&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;PS (5) &amp;gt; Unregister-Event -SubscriptionId 1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;Unregister-Event : Event subscription with identifier &amp;#39;1&amp;#39; does not&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;exist.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;At line:1 char:17&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;+ Unregister-Event &amp;lt;&amp;lt;&amp;lt;&amp;lt; -SubscriptionId 1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;+ CategoryInfo : InvalidArgument: (:) [Unregister-Event&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;], ArgumentException&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;+ FullyQualifiedErrorId : INVALID_SUBSCRIPTION_IDENTIFIER,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;Microsoft.PowerShell.Commands.UnregisterEventCommand&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&amp;hellip;and it results in an error. The &lt;b&gt;Unregister-Event&lt;/b&gt; cmdlet is silent as long as nothing goes wrong. If something does go wrong, you get an error.&lt;/p&gt;
&lt;p&gt;We&amp;rsquo;ve covered the basics of creating and managing event subscriptions. But before the handlers for these events can do much useful work, they&amp;rsquo;ll need access to additional information. In the next section, you&amp;rsquo;ll write more sophisticated handlers and see how they can use the automatic variables provided by the eventing subsystem.&lt;/p&gt;
&lt;h2&gt;Asynchronous event handling with scriptblocks&lt;/h2&gt;
&lt;p&gt;In this section, we&amp;rsquo;ll look at the automatic variables and other features that PowerShell provides to allow scriptblocks to be used as effective event handlers.&lt;/p&gt;
&lt;h3&gt;Automatic variables in the event handler&lt;/h3&gt;
&lt;p&gt;In PowerShell eventing, the scriptblock that handles the event action has access to a number of variables that provide information about the event being handled:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;$event, $eventSubscriber, $sender, $sourceEventArgs, and $sourceArgs&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;These variables are described in table 2.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Table 2&lt;/b&gt; The automatic variables available in the event handler scriptblock &lt;/p&gt;
&lt;table cellpadding="0" cellspacing="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p&gt;Variable&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p&gt;Description&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p&gt;$event&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p&gt;This variable contains an object of type System.Management.Automation.PSEventArgs that represents the event that&amp;rsquo;s being handled. It allows you to access a wide variety of information about the event, as you&amp;rsquo;ll see in an example. The value of this variable is the same object that the Get-Event cmdlet returns.&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p&gt;$eventSubscriber&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p&gt;This variable contains the PSEventSubscriber object that represents the event subscriber of the event that&amp;rsquo;s being handled. The value of this variable is the same object that the Get-EventSubscriber cmdlet returns.&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p&gt;$sender&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p&gt;The value in this variable is the object that generated the event. This variable is a shortcut for $EventArgs.Sender.&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p&gt;$sourceEventArgs&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p&gt;Contains objects that represent the arguments of the event that&amp;rsquo;s being processed. This variable is a shortcut for $Event.SourceArgs.&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p&gt;$sourceArgs&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p&gt;Contains the values from $Event.SourceArgs. Like any other scriptblock, if there is a param statement, the parameters defined by that statement will be populated and $args will only contain leftover values for which there were no parameters.&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Let&amp;rsquo;s write a quick test event handler to see what&amp;rsquo;s in the object in &lt;b&gt;$Event&lt;/b&gt;. You&amp;rsquo;ll use the timer event again:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;PS (1) &amp;gt; $timer = New-Object System.Timers.Timer -Property @{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&amp;gt; Interval = 1000; Enabled = $true; AutoReset = $false }&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span style="color:#000000;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span style="font-family:Courier New;"&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In the event subscription action, you&amp;rsquo;ll display the contents of the event object:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;PS (2) &amp;gt; Register-ObjectEvent $timer Elapsed -Action {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&amp;gt; $Event | Out-Host&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&amp;gt; }&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;&lt;span&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;Id&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Name&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;State&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;HasMoreData&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Location&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;--&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;----&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;-----&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;-----------&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;--------&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;4&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;9e3586c3-534...&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;NotStarted False&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;You&amp;rsquo;ll start the timer to generate the event:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;PS (3) &amp;gt; $timer.Start()&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;PS (4) &amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;ComputerName&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;:&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;RunspaceId&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;: 373d0ee9-47a5-4ceb-89e5-61e6389d6838&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;EventIdentifier&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;: 7&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;Sender&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;: System.Timers.Timer&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;SourceEventArgs&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;: System.Timers.ElapsedEventArgs&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;SourceArgs&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;: {System.Timers.Timer, System.Timers.ElapsedEv&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;entArgs}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;SourceIdentifier : 9e3586c3-534b-465a-84b3-7404110a0f12&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;TimeGenerated&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;: 8/10/2010 12:17:40 PM&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;MessageData&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;:&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In this output, you see the properties on the &lt;b&gt;PSEvent&lt;/b&gt;&lt;b&gt; &lt;/b&gt;object that correspond to the variables listed in table 2. The &lt;b&gt;Timer&lt;/b&gt; object that generated the event is available through the Sender property on the object and the &lt;b&gt;$sender&lt;/b&gt; variable in the scriptblock.&lt;/p&gt;
&lt;p&gt;The &lt;b&gt;PSEvent&lt;/b&gt; object also includes context data about the event, including the time the event occurred, the event identifier, and the &lt;b&gt;RunspaceId&lt;/b&gt; this event is associated with. The &lt;b&gt;ComputerName&lt;/b&gt; property is blank because this is a local event, but, in the case of a remote event, it would contain the name of the computer where the event occurred. &lt;/p&gt;
&lt;h3&gt;Dynamic modules and event handler state&lt;/h3&gt;
&lt;p&gt;Because an event can fire at any time, you could never know what variables were in scope and this, in turn, could make it hard to know what state will exist when the action is executed. Instead, you want to be able to run the event handlers in a well-defined, isolated environment. This objective aligns with the design goals for PowerShell modules, so you can leverage this feature by creating a dynamic module for the action scriptblock. The eventing subsystem does this by calling the &lt;b&gt;New-BoundScriptBlockScriptblock()&lt;/b&gt; method to attach a dynamic module to the handler scriptblock.&lt;/p&gt;
&lt;p&gt;Beyond ensuring a coherent runtime environment for your event handler scriptblock, the module also allows it to have private state. This ability can be quite useful when you&amp;rsquo;re monitoring a system&amp;rsquo;s behavior over a period of time. The information can be accumulated privately and then processed once enough samples have been gathered. Let&amp;rsquo;s look at an example that illustrates how this state isolation works. The following is a trivial example where you maintain a count of the number of timer events fired. Once you reach a predetermined limit, the timer will be stopped. Let&amp;rsquo;s walk through the example. First, you create the &lt;b&gt;Timer&lt;/b&gt; object:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;PS (1) &amp;gt; $timer = New-Object System.Timers.Timer -Property @{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&amp;gt; Interval = 500; AutoReset = $true}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;&lt;span&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;As usual, subscribe to the &lt;strong&gt;Elapsed&lt;/strong&gt; event on the timer:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;PS (2) &amp;gt; Register-ObjectEvent -InputObject $timer `&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&amp;gt; -MessageData 5 `&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&amp;gt; -SourceIdentifier Stateful -EventName Elapsed -Action {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&amp;gt; $script:counter += 1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&amp;gt; Write-Host &amp;quot;Event counter is $counter&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&amp;gt; if ($counter -ge $Event.MessageData)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&amp;gt; {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&amp;gt; Write-Host &amp;quot;Stopping timer&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&amp;gt; $timer.Stop()&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&amp;gt; }&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&amp;gt; } &amp;gt; $null&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;&lt;span&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In the handler scriptblock for this event, you&amp;rsquo;re updating a script-scoped variable &lt;b&gt;$script:counter&lt;/b&gt;, which holds the number of times the event has fired. This variable will only be visible within the dynamic module associated with the event, thus preventing your &lt;b&gt;$counter&lt;/b&gt; from colliding with any other users of a variable called &lt;b&gt;$counter&lt;/b&gt;.&lt;/p&gt;
&lt;p&gt;After the variable is incremented, you print the event count and then check to see if the limit has been reached. Notice that you&amp;rsquo;re making use of the &lt;b&gt;-MessageData&lt;/b&gt; parameter to pass the limit to the event handler, which it retrieves from the &lt;b&gt;MessageData&lt;/b&gt; property on the &lt;b&gt;Event &lt;/b&gt;object. Now start the timer running to see it in action:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;PS (3) &amp;gt; $timer.Start()&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;PS (4) &amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;PS (5) &amp;gt; Event counter is 1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;Event counter is 2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;Event counter is 3&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;Event counter is 4&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;Event counter is 5&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;Stopping timer&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="line-height:normal;list-style-type:disc;margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;span style="font-family:Lucida Sans Typewriter;"&gt;&lt;span style="color:#000000;"&gt;PS (6) &amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;As intended, the timer message is displayed five times and then the timer is stopped. This example can easily be modified to, for example, monitor CPU usage or process working sets over a period of time.&lt;/p&gt;
&lt;h2&gt;Summary&lt;/h2&gt;
&lt;p&gt;There are two fundamental event types: synchronous and asynchronous. In synchronous events, all activities are synchronized so that no activity is ever interrupted. Asynchronous events execute in a nondeterministic order. To deal with these asynchronous events, PowerShell includes an eventing subsystem that takes care of synchronizing all operations. The core model for eventing in PowerShell is built around the idea of event subscriptions. There are three cmdlets for creating these subscriptions: &lt;b&gt;Get-ObjectEvent&lt;/b&gt;, &lt;b&gt;Get-WmiEvent&lt;/b&gt;, and &lt;b&gt;Get-EngineEvent&lt;/b&gt; for .NET, WMI, and PowerShell engine events respectively.&lt;/p&gt;
&lt;p&gt;As part of the event subscription, an action scriptblock may be specified that will be executed when the event is triggered. Context information for the event is made available to the scriptblock through the &lt;b&gt;$Event&lt;/b&gt; automatic variable. Some of the properties on the object in &lt;b&gt;$Event&lt;/b&gt; are also directly available through additional automatic variables.&lt;/p&gt;
&lt;p&gt;Thank you, Bruce. &lt;/p&gt;
&lt;p&gt;Well, this concludes an awesome week of guest writers from Manning Press. Join me tomorrow for the Weekend Scripter as I delve into my top ten favorite Windows PowerShell tricks.&lt;/p&gt;
&lt;p&gt;I invite you to follow me on &lt;a target="_blank" href="http://bit.ly/scriptingguystwitter"&gt;Twitter&lt;/a&gt; and &lt;a target="_blank" href="http://bit.ly/scriptingguysfacebook"&gt;Facebook&lt;/a&gt;. If you have any questions, send email to me at &lt;a href="mailto:scripter@microsoft.com"&gt;scripter@microsoft.com&lt;/a&gt;, or post your questions on the &lt;a target="_blank" href="http://bit.ly/scriptingforum"&gt;Official Scripting Guys Forum&lt;/a&gt;. See you tomorrow. Until then, peace.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Ed Wilson, Microsoft Scripting Guy&lt;/b&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3434353" width="1" height="1" alt="" /&gt;</description></item></channel></rss>