<?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!', '2012 Scripting Games', and 'Jonathan Medd'</title><link>http://powershell.com/cs/search/SearchResults.aspx?q=app:weblogs&amp;tag=Scripting+Guy!,2012+Scripting+Games,Jonathan+Medd&amp;orTags=0&amp;o=DateDescending</link><description>Search results for 'app:weblogs' matching tags 'Scripting Guy!', '2012 Scripting Games', and 'Jonathan Medd'</description><dc:language>en-US</dc:language><generator>CommunityServer 2008.5 (Build: 30929.2835)</generator><item><title>Expert Commentary: 2012 Scripting Games Beginner Event 9</title><link>http://powershell.com/cs/blogs/hey-scriptingguy/archive/2012/04/26/expert-commentary-2012-scripting-games-beginner-event-9.aspx</link><pubDate>Thu, 26 Apr 2012 05:00:00 GMT</pubDate><guid isPermaLink="false">f421715f-7aba-45f0-8a8d-44de5318a3a7:16213</guid><dc:creator>Anonymous</dc:creator><description>&lt;p&gt;&lt;b&gt;Summary&lt;/b&gt;: Windows PowerShell MVP, Jonathan Medd, provides expert commentary for 2012 Scripting Games Beginner Event 9.&lt;/p&gt;
&lt;p&gt;Microsoft Scripting Guy, Ed Wilson, is here. Jonathan Medd is the expert commentator for Beginner Event 9.&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/6165.hsg_2D00_4_2D00_26_2D00_12_2D00_beg_2D00_1.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/150x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/6165.hsg_2D00_4_2D00_26_2D00_12_2D00_beg_2D00_1.png" alt="Photo of Jonathan Medd" title="Photo of Jonathan Medd" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Jonathan has been working with IT infrastructure products since 1997. A few years ago, he discovered Windows PowerShell, and he now cohosts the&amp;nbsp;&lt;a href="http://get-scripting.blogspot.com/" target="_blank"&gt;Get-Scripting&lt;/a&gt;&amp;nbsp;PowerShell podcast. Since April 2010, he has been a Microsoft MVP for Windows PowerShell, and in 2011 was honored as a VMware vExpert. He is co-author of&amp;nbsp;&lt;a href="http://www.powerclibook.com/"&gt;VMware vSphere PowerCLI Reference&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Wherever possible, when you are working with the Windows PowerShell pipeline, it is beneficial to carry out filtering as close to the left side of the set of commands as possible. Consequently, it is always worth checking out the first cmdlet in the pipeline to see if it has built-in possibilities for filtering, rather than having to pipe the results of the cmdlet into &lt;b&gt;Where-Object&lt;/b&gt; and carrying out the filtering there.&lt;/p&gt;
&lt;p&gt;In Event 9, we are using the &lt;b&gt;Get-EventLog&lt;/b&gt; cmdlet, so it is worth examining the Help &lt;b&gt;for Get-EventlLog&lt;/b&gt; to see what, if any, filtering options are available.&lt;/p&gt;
&lt;p&gt;First of all though, I like to run a quick check on a cmdlet to see what parameters are potentially available. Rather than start reading through the Help, I use Shay Levy&amp;rsquo;s &lt;b&gt;Get-Parameter&lt;/b&gt; function, which you can get from his blog, &lt;a href="http://blogs.microsoft.co.il/blogs/scriptfanatic/archive/2010/05/03/get-parameter-learn-more-about-your-cmdlets.aspx" target="_blank"&gt;Get-Parameter - Learn more about your cmdlets&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For &lt;b&gt;Get-EventLog&lt;/b&gt;, it produces the following results:&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Get-Parameter Get-EventLog&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/3618.hsg_2D00_4_2D00_26_2D00_12_2D00_beg_2D00_2.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/3618.hsg_2D00_4_2D00_26_2D00_12_2D00_beg_2D00_2.png" alt="Image of command output" title="Image of command output" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;From this information, we can quickly see that there are some possibilities for filtering at the beginning of the pipeline to match the requirements of the event, that is, &lt;i&gt;-InstanceId&lt;/i&gt; and &lt;i&gt;-Source&lt;/i&gt;.&lt;/p&gt;
&lt;p&gt;If you want to find out more about these parameters you can use&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Get-Help Get-EventLog &amp;ndash;Detailed&lt;/p&gt;
&lt;p&gt;or&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Get-Help Get-EventLog &amp;ndash;Parameter InstanceID&lt;/p&gt;
&lt;p&gt;So our initial query of the event log makes use of the following parameters, and we don&amp;rsquo;t need to pipe the results into &lt;b&gt;Where-Object&lt;/b&gt; to carry out the filtering.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Get-EventLog -LogName Application -InstanceId 10001 -EntryType Information -Source Winsrv -Message &amp;quot;The following application attempted to veto the shutdown*&amp;quot;&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/3487.hsg_2D00_4_2D00_26_2D00_12_2D00_beg_2D00_3.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/3487.hsg_2D00_4_2D00_26_2D00_12_2D00_beg_2D00_3.png" alt="Image of command output" title="Image of command output" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;So it looks like the properties we need to work with for this event are &lt;b&gt;Time&lt;/b&gt; and &lt;b&gt;Message&lt;/b&gt;.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Get-EventLog -LogName Application -InstanceId 10001 -EntryType Information -Source Winsrv -Message &amp;quot;The following application attempted to veto the shutdown*&amp;rdquo; | Select Time,Message&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/1374.hsg_2D00_4_2D00_26_2D00_12_2D00_beg_2D00_4.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/1374.hsg_2D00_4_2D00_26_2D00_12_2D00_beg_2D00_4.png" alt="Image of command output" title="Image of command output" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;However, &lt;b&gt;Time&lt;/b&gt; is empty. Puzzling? Something I always remind students in my Windows PowerShell workshops is, &amp;ldquo;What you see is not necessarily what you get.&amp;rdquo; That is, the cmdlet designer has chosen to present the output of their cmdlet to the console in a certain way, and that may not necessarily match up with the object that has been returned by the cmdlet.&lt;/p&gt;
&lt;p&gt;So, we should pipe our &lt;b&gt;Get-EventLog&lt;/b&gt; query into the &lt;b&gt;Get-Member&lt;/b&gt; cmdlet to have a closer look.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Get-EventLog -LogName Application -InstanceId 10001 -EntryType Information -Source Winsrv -Message &amp;quot;The following application attempted to veto the shutdown*&amp;rdquo; | Get-Member&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/5545.hsg_2D00_4_2D00_26_2D00_12_2D00_beg_2D00_5.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/5545.hsg_2D00_4_2D00_26_2D00_12_2D00_beg_2D00_5.png" alt="Image of command output" title="Image of command output" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;We can see that we actually need to work with the &lt;b&gt;TimeGenerated&lt;/b&gt; property&amp;mdash;one called &lt;b&gt;Time&lt;/b&gt; does not exist.&lt;/p&gt;
&lt;p&gt;With the &lt;b&gt;Message&lt;/b&gt; property, we don&amp;rsquo;t need the whole message, just the part that contains the application that is vetoing the shutdown. For that, we can create a calculated property, that is, a custom property of our own. You can see how we do that in the following code. In the expression, we expand the &lt;b&gt;Message&lt;/b&gt; property, split it at the &lt;b&gt;&amp;lsquo;:&amp;rsquo;&lt;/b&gt; character, and use the second element in the array. (Arrays start at 0, so we need to use [1].)&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Get-EventLog -LogName Application -InstanceId 10001 -EntryType Information -Source Winsrv -Message &amp;quot;The following application attempted to veto the shutdown*&amp;quot;&amp;nbsp; | select TimeGenerated,@{n=&amp;#39;Application&amp;#39;;e={(($_ | Select -ExpandProperty Message) -split &amp;#39;:&amp;#39;)[1]}}&lt;/p&gt;
&lt;p&gt;This gives us the solution to the event 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/0044.hsg_2D00_4_2D00_26_2D00_12_2D00_beg_2D00_6.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/0044.hsg_2D00_4_2D00_26_2D00_12_2D00_beg_2D00_6.png" alt="Image of command output" title="Image of command output" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;While checking out this event, I needed to generate some events in the Application log to match those of the requirements. To do so, I used the &lt;b&gt;Write-EventLog&lt;/b&gt; cmdlet to create some (fake) matching entries.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Write-EventLog -LogName Application -Source Winsrv -Category 0 -EventId 10001 -Message &amp;quot;The following application attempted to veto the shutdown: BTTray.exe.&amp;quot;&lt;/p&gt;
&lt;p&gt;However, when I first ran this on my Windows&amp;nbsp;7 system, I received the following result.&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/0045.hsg_2D00_4_2D00_26_2D00_12_2D00_beg_2D00_7.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/0045.hsg_2D00_4_2D00_26_2D00_12_2D00_beg_2D00_7.png" alt="Image of command output" title="Image of command output" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;To have an event in the log of Source Winsrv, it first needs to be registered, and this does not appear to be so by default. You can use the following .NET code to determine if this is the case for any event source.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;[system.diagnostics.eventlog]::sourceexists(&amp;quot;Winsrv&amp;quot;)&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/0652.hsg_2D00_4_2D00_26_2D00_12_2D00_beg_2D00_8.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/0652.hsg_2D00_4_2D00_26_2D00_12_2D00_beg_2D00_8.png" alt="Image of command output" title="Image of command output" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;And use this code to create one for the Application log:&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;[system.diagnostics.eventlog]::CreateEventSource(&amp;quot;Winsrv&amp;quot;,&amp;#39;Application&amp;#39;)&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/8863.hsg_2D00_4_2D00_26_2D00_12_2D00_beg_2D00_9.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/8863.hsg_2D00_4_2D00_26_2D00_12_2D00_beg_2D00_9.png" alt="Image of command output" title="Image of command output" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Now I can create my required event log entry.&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/1323.hsg_2D00_4_2D00_26_2D00_12_2D00_beg_2D00_10.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/1323.hsg_2D00_4_2D00_26_2D00_12_2D00_beg_2D00_10.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;p&gt;&amp;nbsp;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/6011.hsg_2D00_4_2D00_26_2D00_12_2D00_beg_2D00_11.png"&gt;&lt;img src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/6011.hsg_2D00_4_2D00_26_2D00_12_2D00_beg_2D00_11.png" alt="Image of results" title="Image of results" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;~Jonathan&lt;/p&gt;
&lt;p&gt;The 2012 Scripting Games Guest Commentator Week Part 2 will continue tomorrow when we will present the scenario for Event 10.&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&amp;nbsp;&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=3493214" width="1" height="1" alt="" /&gt;</description></item></channel></rss>