<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Huddled Masses &#187; Joel &#8216;Jaykul&#8217; Bennett</title>
	<atom:link href="http://joelbennett.net/author/jaykul/feed/" rel="self" type="application/rss+xml" />
	<link>http://joelbennett.net</link>
	<description>The internet home of Joel "Jaykul" Bennett...</description>
	<lastBuildDate>Sat, 28 Jan 2012 21:37:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<cloud domain='joelbennett.net' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>Rich formatting for PowerShell help</title>
		<link>http://joelbennett.net/rich-formatting-for-powershell-help/</link>
		<comments>http://joelbennett.net/rich-formatting-for-powershell-help/#comments</comments>
		<pubDate>Sat, 28 Jan 2012 03:54:33 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Help]]></category>
		<category><![CDATA[Html]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[ShowUI]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1802</guid>
		<description><![CDATA[[updated] Ok, I just updated this with a new post on PoshCode. I posted the HtmlHelp module to PoshCode for generating HTML web pages based on the help for functions or cmdlets. It basically has one command: Get-HtmlHelp, which takes a couple of parameters now. The only mandatory parameter is the name of the command [...]]]></description>
			<content:encoded><![CDATA[	<p><strong>[updated]</strong> Ok, I just updated this with a new post on PoshCode.  I posted the <a href="http://poshcode.org/3197">HtmlHelp</a> module to PoshCode for <em>generating <span class="caps">HTML</span> web pages based on the help for functions or cmdlets</em>. It basically has one command: Get-HtmlHelp, which takes a couple of parameters now.  The only mandatory parameter is the name of the command you want help for, in this case the html is output on the pipeline and can be redirected into a file.</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">HtmlHelp</span></span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">HtmlHelp</span></span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">Content</span></span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">HtmlHelp</span></span>.<span style="color: #003366;">html</span><br />
&nbsp;</div>

	<p>The markup generated is (I hope) reasonable and lightweight, with some simple css rules pre-applied.  Feel free to customize the script to generate help however you like.  </p>

	<h2><strong> <img src='http://joelbennett.net/wordpress/wp-includes/' alt='[new]' class='wp-smiley' /> </strong> Generating many at once</h2>

	<p>I forgot to mention the other parameters on Get-HtmlHelp. They&#8217;re pretty cool, because if you want to upload your help you can do so with this. Say you created a module, and you wanted to generate all the help into files for uploading.  You want to set the <code>-BaseUrl</code> to the location you will upload them to, and then use the <code>-OutputFolder</code> parameter to generate an html file for each command into the specified folder:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Command</span></span> <span style="color: #000066;">-Module</span> ModuleName <span style="color: #66cc66;">|</span> <br />
<span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">HtmlHelp</span></span> <span style="color: #000066;">-BaseUrl</span> http:<span style="color: #66cc66;">//</span>HuddledMasses.<span style="color: #003366;">org</span><span style="color: #66cc66;">/</span>HtmlHelp<span style="color: #66cc66;">/</span> <span style="color: #000066;">-OutputFolder</span> ~\sites\HuddledMasses\HtmlHelp\<br />
&nbsp;</div>

	<p>Now you can just take those files and upload them to the right spot on your website. I actually have some scripts which I can wrap around this to post the help to a wiki, but you&#8217;re just going to have to wait for that until the next time I get inspired to work on help &#8230;</p>

	<h3>Show-Help</h3>

	<p>I did include a little function in the comments and in the help for Get-HtmlHelp which uses <a href="http://ShowUI.CodePlex.com">ShowUI</a> to display the rich formatted help in a popup window:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Show-<span style="font-style: normal;">Help</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #333;">&#91;</span>CmdletBinding<span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
<span style="color: #666699; font-weight: bold;">param</span><span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">String</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Name</span><span style="color: #333;">&#41;</span> &nbsp;<br />
&nbsp; &nbsp;Window <span style="color: #333;">&#123;</span> WebBrowser <span style="color: #000066;">-Name</span> wb <span style="color: #333;">&#125;</span> <span style="color: #000066;">-On_Loaded</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$wb</span>.<span style="color: #003366;">NavigateToString</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">HtmlHelp</span></span> <span style="color: #660033; font-weight: bold;">$Name</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">Title</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Get-Help $Name&quot;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Show</span><br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>So anyway, enough of that. When you run it, it looks like this (click for the full screenshot):</p>

	<p><a href="http://joelbennett.net/wordpress/wp-content/uploads/2012/01/Show-Help.png"><img src="http://joelbennett.net/wordpress/wp-content/uploads/2012/01/Show-Help-Mini.png" alt="Click to see full screen-shot" title="Show-Help" width="630" height="486" class="alignright size-full wp-image-1804" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://joelbennett.net/rich-formatting-for-powershell-help/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Did you know PowerShell can use Selenium?</title>
		<link>http://joelbennett.net/did-you-know-powershell-can-use-selenium/</link>
		<comments>http://joelbennett.net/did-you-know-powershell-can-use-selenium/#comments</comments>
		<pubDate>Thu, 05 Jan 2012 05:46:42 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Automation]]></category>
		<category><![CDATA[Browser]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Selenium]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[WatiN]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1789</guid>
		<description><![CDATA[This is sort-of a place-holder for a full-length post that I really ought to write about driving web testing from PowerShell using Selenium. I actually have a little module around for doing that with WaTiN, but honestly the Selenium project seems to be a lot more active, and has quite a bit of muscle behind [...]]]></description>
			<content:encoded><![CDATA[	<p>This is sort-of a place-holder for a full-length post that I really ought to write about driving web testing from PowerShell using Selenium.  I actually have a little module around for doing that with WaTiN, but honestly the Selenium project seems to be a lot more active, and has quite a bit of muscle behind it since they&#8217;ve merged with WebDriver&#8230;</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">Type</span></span> <span style="color: #000066;">-path</span> ~\Downloads\selenium<span style="color: #66cc66;">-</span>dotnet<span style="color: #66cc66;">-</span>2.16.0\net40\WebDriver.<span style="color: #003366;">dll</span> <br />
<br />
<span style="color: #666666; font-style: italic;"># Navigate to google in IE (or Firefox, Chrome, Opera, etc)</span><br />
<span style="color: #660033; font-weight: bold;">$driver</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> OpenQA.<span style="color: #003366;">Selenium</span>.<span style="color: #003366;">IE</span>.<span style="color: #003366;">InternetExplorerDriver</span> <br />
<span style="color: #660033; font-weight: bold;">$driver</span>.<span style="color: #003366;">Url</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;http://google.com&quot;</span><br />
<br />
<span style="color: #666666; font-style: italic;"># Type PowerShell into the query box, the page will update via AJAX</span><br />
<span style="color: #666666; font-style: italic;"># Note we won't hit enter or anything</span><br />
<span style="color: #660033; font-weight: bold;">$q</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$driver</span>.<span style="color: #003366;">FindElementByname</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;q&quot;</span><span style="color: #333;">&#41;</span> <br />
<span style="color: #660033; font-weight: bold;">$q</span>.<span style="color: #003366;">SendKeys</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;PowerShell&quot;</span><span style="color: #333;">&#41;</span> <br />
<br />
<span style="color: #666666; font-style: italic;"># Use a CSS selector to find the first result link and click it</span><br />
<span style="color: #660033; font-weight: bold;">$driver</span>.<span style="color: #003366;">FindElementByCssSelector</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;li.g h3.r a&quot;</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">Click</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp;</div>

	<h3>One Catch</h3>

	<p><a href="http://joelbennett.net/wordpress/wp-content/uploads/2012/01/ProtectedMode.png" style="float:right; padding-left: 1em; "><img src="http://joelbennett.net/wordpress/wp-content/uploads/2012/01/ProtectedMode.png" alt="The Security tab of the Internet Options dialog" title="ProtectedMode" width="423" height="541" class="size-full wp-image-1796" /></a>If you try this with IE and you get the error <em>Unexpected error launching Internet Explorer. Protected Mode must be set to the same value (enabled or disabled) for all zones</em> ... it means exactly what it says.  You need to open &#8220;Internet Options&#8221; from your start menu (or from IE), and go through each &#8220;zone&#8221; and set the &#8220;Enabled Protected Mode&#8221; check box to the same value for each zone (either all checked, obviously the most secure, or all unchecked).  I&#8217;m not going to debate whether setting them all <em>unprotected</em> is a good idea &#8230; I set mine to all protected, but I don&#8217;t generally use IE anyway. </p>

	<p>If you want more help, Selenium&#8217;s documentation is great, and there&#8217;s a section on <a href="http://seleniumhq.org/docs/03_webdriver.html#getting-started-with-selenium-webdriver">Getting Started with Selenium WebDriver</a> which I found quite helpful (make sure your examples are in &#8220;csharp&#8221; and you can almost just copy and paste &#8212; someone should offer to do them in PowerShell).  </p>

	<p>If you want more information about the Internet Explorer driver and this problem in particular, the short answer is that &#8220;Protected Mode&#8221; is a security boundry, so if you cross over it the <span class="caps">COM</span> automation object doesn&#8217;t work &#8212; thus, you need to make sure you always stay on the same side. There&#8217;s a good discussion <a href="http://code.google.com/p/selenium/issues/detail?id=1795">on the mailing list archive</a> about how it works and why, as well a weird alternative <a href="http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/ie/InternetExplorerDriver.html#INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS">documented on the Selenium JavaDocs</a></p>]]></content:encoded>
			<wfw:commentRss>http://joelbennett.net/did-you-know-powershell-can-use-selenium/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>PowerShell 3 &#8211; Finally on the DLR!</title>
		<link>http://joelbennett.net/powershell-3-finally-on-the-dlr/</link>
		<comments>http://joelbennett.net/powershell-3-finally-on-the-dlr/#comments</comments>
		<pubDate>Thu, 05 Jan 2012 05:38:21 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[CTP]]></category>
		<category><![CDATA[DLR]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[PS3]]></category>
		<category><![CDATA[Windows 8]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1780</guid>
		<description><![CDATA[For those of you living in a cave: PowerShell 3 will be released in Windows 8, and we got a CTP at roughly the same time as the Windows 8 Developer Preview was released (at Microsoft&#8217;s new //Build/ conference in September 2011). A second CTP was released just in time for Christmas. I&#8217;ve been playing [...]]]></description>
			<content:encoded><![CDATA[	<p class="highlight">For those of you living in a cave: PowerShell 3 will be released in Windows 8, and we got a <acronym title="Community Technology Preview">CTP</acronym> at roughly the same time as the Windows 8 Developer Preview was released (at Microsoft&#8217;s new <a href="https://www.microsoft.com/presspass/events/build/">//Build/</a> conference in September 2011).  A <a href="http://www.microsoft.com/download/en/details.aspx?id=27548">second CTP</a> was released just in time for Christmas.</p>

	<p>I&#8217;ve been playing with PowerShell 3 for a few months now, and I guess it&#8217;s long past time I started blogging about it.</p>

	<p>There are a <strong>lot</strong> of new things coming in this release, but for me, the biggest change is the fact that PowerShell is now based on the <a href="http://dlr.codeplex.com/">Dynamic Language Runtime</a>, a runtime environment that adds a set of services for dynamic languages to the Common Language Runtime (<span class="caps">CLR</span>), which is the core of the .<span class="caps">NET</span> Framework. The <span class="caps">DLR</span> makes it easier to develop dynamic languages to run on the .<span class="caps">NET</span> Framework.  Of course, PowerShell is a dynamic language that runs on the .<span class="caps">NET</span> framework, but it was originally begun before the <span class="caps">DLR</span> had been released, so it&#8217;s only now that it&#8217;s finally been adapted to the <span class="caps">DLR</span>.  </p>

	<p>However, although PowerShell 3 is implemented using the <span class="caps">DLR</span>, it&#8217;s not a <span class="caps">DLR</span> language in every way that IronPython or IronRuby are.  Let me borrow a couple of graphics from the <span class="caps">DLR</span> overview documentation. </p>

	<h3>The <span class="caps">DLR</span> Overview</h3>

	<p><img src="http://joelbennett.net/wordpress/wp-content/uploads/2011/12/DLR.png" title="DLR Overview" alt="DLR Overview" width="628" height="387" /></p>

	<p>You can see there&#8217;s three major sections to the <span class="caps">DLR</span> as it&#8217;s <a href="http://dlr.codeplex.com">available on CodePlex</a>: hosting, runtime, and language.  However, not all of the <span class="caps">DLR</span> actually shipped in the .<span class="caps">NET</span> Framework 4.0 <span class="caps">CLR</span>.</p>

	<h3>The <span class="caps">DLR</span> shipped in <span class="caps">CLR</span> 4</h3>

	<p><img src="http://joelbennett.net/wordpress/wp-content/uploads/2011/12/DLRonCLR4.png" title="DLR Shipped in CLR4" alt="DLR Shipped in CLR4" width="586" height="367" /></p>

	<p>PowerShell 3 takes advantage of all (or most) of what shipped in the <span class="caps">CLR</span>, but since the PowerShell team wasn&#8217;t willing to be responsible for shipping the rest of the <span class="caps">DLR</span> in the operating system, they didn&#8217;t implement the rest of it. Which is to say, PowerShell 3 is using the <span class="caps">DLR</span> Language Implementation code, with Shared <span class="caps">AST</span> and Expression trees, as well as the DynamicObject and Call Site Caching portions of the runtime, but none of the Common Hosting pieces like ScriptRuntime, ScriptScope, ScriptSource, or CompiledCode &#8230;</p>

	<p>This means that you cannot use the same hosting APIs for PowerShell that you use for IronPython or IronRuby.  However, even though you&#8217;re stuck using the same hosting APIs that you used with PowerShell 2 &#8230; you <strong>do</strong> get to use <strong>dynamic</strong> instead of PSObject when you&#8217;re working with the output in C#.</p>

	<h3>This really is a big deal</h3>

	<p>I wouldn&#8217;t care to speculate how many of the changes you&#8217;ll see in PowerShell 3 are directly due to the conversion to the <span class="caps">DLR</span>, but there are a few changes that you ought to be aware of. The first thing that you&#8217;ll probably notice is the difference in execution and performance. Anything you&#8217;ve learned about the relative performance of Scripts vs. Functions vs. Cmdlets and the load time of binary vs. script modules is going to go right out the window with PowerShell 3, as scripts and functions are now no longer (re)interpreted each time they&#8217;re run, but are <em>compiled</em>, executed, and (sometimes) cached.  The result is that initial runs of scripts and imports of script modules are sometimes slower than they used to be, but subsequent runs of the same script or execution of functions from script modules run much faster, and this applies in particular to actual scripts in files and pre-defined functions in modules.  Running a function repeatedly is now <em>much faster</em> than pasting the same code repeatedly into the console.</p>

	<h4>A more subtle, but significant difference is the change to PSObject.  </h4>

	<p>In PowerShell 3, PSObject is a true dynamic object, and thus the output of cmdlets or scripts called in C# can be used with the dynamic keyword in C# instead of with the pseduo-reflection methods which are required for working with PSObject.  However, this is just the tip of the iceberg, so to speak.  </p>

	<p>In PowerShell 2, all of PowerShell&#8217;s Extended Type System (<span class="caps">ETS</span>) was based on PSObject. New members were always added to a PSObject which is wrapped around the actual &#8220;BaseObject&#8221; &#8212; regardless of whether they came from a types.ps1xml file, or from calling Add-Member on an object.  If you use Add-Member on strongly objects that are not already wrapped in a PSObject, you have to specify the <code>-Passthru</code> parameter and capture the output in order to have your object wrapped into a PSObject that the new member can be added to. In addition, when you cast an object to a specific type, those <span class="caps">ETS</span> members are mostly lost. Take this script for example:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #660033; font-weight: bold;">$psObject</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">ChildItem</span></span><br />
<span style="color: #660033; font-weight: bold;">$psObject</span>.<span style="color: #003366;">Count</span><br />
<span style="color: #660033; font-weight: bold;">$Count1</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$psObject</span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">where</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">PSIsContainer</span> <span style="color: #333;">&#125;</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">Count</span><br />
<br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>IO.<span style="color: #003366;">FileSystemInfo</span><span style="color: #333;">&#91;</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$ioObject</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">ChildItem</span></span><br />
<span style="color: #660033; font-weight: bold;">$ioObject</span>.<span style="color: #003366;">Count</span><br />
<span style="color: #660033; font-weight: bold;">$Count2</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$ioObject</span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">where</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">PSIsContainer</span> <span style="color: #333;">&#125;</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">Count</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$Count3</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$ioObject</span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">where</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #000066;">-is</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>IO.<span style="color: #003366;">DirectoryInfo</span><span style="color: #333;">&#93;</span></span> <span style="color: #333;">&#125;</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">Count</span><br />
&nbsp;</div>

	<p>In PowerShell 2, <code>$Count1</code> and <code>$Count3</code> will be the number of folders in the current directory, but <code>$Count2</code> will always be <span class="caps">ZERO</span>, because the PSIsContainer property is actually an <span class="caps">ETS</span> property that&#8217;s lost when you cast the object to <code>FileSystemInfo</code> (and therefore it always evaluates as null and </p>

	<p>However, in PowerShell 3 that&#8217;s no longer true.  PowerShell now works with everything as dynamic objects, and Add-Member no longer needs the PSObject to keep track of these <span class="caps">ETS</span> members. This script will now get <code>$Count1</code>, <code>$Count2</code>, and <code>$Count3</code> equal, as expected. Obviously the <code>-Passthru</code> switch on <code>Add-Member</code> is only needed when you&#8217;re trying to pipeline things, and not for simple assignments.  However, there may also be other implications on when things get wrapped into a PSObject, and when it matters.  </p>

	<p>I think you&#8217;ll agree that having PowerShell on the <span class="caps">DLR</span> is awesome! But be aware that there are a few inconsequential <em>breaking changes</em> hiding in this kind of stuff.  For example, after running that script above, try these three lines on PowerShell 2 and PowerShell 3 CTP2:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #660033; font-weight: bold;">$Count1</span> <span style="color: #000066;">-eq</span> <span style="color: #660033; font-weight: bold;">$Count2</span><br />
<span style="color: #660033; font-weight: bold;">$e</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$ioObject</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">Member</span></span> NoteProperty Note <span style="color: #009900;">&quot;This is a note&quot;</span> <span style="color: #000066;">-Passthru</span><br />
<span style="color: #660033; font-weight: bold;">$f</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$ioObject</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">Member</span></span> NoteProperty Note <span style="color: #009900;">&quot;This is a note&quot;</span> <span style="color: #000066;">-Passthru</span><br />
&nbsp;</div>

	<p>In PowerShell 2, you&#8217;ll get <code>False</code>, and then the next two lines will work fine.  In PowerShell 3 the first line will return <code>True</code>, and since Add-Member actually affects the underlying object even when it&#8217;s not wrapped in a PSObject, the third line will actually <strong>cause an error</strong>, because &#8220;Add-Member : Cannot add a member with the name &#8220;Note&#8221; because a member with that name already exists.&#8221;</p>

	<p>Anyway, I&#8217;m sure I&#8217;ll have more to write about the <span class="caps">DLR</span> and the changes it&#8217;s bringing to PowerShell, but for now, I hope that&#8217;s enough to get you thinking  <img src='http://joelbennett.net/wordpress/wp-includes/' alt=';-)' class='wp-smiley' /> </p>]]></content:encoded>
			<wfw:commentRss>http://joelbennett.net/powershell-3-finally-on-the-dlr/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Arrange &#8211; Act &#8211; Assert: Intuitive Testing</title>
		<link>http://joelbennett.net/arrange-act-assert-intuitive-testing/</link>
		<comments>http://joelbennett.net/arrange-act-assert-intuitive-testing/#comments</comments>
		<pubDate>Wed, 05 Oct 2011 03:31:56 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[3As]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1757</guid>
		<description><![CDATA[Today I have a new module to introduce you to. It&#8217;s a relatively simple module for testing, and you can pick it up in short order and start testing your scripts, modules, and even compiled .Net code. If you put it together with WASP you can pretty much test anything The basis for the module [...]]]></description>
			<content:encoded><![CDATA[	<p>Today I have a new module to introduce you to. It&#8217;s a relatively simple module for testing, and you can pick it up in short order and start testing your scripts, modules, and even compiled .Net code. If you put it together with <a href="http://poshcode.org/2483" title="my UI Automation module">WASP</a> you can pretty much test anything  <img src='http://joelbennett.net/wordpress/wp-includes/' alt=';-)' class='wp-smiley' /> </p>

	<p>The basis for the module is the arrange-act-assert model of testing. First we <strong>arrange</strong> the things we&#8217;re going to test: set up data structures or whatever you need for testing. Then we <strong>act</strong> on them: we perform the actual test steps. Finally, we <strong>assert</strong> the expected output of the test. Normally, the expectation is that during the assert step we&#8217;ll return <code>$false</code> if the test failed, and that&#8217;s all there is to it. Of course, there&#8217;s plenty more to testing, but lets move on to my new module.</p>

	<p>The module is called <a href="/downloads/PSaint.7z">PSaint</a> (pronounced &#8220;saint&#8221;), and it stands, loosely, for PowerShell Arrange-Act-Assert in testing. Of course, what it stands for isn&#8217;t important, just remember the name is PSaint  <img src='http://joelbennett.net/wordpress/wp-includes/' alt=':)' class='wp-smiley' /> </p>

	<p>PSaint is really a very simple module, with only a few functions. There are two major functions which we&#8217;ll discuss in detail: Test-Code and New-RhinoMock, and then a few helpers which you may or may not even use:</p>

	<h3>Set-TestFilter </h3>

	<p>Sets filters (include and/or exclude) for the tests by name or category.</p>

	<h3>Set-TestSetup (alias &#8220;Setup&#8221;)</h3>

	<p>Sets the test setup ScriptBlock which will be run before each test.</p>

	<h3>Set-TestTeardown (alias &#8220;Teardown&#8221;)</h3>

	<p>Sets the test teardown ScriptBlock which will be run after each test.</p>

	<h3>Assert-That</h3>

	<p>Assserts something about an object (or the output of a scriptblock) and throws if that assertion is false.  This function supports asserting that an exception should be thrown, or that a test is false &#8230; and supports customizing the error message as well.</p>

	<h3>Assert-PropertyEqual</h3>

	<p>This is a wrapper around Compare-Object to compare the properties of two objects.</p>

	<h2>How to test with PSaint: Test-Code</h2>

	<p>Test-Code (alias &#8220;Test&#8221;) is the main driver of functionality in PSaint, and you use it to define the tests that you want to run.  Let&#8217;s jump to an example or two so you can see the usefulness of this module.</p>

	<p>Let&#8217;s start with an extremely simple function that we want to write: New-Guid.  We want a function that generates a valid random <span class="caps">GUID</span> as a string. We&#8217;ll start by writing a couple of tests. First we&#8217;ll test that the output of the function is a valid <span class="caps">GUID</span>.</p>

	<div class="posh code posh" style="font-family:monospace;">test <span style="color: #009900;">&quot;New-Guid outputs a Guid&quot;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;act <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$guid</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Guid</span></span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;assert <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$guid</span> <span style="color: #000066;">-is</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> Guid <span style="color: #660033; font-weight: bold;">$guid</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>Now, to verify that the test works, you should define this function (the GUID-looking thing is one letter short) and then run that test:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Guid</span></span> <span style="color: #333;">&#123;</span> <span style="color: #009900;">&quot;aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaa&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>Another proof that it works would be that it should fail on this function too, because &#8220;x&#8221; is not a valid character in a Guid:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Guid</span></span> <span style="color: #333;">&#123;</span> <span style="color: #009900;">&quot;xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>So, let&#8217;s write a minimal New-Guid that actually generates a valid Guid:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Guid</span></span> <span style="color: #333;">&#123;</span> <span style="color: #009900;">&quot;aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>If you run our test on that, you will see:</p>

	<div class="text code text" style="font-family:monospace;">&nbsp; &nbsp;Result: Pass<br />
<br />
Result Name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Category<br />
------ ---- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;--------<br />
Pass &nbsp; New-Guid outputs a Guid<br />
&nbsp;</div>

	<p>If you don&#8217;t like the fact that the Category is empty, you could add a category or two to the end of our test.  We should also switch to using Assert-That if we want to know which test in the assert failed. Finally, we want to write another test which would test that New-Guid doesn&#8217;t just return the same Guid every time, the way ours does right now:</p>

	<div class="posh code posh" style="font-family:monospace;">test <span style="color: #009900;">&quot;New-Guid outputs a Guid&quot;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;act <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$guid</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Guid</span></span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;assert <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Assert-<span style="font-style: normal;">That</span></span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$guid</span> <span style="color: #000066;">-is</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span> <span style="color: #333;">&#125;</span> <span style="color: #000066;">-FailMessage</span> <span style="color: #009900;">&quot;New-Guid returned a $($guid.GetType().FullName)&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> Guid <span style="color: #660033; font-weight: bold;">$guid</span> &nbsp;<span style="color: #666666; font-style: italic;"># Throws relevant errors already</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Category</span> Output, ValidGuid<br />
<br />
test <span style="color: #009900;">&quot;New-Guid outputs different Guids&quot;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;arrange <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$guids</span> <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$count</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">100</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;act <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># generate a bunch of Guids</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">for</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$i</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">0</span>; <span style="color: #660033; font-weight: bold;">$i</span> <span style="color: #000066;">-lt</span> <span style="color: #660033; font-weight: bold;">$count</span>; <span style="color: #660033; font-weight: bold;">$i</span><span style="color: #66cc66;">++</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$guids</span> <span style="color: #66cc66;">+=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Guid</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;assert <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># compare each guid to all the ones after it</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">for</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$i</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">0</span>; <span style="color: #660033; font-weight: bold;">$i</span> <span style="color: #000066;">-lt</span> <span style="color: #660033; font-weight: bold;">$count</span>; <span style="color: #660033; font-weight: bold;">$i</span><span style="color: #66cc66;">++</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">for</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$j</span><span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$i</span><span style="color: #66cc66;">+</span><span style="color: #cc66cc;">1</span>; <span style="color: #660033; font-weight: bold;">$j</span> <span style="color: #000066;">-lt</span> <span style="color: #660033; font-weight: bold;">$count</span>; <span style="color: #660033; font-weight: bold;">$j</span><span style="color: #66cc66;">++</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Assert-<span style="font-style: normal;">That</span></span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$guids</span><span style="color: #333;">&#91;</span><span style="color: #660033; font-weight: bold;">$i</span><span style="color: #333;">&#93;</span> <span style="color: #000066;">-ne</span> <span style="color: #660033; font-weight: bold;">$guids</span><span style="color: #333;">&#91;</span><span style="color: #660033; font-weight: bold;">$j</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#41;</span> <span style="color: #000066;">-FailMessage</span> <span style="color: #009900;">&quot;There were equal Guids: $($guids[$i])&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Category</span> Output, RandomGuids<br />
&nbsp;</div>

	<p>Now, we have to actually fix our New-Guid function to generate real random Guids:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Guid</span></span> <span style="color: #333;">&#123;</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Guid</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">NewGuid</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">ToString</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>And at that point, we should have a function, and a couple of tests that verify it&#8217;s functionality&#8230;</p>

	<h3>The finer points of assertions</h3>

	<p>One thing you&#8217;ll notice the first time you use Get-Member after loading the PSaint module is a few script properties have been added to everything.  I did this because I found myself writing the same Assert-That calls over and over and decided that it would be slicker to make these extension methods than to write new functions for each one:</p>

	<div class="posh code posh" style="font-family:monospace;">MustBeA<span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">Type</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Expected</span>,<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Message</span><span style="color: #333;">&#41;</span><br />
MustBeFalse<span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Message</span><span style="color: #333;">&#41;</span><br />
MustBeTrue<span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Message</span><span style="color: #333;">&#41;</span><br />
MustEqual<span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Object<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Expected</span>,<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Message</span><span style="color: #333;">&#41;</span><br />
MustNotEqual<span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Object<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Expected</span>,<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Message</span><span style="color: #333;">&#41;</span><br />
&nbsp;</div>

	<p>There&#8217;s also a <span style="font-family:monospace;">MustThrow<span style="color: #333;">(</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">[</span><span style="color: #003366; font-weight: bold;">Type</span><span style="color: #333;">]</span></span><span style="color: #660033; font-weight: bold;">$Expected</span>, <span style="color: #003366; font-weight: bold;"><span style="color: #333;">[</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">]</span></span><span style="color: #660033; font-weight: bold;">$Message</span><span style="color: #333;">)</span></span> which can be used on script blocks (note that this function executes the ScriptBlock immediately, so be careful how you use it).</p>

	<p>We can use these to tidy up our tests quite a bit, while still getting good error messages when tests fail:</p>

	<div class="posh code posh" style="font-family:monospace;">test <span style="color: #009900;">&quot;New-Guid outputs a Guid String&quot;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;act <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$guid</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Guid</span></span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;assert <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$guid</span>.<span style="color: #003366;">MustBeA</span><span style="color: #333;">&#40;</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> Guid <span style="color: #660033; font-weight: bold;">$guid</span> <span style="color: #666666; font-style: italic;"># Throws relevant errors already</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Category</span> Output, ValidGuid<br />
<br />
test <span style="color: #009900;">&quot;New-Guid outputs different Guids&quot;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;arrange <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$guids</span> <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$count</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">100</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;act <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># generate a bunch of Guids</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">for</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$i</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">0</span>; <span style="color: #660033; font-weight: bold;">$i</span> <span style="color: #000066;">-lt</span> <span style="color: #660033; font-weight: bold;">$count</span>; <span style="color: #660033; font-weight: bold;">$i</span><span style="color: #66cc66;">++</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$guids</span> <span style="color: #66cc66;">+=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Guid</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;assert <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># compare each guid to all the ones after it</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">for</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$i</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">0</span>; <span style="color: #660033; font-weight: bold;">$i</span> <span style="color: #000066;">-lt</span> <span style="color: #660033; font-weight: bold;">$count</span>; <span style="color: #660033; font-weight: bold;">$i</span><span style="color: #66cc66;">++</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">for</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$j</span><span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$i</span><span style="color: #66cc66;">+</span><span style="color: #cc66cc;">1</span>; <span style="color: #660033; font-weight: bold;">$j</span> <span style="color: #000066;">-lt</span> <span style="color: #660033; font-weight: bold;">$count</span>; <span style="color: #660033; font-weight: bold;">$j</span><span style="color: #66cc66;">++</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$guids</span><span style="color: #333;">&#91;</span><span style="color: #660033; font-weight: bold;">$i</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">MustNotEqual</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$guids</span><span style="color: #333;">&#91;</span><span style="color: #660033; font-weight: bold;">$j</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Category</span> Output, RandomGuids<br />
&nbsp;</div>

	<h3><span class="caps">COM</span> Objects </h3>

	<p>PSaint also has a wrapper for <span class="caps">COM</span> objects to help with testing them. It adds GetProperty and SetProperty methods to allow you to access <span class="caps">COM</span> object properties which don&#8217;t show up on boxed <span class="caps">COM</span> objects (a common problem when working with MSOffice, for instance).  It also adds InvokeMethod for <span class="caps">COM</span> objects to invoke methods that don&#8217;t show up for similar reasons.  These, of course, only help you if you&#8217;re already fairly literate with the <span class="caps">COM</span> object in question.</p>

	<h3>Mock Objects</h3>

	<p>PSaint includes New-RhinoMock, a function for generating a new mock object using <a href="http://hibernatingrhinos.com/open-source/rhino-mocks">RhinoMocks</a> (which is included).  Rhino Mocks is a BSD-licensed dynamic mock object framework for the .Net platform. Its purpose is to ease testing by allowing the developer to create mock implementations of custom objects and verify the interactions using unit testing.</p>

	<p>I have to admit that this New-RhinoMock function is incomplete, and exposes only a fraction of the options and power in RhinoMocks, but it&#8217;s been sufficient for the few times when I&#8217;ve wanted to actually mock objects from PowerShell, so I&#8217;m including it here.</p>

	<p>For those of you (developers) who want to know why RhinoMocks instead of your favorite mocking framework, the answer is astonishingly simple: it had the least number of necessary generic methods (which are impossible to call in PowerShell 2).</p>]]></content:encoded>
			<wfw:commentRss>http://joelbennett.net/arrange-act-assert-intuitive-testing/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Ramblings about computer markets &#8230;</title>
		<link>http://joelbennett.net/ramblings-about-computer-markets/</link>
		<comments>http://joelbennett.net/ramblings-about-computer-markets/#comments</comments>
		<pubDate>Tue, 04 Oct 2011 04:30:29 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[BusinessModel]]></category>
		<category><![CDATA[Capitalism]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1762</guid>
		<description><![CDATA[This is what I call a stream of consciousness, edited. Please don&#8217;t rip my head off, call me names, etc. Microsoft is a product of the commodity hardware era. That is, Microsoft is a company that couldn&#8217;t have existed previously, in an era where computers cost tens of thousands of dollars (or hundreds of thousands [...]]]></description>
			<content:encoded><![CDATA[	<p>This is what I call a stream of consciousness, edited.  Please don&#8217;t rip my head off, call me names, etc.</p>

	<h3>Microsoft is a product of the commodity hardware era.  </h3>

	<p>That is, Microsoft is a company that couldn&#8217;t have existed previously, in an era where computers cost tens of thousands of dollars (or hundreds of thousands of dollars).  When the computers themselves were expensive, the hardware was the item of value.  </p>

	<h3>There was only one business model.</h3>

	<p>Companies producing them were not thinking about the software running on them as valuable (and even missed patenting things like windowed interfaces, pointing devices, and networking protocols), because they through the valuable thing would be the hardware forever.</p>

	<h3>Modern business models for personal computing.</h3>

	<p>In the modern era, we have three major business models:  hardware, software, service.</p>

	<p>Apple represents the hardware model: they&#8217;re making <strong>billions</strong> selling top-end hardware (phones, laptops, computers, tablets). They&#8217;re basically giving away their software. They&#8217;re trying to commodify the software &#8212; making it trivial and making it easier for thousands of companies and even individuals to produce competing versions of every possible application <em>on their hardware</em>.</p>

	<p>Microsoft represents the software model: they&#8217;re making <strong>billions</strong> selling software licenses for software that runs <em>on any kind of hardware</em>. They&#8217;re actively trying to commodify hardware, making their software run on any kind of hardware from hundreds of manufacturers, and driving prices of home computers, netbooks, and tablets down to a couple of hundred dollars.</p>

	<p>RedHat (and Linux vendors in general) represents the service model: they&#8217;re making <strong>millions</strong> selling service contracts for <em>free</em> software that runs on that same really cheap hardware that Microsoft&#8217;s model has been driving.</p>

	<h3>How might that affect service, software, and hardware?</h3>

	<p>It wouldn&#8217;t be any surprise if <em>service</em> companies didn&#8217;t care much about how user friendly the software is: after all, they only make money if you need support to use it.</p>

	<p>It would also not be surprising if <em>software</em> companies didn&#8217;t care how confusing the hardware choices are &#8212; they want to make sure that there are <em>lots</em> of hardware companies, because that&#8217;s how you maintain the commodity status of the hardware.</p>

	<p>And of course, it wouldn&#8217;t be a surprise if <em>hardware</em> companies made high end hardware, released their software updates freely or cheaply, treating the software like firmware designed to run only on their specific hardware models.  These companies <em>might</em> have the lowest barriers to switching hardware architectures and creating new hardware form factors, since they&#8217;re providing a stand-alone platform (the software is like firmware) without much regard for disruption of software platforms.</p>

	<h3>Which market is easier to enter?</h3>

	<p>It seems to me like the most sustainable of these models, and more importantly, the most favorable to the consumer &#8230; is the software model.  I prefer business models which encourage competition, and I think that hardware and service models do not.</p>

	<p>On the one hand it&#8217;s easier to grow a software company than it is to grow a new service company.  It&#8217;s very hard to compete with the big guys on service because of reach (you can&#8217;t provide world-wide service), and as a result, very few service companies make it past the mom-and-pop size to medium-sized businesses employing more than 10 people.</p>

	<p>On the other hand, it&#8217;s easier to create a new software company than it is to create a new hardware company. It&#8217;s very hard to compete with the big guys on hardware because of costs (you can&#8217;t get hardware costs down without substantial volume).</p>

	<p>You only have to look at the past few years to see that it takes a long time for a new hardware company to grow to a useful size, and that service companies tend to consolidate long before they become competitive.</p>

	<h3>But you&#8217;re not taking X into account</h3>

	<p>Of <em>course</em>, this is a fairly simple interpretation of the personal computer marketplace.  I know that. Tell me something I don&#8217;t know below.</p>]]></content:encoded>
			<wfw:commentRss>http://joelbennett.net/ramblings-about-computer-markets/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Working with multiple versions of PowerShell Modules</title>
		<link>http://joelbennett.net/working-with-multiple-versions-of-powershell-modules/</link>
		<comments>http://joelbennett.net/working-with-multiple-versions-of-powershell-modules/#comments</comments>
		<pubDate>Wed, 31 Aug 2011 04:14:45 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Modules]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Versioning]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1744</guid>
		<description><![CDATA[What follows is a brief explanation of modules, followed by an explanation of how I handle multiple versions of a module. My way isn&#8217;t the only way &#8212; and I&#8217;m rather annoyed that I have to do it &#8212; but it works, and might help someone, so here it is. Some back-story During development of [...]]]></description>
			<content:encoded><![CDATA[	<p>What follows is a brief explanation of modules, followed by an explanation of how <strong>I</strong> handle multiple versions of a module. My way isn&#8217;t the only way &#8212; and I&#8217;m <em>rather annoyed</em> that I have to do it &#8212; but it works, and might help someone, so here it is.</p>

	<h2>Some back-story</h2>

	<p>During development of a PowerShell module like <a href="http://ShowUI.CodePlex.com">ShowUI</a>, I am usually <strong>not</strong> using a released version of the module, since I&#8217;m obviously working on the next version. In addition, sometimes I need to have &#8220;beta&#8221; and &#8220;stable&#8221; releases &#8230; so I might have two or three versions of the module that I need to be able to load in order to support users and actually do development.</p>

	<p>In case you don&#8217;t know how modules work, here&#8217;s the deal: There&#8217;s an environment variable <code>PSModulePath</code>. It&#8217;s just like the <code>Path</code> environment variable, but for modules. The variable contains a semi-colon delimited list of folders paths, which PowerShell searches for modules.  Some people think of a PowerShell module as basically a .dll (binary module), a .psm1 (script module), or a .psd1 (manifest module)... but it&#8217;s never <em>just</em> a file, it&#8217;s a <em>*Folder* and a file</em>.  In order for PowerShell to find ShowUI when you write <code>Import-Module ShowUI</code>, you have to have a <strong>Folder</strong> in the PSModulePath named &#8220;ShowUI&#8221; and in it, a file (either .dll, .psm1, or .psd1) also named ShowUI.</p>

	<h3>What I would <em>like</em></h3>

	<p>The Import-Module command has a -Version parameter. What I expected What I would <em>like</em> to be able to do is create a folder &#8220;ShowUI&#8221; and in there, create folders &#8220;1.0&#8221; and &#8220;1.1&#8221; and &#8220;1.2&#8221; and then be able to use <code>Import-Module ShowUI -Version 1.1</code> to load a specific version. </p>

	<p>Sadly, this doesn&#8217;t work. First of all, the -Version parameter on Import-Module, is a <strong>minimum</strong> only. Additionally, Import-Module requires the file and folder to match names exactly, so putting the module inside a subfolder doesn&#8217;t work even if there&#8217;s only one.</p>

	<p>However, there are two things we can use to fix our problem:</p>

	<p>1. Just like the <span class="caps">PATH</span> variable, PSModulePath works in order. PowerShell searches the listed paths one at a time and uses the first Module that meets the specified minimimum version.<br />
2. Additionally, PowerShell allows you to specify a relative Path to a module that starts in a PSModulePath &#8230; I&#8217;ll explain more in a moment.</p>

	<h2>My <em>actual</em> working environment</h2>

	<p>All of my development projects are in $Home\Projects, so I put a &#8220;Modules&#8221; folder in there which is where I put modules that I&#8217;m actively working on.  Then I add that folder to my PSModulePath environment variable: @$Env:PSModulePath += &#8220;;$Home\Projects&#8221; ...</p>

	<p>Since I put it on the end, modules in there won&#8217;t be imported if there&#8217;s a module with the same name in my regular Module folder ( $Home\Documents\WindowsPowerShell\Modules ), <strong>unless</strong> I increment the version number in the psd1 metadata file and specify -Version when importing.</p>

	<p>For example, I have two copies of WASP:</p>

	<p>$Home\Documents\WindowsPowerShell\Modules\WASP\Wasp.psd1  (version 1.0)<br />
$Home\Projects\Modules\WASP\Wasp.psd1 (version 2.0.0.2)</p>

	<p>If I want to load the released version, I can just <code>Import-Module WASP</code> &#8212; this ensures that scripts that I have in my scripts folder generally load the released version of <span class="caps">WASP</span>.  When I want to load the development version, I can <code>Import-Module WASP -Version 2.0</code>, and get the version from my Projects folder, because the released version&#8217;s version number is lower than 2.0.</p>

	<h2>More layers onion boy?</h2>

	<p>When I need to have more than one older version, this presents a problem, because I would need another folder in my path, and for the numbering scheme to work despite the fact that -Version is only a minimum value, I would need the released version (say 1.0) to come first in the path, then the beta version (say 2.0.1), then the development version (say 2.1) ... which means I&#8217;d need to add a $Home\Projects\BetaReleaseModules or something like that, and put it in my PSModulePath before the $Home\Projects\Modules folder.  That would work, but you can imagine that it wouldn&#8217;t scale very well.</p>

	<p>Here&#8217;s what I do: past the release and development versions, I start creating folders like this:</p>

	<p>$Home\Documents\WindowsPowerShell\Modules\ShowUI1.0\ShowUI.psd1  (version 1.0)<br />
$Home\Documents\WindowsPowerShell\Modules\ShowUI\ShowUI.psd1  (Release version: 1.1)<br />
$Home\Documents\WindowsPowerShell\Modules\ShowUI1.1\ShowUI.psd1  (Release version: 1.1)<br />
$Home\Documents\WindowsPowerShell\Modules\ShowUI1.2\ShowUI.psd1  (Beta version 1.2)<br />
$Home\Projects\Modules\ShowUI\ShowUI.psd1 (Development version 1.3)</p>

	<p>Now, if I <code>Import-Module ShowUI</code>, I&#8217;ll get 1.1 and if I specify <code>Import-Module ShowUI -Version 1.2</code> I&#8217;ll get &#8230; version 1.3 (<em>whoops</em>).</p>

	<p>To load ShowUI 1.2 with that structure I have to do this: <code>Import-Module ShowUI1.2\ShowUI</code> &#8212; specifying the folder name <strong>and</strong> the module name.  Of course, I could use &#8220;ShowUIBeta&#8221; as the folder name instead, but usually when I get a request for help writing scripts, I can get people to give me the specific version that they&#8217;re working with, so I like the numbered folders instead.  </p>

	<p>A last important note is that I <strong>do not</strong> rename the psd1 or anything else.  </p>

	<p>I <em>could</em> rename the metadata file to $Home\Documents\WindowsPowerShell\Modules\ShowUI1.2\ShowUI1.2.psd1 and then I&#8217;d be able to <code>Import-Module ShowUI1.2</code>, but there&#8217;s a catch.  If I rename it, then I&#8217;ve renamed the module.  If I leave the metadata file as &#8220;ShowUI.psd1&#8221; then <em>once I&#8217;ve imported the module</em> I can call <code>Get-Module ShowUI</code> or <code>Get-Command -Module ShowUI</code> and they still work as they should.</p>

	<p>In any case, this folder-naming stuff is less than ideal and will hopefully be fixed in a future release of PowerShell, but with a &#8220;Projects\Modules&#8221; folder I can have two versions, and usually a development and release version is enough.  Personally, I keep old releases of major modules around in numbered folders just in case, but I rarely actually use them.</p>]]></content:encoded>
			<wfw:commentRss>http://joelbennett.net/working-with-multiple-versions-of-powershell-modules/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>More ShowUI Widgets</title>
		<link>http://joelbennett.net/more-showui-widgets/</link>
		<comments>http://joelbennett.net/more-showui-widgets/#comments</comments>
		<pubDate>Wed, 20 Jul 2011 05:49:37 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Clock]]></category>
		<category><![CDATA[Gadgets]]></category>
		<category><![CDATA[ShowUI]]></category>
		<category><![CDATA[Widgets]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1735</guid>
		<description><![CDATA[Well, I got so many tweets and comments on IRC about my ShowUI clock that I figured I&#8217;d share a few other widgets I had lying around &#8230; but first, an update to New-UIWidget: function New-UIWidget &#123; &#91;CmdletBinding&#40;&#41;&#93; param&#40; &#160; &#160; &#91;ScriptBlock&#93;$Content, &#160; &#160; &#91;Alias&#40;&#34;Refresh&#34;&#41;&#93; &#160; &#160; &#91;TimeSpan&#93;$Interval = &#34;0:0:2&#34;, &#160; &#160; &#91;ScriptBlock&#93;$UpdateBlock, &#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[	<p>Well, I got so many tweets and comments on <span class="caps">IRC</span> about <a href="http://huddledmasses.org/the-obligatory-showui-clock/">my ShowUI clock</a> that I figured I&#8217;d share a few other widgets I had lying around &#8230; but first, an update to New-UIWidget:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">UIWidget</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #333;">&#91;</span>CmdletBinding<span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
<span style="color: #666699; font-weight: bold;">param</span><span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">ScriptBlock</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Content</span>,<br />
&nbsp; &nbsp; <span style="color: #333;">&#91;</span>Alias<span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;Refresh&quot;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>TimeSpan<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Interval</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;0:0:2&quot;</span>,<br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">ScriptBlock</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$UpdateBlock</span>,<br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #666699; font-weight: bold;">Switch</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Show</span>, <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #666699; font-weight: bold;">Switch</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$AsJob</span><br />
<span style="color: #333;">&#41;</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$WidgetValues</span> <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; AllowsTransparency <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$true</span><br />
&nbsp; &nbsp; WindowStyle <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;None&quot;</span> <br />
&nbsp; &nbsp; ShowInTaskbar <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$true</span><br />
&nbsp; &nbsp; Background <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Transparent&quot;</span> <br />
&nbsp; &nbsp; On_MouseLeftButtonDown <span style="color: #66cc66;">=</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$Window</span>.<span style="color: #003366;">DragMove</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; On_Closing <span style="color: #66cc66;">=</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$Window</span>.<span style="color: #003366;">Resources</span>.<span style="color: #003366;">Timers</span>.<span style="color: #009900;">&quot;Clock&quot;</span>.<span style="color: #003366;">Stop</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; Tag <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#123;</span><span style="color: #009900;">&quot;UpdateBlock&quot;</span><span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$UpdateBlock</span>;<span style="color: #009900;">&quot;Interval&quot;</span><span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$Interval</span><span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; SizeToContent <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;WidthAndHeight&quot;</span><br />
&nbsp; &nbsp; ResizeMode <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;NoResize&quot;</span><br />
&nbsp; &nbsp; On_SourceInitialized <span style="color: #66cc66;">=</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Window</span>.<span style="color: #003366;">Resources</span>.<span style="color: #003366;">Timers</span>.<span style="color: #003366;">Clock</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> Windows.<span style="color: #003366;">Threading</span>.<span style="color: #003366;">DispatcherTimer</span><span style="color: #333;">&#41;</span>.<span style="color: #003366; font-weight: bold;">PSObject</span>.<span style="color: #003366;">BaseObject</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Window</span>.<span style="color: #003366;">Resources</span>.<span style="color: #003366;">Timers</span>.<span style="color: #003366;">Clock</span>.<span style="color: #003366;">Interval</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$Window</span>.<span style="color: #003366;">Tag</span>.<span style="color: #003366;">Interval</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">EventHandler</span></span> <span style="color: #660033; font-weight: bold;">$Window</span>.<span style="color: #003366;">Resources</span>.<span style="color: #003366;">Timers</span>.<span style="color: #003366;">Clock</span> Tick <span style="color: #660033; font-weight: bold;">$Window</span>.<span style="color: #003366;">Tag</span>.<span style="color: #003366;">UpdateBlock</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Window</span>.<span style="color: #003366;">Resources</span>.<span style="color: #003366;">Timers</span>.<span style="color: #003366;">Clock</span>.<span style="color: #660033;">Start</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Window</span>.<span style="color: #003366;">Tag</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$Null</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; On_ContentRendered <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$UpdateBlock</span> &nbsp; <br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Window</span></span> @WidgetValues <span style="color: #000066;">-Content</span> <span style="color: #660033; font-weight: bold;">$Content</span> <span style="color: #000066;">-Show</span>:<span style="color: #660033; font-weight: bold;">$Show</span> <span style="color: #000066;">-AsJob</span>:<span style="color: #660033; font-weight: bold;">$AsJob</span><br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>Basically I just switched it to use Add-EventHandler, which in ShowUI does at <strong>lot</strong> of awesome magic to make sure all the variables you would want (i.e.: variables for named controls) are defined in your event handler.  It makes writing these gadgets much easier. Here are a few:</p>

	<p>Let&#8217;s start with the simplest Widget I can dream up:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">UIWidget</span></span> <span style="color: #333;">&#123;</span> Label <span style="color: #000066;">-Name</span> Time <span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">48</span> <span style="color: #333;">&#125;</span> <span style="color: #009900;">&quot;0:0:0.2&quot;</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$Time</span>.<span style="color: #003366;">Content</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Date</span></span> <span style="color: #000066;">-f</span> <span style="color: #009900;">&quot;h:mm tt&quot;</span><span style="color: #333;">&#125;</span></div>

	<p>Yeah, just a simple text clock. To be honest, that&#8217;s a bit plain for me &#8230; but I do like digital. </p>

	<h3>Another few Clocks</h3>

	<p>My preference is for something very large, with the date and stuff on it:<br />
<img src="/images/ShowUI/Widget-DigiClock.png" alt="" /><br />
<script type="text/javascript" src="http://PoshCode.org/embed/2808"></script></p>

	<p>If you prefer analog clocks, this one has &#8220;hands&#8221; and old school stopped movement (no click sound, but maybe <em>you</em> could add that)...<br />
<img src="/images/ShowUI/Widget-HandsClock.png" alt="" /><br />
<script type="text/javascript" src="http://PoshCode.org/embed/2806"></script></p>

	<p>And finally, while I&#8217;m on the subject, the clock from yesterday could be a lot simpler if it didn&#8217;t need animation, here&#8217;s the target part of it. Feel free to explore.<br />
<img src="/images/ShowUI/Widget-TargetClock1.png" alt="" /><br />
<script type="text/javascript" src="http://PoshCode.org/embed/2807"></script></p>

	<h3>And now for something completely different</h3>

	<p>My other favorite widget to implement in new UI frameworks is a weather widget. Why? Well, mostly because it requires parsing an <span class="caps">RSS</span> feed and a few extra pieces of work that let you actually determine how hard it&#8217;s going to be to write more advanced UI. In any case, here&#8217;s my ShowUI weather widget (note that you have to change the <code>$woEID</code> to get your forecast):<br />
<img src="/images/ShowUI/Widget-Weather.png" alt="" /><br />
<script type="text/javascript" src="http://PoshCode.org/embed/2809"></script></p>

	<p>OK, that&#8217;s enough for one night, but I&#8217;ll take requests for tomorrow! As a side note, you know the cool thing about these gadgets? When I took the screenshots, those were all running from the same PowerShell window <code>-AsJob</code>s, and using less than 1% <span class="caps">CPU</span>.</p>]]></content:encoded>
			<wfw:commentRss>http://joelbennett.net/more-showui-widgets/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Obligatory ShowUI Clock</title>
		<link>http://joelbennett.net/the-obligatory-showui-clock/</link>
		<comments>http://joelbennett.net/the-obligatory-showui-clock/#comments</comments>
		<pubDate>Tue, 19 Jul 2011 05:44:42 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Animation]]></category>
		<category><![CDATA[ShowUI]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1724</guid>
		<description><![CDATA[Well, it all started when Richard Siddaway posted his PowerShell Clock using raw XAML. Then Doug posted his ShowUI Clock, and after some back and forth on our ShowUI developer mailing list, James Brundage posted a video walkthrough of building his clock &#8230; Now I&#8217;ve dug an old clock of mine out of mothballs and [...]]]></description>
			<content:encoded><![CDATA[	<p>Well, it all started when Richard Siddaway posted his <a href="http://msmvps.com/blogs/richardsiddaway/archive/2011/07/07/a-powershell-clock.aspx">PowerShell Clock</a> using raw <span class="caps">XAML</span>. Then <a href="http://www.dougfinke.com/blog/index.php/2011/07/13/updated-hippy-dippy-powershell-clock-with-showui/">Doug posted his ShowUI Clock</a>, and after some back and forth on our ShowUI developer mailing list,  <a href="http://www.youtube.com/watch?v=Hqn-b2HMgSM">James Brundage posted a video walkthrough of building his clock</a> &#8230;</p>

	<p>Now I&#8217;ve dug an old clock of mine out of mothballs and updated it to run on ShowUI &#8212; I believe that I&#8217;ve never posted this script before, so I don&#8217;t feel bad at all doing so now, even though I actually wrote it last December (hey, look, I have a <a href="http://huddledmasses.org/images/PowerBoots/Gadget-SuperClock.png">screenshot against my bright red Christmas wallpaper to prove it</a> &#8212; totally random, I know).  Anyway, here&#8217;s the deal: this post was supposed to be written 6 months ago, about building fun UI Widgets in PowerShell, but I never got to it, because we started working on the PowerBoots + <span class="caps">WPK</span> merger we call ShowUI.  So here&#8217;s a fun little function for you:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">UIWidget</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #666699; font-weight: bold;">param</span><span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">ScriptBlock</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Content</span>,<br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>TimeSpan<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Interval</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;0:0:2&quot;</span>,<br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">ScriptBlock</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$UpdateBlock</span>,<br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #666699; font-weight: bold;">Switch</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Show</span>, <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #666699; font-weight: bold;">Switch</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$AsJob</span><br />
<span style="color: #333;">&#41;</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$WidgetValues</span> <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; AllowsTransparency <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$true</span><br />
&nbsp; &nbsp; WindowStyle <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;None&quot;</span> <br />
&nbsp; &nbsp; ShowInTaskbar <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$true</span><br />
&nbsp; &nbsp; Background <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Transparent&quot;</span> <br />
&nbsp; &nbsp; On_MouseLeftButtonDown <span style="color: #66cc66;">=</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$Window</span>.<span style="color: #003366;">DragMove</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; On_Closing <span style="color: #66cc66;">=</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$Window</span>.<span style="color: #003366;">Resources</span>.<span style="color: #003366;">Timers</span>.<span style="color: #009900;">&quot;Clock&quot;</span>.<span style="color: #003366;">Stop</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; Tag <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$UpdateBlock</span>,<span style="color: #660033; font-weight: bold;">$Interval</span><br />
&nbsp; &nbsp; SizeToContent <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;WidthAndHeight&quot;</span><br />
&nbsp; &nbsp; ResizeMode <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;NoResize&quot;</span><br />
&nbsp; &nbsp; On_SourceInitialized <span style="color: #66cc66;">=</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Window</span>.<span style="color: #003366;">Resources</span>.<span style="color: #003366;">Timers</span>.<span style="color: #009900;">&quot;Clock&quot;</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> Windows.<span style="color: #003366;">Threading</span>.<span style="color: #003366;">DispatcherTimer</span><span style="color: #333;">&#41;</span>.<span style="color: #003366; font-weight: bold;">PSObject</span>.<span style="color: #003366;">BaseObject</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Window</span>.<span style="color: #003366;">Resources</span>.<span style="color: #003366;">Timers</span>.<span style="color: #009900;">&quot;Clock&quot;</span>.<span style="color: #003366;">Interval</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$Window</span>.<span style="color: #003366;">Tag</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #333;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Window</span>.<span style="color: #003366;">Resources</span>.<span style="color: #003366;">Timers</span>.<span style="color: #009900;">&quot;Clock&quot;</span>.<span style="color: #003366;">Add_Tick</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$Window</span>.<span style="color: #003366;">Tag</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Window</span>.<span style="color: #003366;">Resources</span>.<span style="color: #003366;">Timers</span>.<span style="color: #009900;">&quot;Clock&quot;</span>.<span style="color: #003366;">Tag</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$Window</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Window</span>.<span style="color: #003366;">Resources</span>.<span style="color: #003366;">Timers</span>.<span style="color: #009900;">&quot;Clock&quot;</span>.<span style="color: #660033;">Start</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Window</span>.<span style="color: #003366;">Tag</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$Null</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Window</span></span> @WidgetValues <span style="color: #000066;">-Content</span> <span style="color: #660033; font-weight: bold;">$Content</span> <span style="color: #000066;">-Show</span>:<span style="color: #660033; font-weight: bold;">$Show</span> <span style="color: #000066;">-AsJob</span>:<span style="color: #660033; font-weight: bold;">$AsJob</span><br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>That script is what I use to create little &#8220;gadget&#8221; style windows that are transparent, don&#8217;t show up on the taskbar, but are draggable, and have an automatic update based on a timer.  Now, this is a <strong>very</strong> simple version of the gadgets, without using our PowerShell &#8220;DataSource&#8221; or any data binding at all.  I intend to update this with more functionality one of these days, but this was enough to make my clock work. You can see that basically all it does is sets a bunch of parameters for the Window, including a <code>SourceInitialized</code> event handler, and all the stuff necessary to make it transparent and draggable.</p>

	<p>Now let me post the New-TargetClock funtion, which <strong>depends</strong> on that function, and we&#8217;ll get to explaining it a bit down below.  First, the screenshot, this is what it looks like (with a PowerShell window behind it, instead of that bright red wallpaper):</p>

	<p><img src="/images/ShowUI/TargetClock.png" alt="" /></p>

	<p>And then the actual function:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">TargetClock</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #666699; font-weight: bold;">param</span><span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #666699; font-weight: bold;">Switch</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$ShowMarks</span><span style="color: #333;">&#41;</span><br />
<br />
<span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">UIWidget</span></span> <span style="color: #000066;">-AsJob</span> <span style="color: #000066;">-Content</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; Grid <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$now</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Date</span></span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Label</span> <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#123;</span>HorizontalAlignment<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;Center&quot;</span>; VerticalAlignment<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;Top&quot;</span>;FontSize<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">25</span>;ZIndex<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span><span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Hours</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; TextBlock <span style="color: #000066;">-Text</span> <span style="color: #660033;">H</span> <span style="color: #000066;">-Margin</span> <span style="color: #009900;">'0,-5,0,0'</span> <span style="color: #000066;">-FontWeight</span> ExtraBold <span style="color: #000066;">-Foreground</span> White @Label<br />
&nbsp; &nbsp; &nbsp; &nbsp; Ellipse <span style="color: #000066;">-Name</span> Hours <span style="color: #000066;">-Ov</span> el <span style="color: #000066;">-Fill</span> Transparent <span style="color: #000066;">-Stroke</span> Black `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">-StrokeThickness</span> <span style="color: #cc66cc;">100</span> <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">350</span> <span style="color: #000066;">-Height</span> <span style="color: #cc66cc;">350</span> `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">-StrokeDashArray</span> &nbsp;<span style="color: #cc66cc;">7.85</span>, <span style="color: #cc66cc;">7.85</span> &nbsp;<span style="color: #000066;">-Opacity</span> <span style="color: #cc66cc;">0.5</span> `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">-StrokeDashOffset</span> $<span style="color: #333;">&#40;</span><span style="color: #cc66cc;">7.85</span> <span style="color: #66cc66;">-</span> <span style="color: #333;">&#40;</span><span style="color: #cc66cc;">7.85</span> <span style="color: #66cc66;">*</span> <span style="color: #333;">&#40;</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$now</span>.<span style="color: #003366;">Hour</span> <span style="color: #66cc66;">%</span> <span style="color: #cc66cc;">12</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$now</span>.<span style="color: #003366;">Minute</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">60</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">12</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">-RenderTransformOrigin</span> <span style="color: #009900;">&quot;0.5,0.5&quot;</span> <span style="color: #000066;">-RenderTransform</span> <span style="color: #333;">&#123;</span> RotateTransform <span style="color: #000066;">-Angle</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">90</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$animate</span> <span style="color: #66cc66;">=</span> DoubleAnimation <span style="color: #000066;">-To</span> <span style="color: #cc66cc;">0</span> <span style="color: #000066;">-Duration</span> <span style="color: #009900;">&quot;$(11 - ($now.Hour % 12)):$(59 - $now.Minute):$(60 - $now.Second)&quot;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$el</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">BeginAnimation</span><span style="color: #333;">&#40;</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Shapes</span>.<span style="color: #003366;">Shape</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">StrokeDashOffsetProperty</span>, <span style="color: #660033; font-weight: bold;">$animate</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Minute</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; TextBlock <span style="color: #000066;">-Text</span> M &nbsp;<span style="color: #000066;">-Margin</span> <span style="color: #009900;">'0,20,0,0'</span> <span style="color: #000066;">-FontWeight</span> ExtraBold <span style="color: #000066;">-Foreground</span> White @Label<br />
&nbsp; &nbsp; &nbsp; &nbsp; Ellipse <span style="color: #000066;">-Name</span> Minutes <span style="color: #000066;">-Ov</span> el <span style="color: #000066;">-Fill</span> Transparent <span style="color: #000066;">-Stroke</span> Gray `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">-StrokeThickness</span> <span style="color: #cc66cc;">75</span> &nbsp;<span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">300</span> <span style="color: #000066;">-Height</span> <span style="color: #cc66cc;">300</span> `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">-StrokeDashArray</span> &nbsp;<span style="color: #cc66cc;">9.43</span>, <span style="color: #cc66cc;">9.43</span> `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">-StrokeDashOffset</span> $<span style="color: #333;">&#40;</span><span style="color: #cc66cc;">9.43</span> <span style="color: #66cc66;">-</span> <span style="color: #333;">&#40;</span><span style="color: #cc66cc;">9.43</span> <span style="color: #66cc66;">*</span> <span style="color: #333;">&#40;</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$now</span>.<span style="color: #003366;">Minute</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$now</span>.<span style="color: #003366;">Second</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">60</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">60</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">-RenderTransformOrigin</span> <span style="color: #009900;">&quot;0.5,0.5&quot;</span> <span style="color: #000066;">-RenderTransform</span> <span style="color: #333;">&#123;</span> RotateTransform <span style="color: #000066;">-Angle</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">90</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$animate</span> <span style="color: #66cc66;">=</span> DoubleAnimation <span style="color: #000066;">-To</span> <span style="color: #cc66cc;">0</span> <span style="color: #000066;">-Duration</span> <span style="color: #009900;">&quot;00:$(59 - $now.Minute):$(60 - $now.Second)&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$el</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">BeginAnimation</span><span style="color: #333;">&#40;</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Shapes</span>.<span style="color: #003366;">Shape</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">StrokeDashOffsetProperty</span>, <span style="color: #660033; font-weight: bold;">$animate</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Seconds</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; TextBlock <span style="color: #000066;">-Text</span> S <span style="color: #000066;">-Margin</span> <span style="color: #009900;">'0,45,0,0'</span> <span style="color: #000066;">-FontWeight</span> ExtraBold <span style="color: #000066;">-Foreground</span> Gray @Label<br />
&nbsp; &nbsp; &nbsp; &nbsp; Ellipse <span style="color: #000066;">-Name</span> Seconds <span style="color: #000066;">-Ov</span> el <span style="color: #000066;">-Fill</span> Transparent <span style="color: #000066;">-Stroke</span> White `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">-StrokeThickness</span> <span style="color: #cc66cc;">50</span> &nbsp;<span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">250</span> <span style="color: #000066;">-Height</span> <span style="color: #cc66cc;">250</span> `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">-StrokeDashArray</span> <span style="color: #cc66cc;">12.57</span>,<span style="color: #cc66cc;">12.57</span> `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">-StrokeDashOffset</span> $<span style="color: #333;">&#40;</span><span style="color: #cc66cc;">12.57</span> <span style="color: #66cc66;">-</span> <span style="color: #333;">&#40;</span><span style="color: #cc66cc;">12.57</span> <span style="color: #66cc66;">*</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$now</span>.<span style="color: #003366;">Second</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">60</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">-RenderTransformOrigin</span> <span style="color: #009900;">&quot;0.5,0.5&quot;</span> <span style="color: #000066;">-RenderTransform</span> <span style="color: #333;">&#123;</span> RotateTransform <span style="color: #000066;">-Angle</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">90</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$animate</span> <span style="color: #66cc66;">=</span> DoubleAnimation <span style="color: #000066;">-To</span> <span style="color: #cc66cc;">0</span> <span style="color: #000066;">-Duration</span> <span style="color: #009900;">&quot;00:00:$(60 - $now.Second)&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$el</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">BeginAnimation</span><span style="color: #333;">&#40;</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Shapes</span>.<span style="color: #003366;">Shape</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">StrokeDashOffsetProperty</span>, <span style="color: #660033; font-weight: bold;">$animate</span> <span style="color: #333;">&#41;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">## These go in the center of the circles (notice the alignment)</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Centered</span> <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#123;</span>HorizontalAlignment<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;Center&quot;</span>;VerticalAlignment<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;Center&quot;</span><span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; StackPanel @Centered <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TextBlock <span style="color: #000066;">-Name</span> Time <span style="color: #000066;">-FontWeight</span> ExtraBold <span style="color: #000066;">-Foreground</span> Black <span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">30</span> `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">-Text</span> <span style="color: #660033; font-weight: bold;">$Now</span>.<span style="color: #003366;">ToString</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;h:mm&quot;</span><span style="color: #333;">&#41;</span> @Centered<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TextBlock <span style="color: #000066;">-Name</span> Day <span style="color: #000066;">-FontWeight</span> Normal <span style="color: #000066;">-Foreground</span> Black <span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">23</span> `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">-Text</span> <span style="color: #660033; font-weight: bold;">$Now</span>.<span style="color: #003366;">DayOfWeek</span>.<span style="color: #003366;">ToString</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">ToUpper</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> @Centered<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StackPanel <span style="color: #000066;">-Orientation</span> Horizontal @Centered <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TextBlock <span style="color: #000066;">-Name</span> Date <span style="color: #000066;">-FontWeight</span> ExtraBold <span style="color: #000066;">-Foreground</span> Black <span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">18</span> `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">-Text</span> <span style="color: #660033; font-weight: bold;">$Now</span>.<span style="color: #003366;">Day</span> <span style="color: #000066;">-Margin</span> <span style="color: #009900;">'0,0,5,0'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TextBlock <span style="color: #000066;">-Name</span> Month <span style="color: #000066;">-FontWeight</span> ExtraBold <span style="color: #000066;">-Foreground</span> White <span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">18</span> `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">-Text</span> <span style="color: #660033; font-weight: bold;">$Now</span>.<span style="color: #003366;">ToString</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;MMMM&quot;</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">ToUpper</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">## Hour marks</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$ShowMarks</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Ellipse <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">350</span> <span style="color: #000066;">-Height</span> <span style="color: #cc66cc;">350</span> <span style="color: #000066;">-StrokeThickness</span> <span style="color: #cc66cc;">5</span> <span style="color: #000066;">-Fill</span> Transparent <span style="color: #000066;">-Stroke</span> White <span style="color: #000066;">-StrokeDashArray</span> <span style="color: #cc66cc;">0.5</span>,<span style="color: #cc66cc;">17.6</span> <span style="color: #000066;">-RenderTransformOrigin</span> <span style="color: #009900;">&quot;0.5,0.5&quot;</span> <span style="color: #000066;">-RenderTransform</span> <span style="color: #333;">&#123;</span>RotateTransform <span style="color: #000066;">-Angle</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">90.5</span><span style="color: #333;">&#125;</span> <span style="color: #000066;">-Opacity</span> <span style="color: #cc66cc;">0.8</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Ellipse <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">160</span> <span style="color: #000066;">-Height</span> <span style="color: #cc66cc;">160</span> <span style="color: #000066;">-StrokeThickness</span> <span style="color: #cc66cc;">5</span> <span style="color: #000066;">-Fill</span> Transparent <span style="color: #000066;">-Stroke</span> Black <span style="color: #000066;">-StrokeDashArray</span> <span style="color: #cc66cc;">0.5</span>,<span style="color: #cc66cc;">7.62</span> <span style="color: #000066;">-RenderTransformOrigin</span> <span style="color: #009900;">&quot;0.5,0.5&quot;</span> <span style="color: #000066;">-RenderTransform</span> <span style="color: #333;">&#123;</span>RotateTransform <span style="color: #000066;">-Angle</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">90.5</span><span style="color: #333;">&#125;</span> <span style="color: #000066;">-Opacity</span> <span style="color: #cc66cc;">0.8</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-UpdateBlock</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$children</span> <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#123;</span><span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">foreach</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$child</span> <span style="color: #666699; font-weight: bold;">in</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">ChildControl</span></span> <span style="color: #000066;">-Control</span> <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">Tag</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$children</span>.<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$child</span>.<span style="color: #003366;">Name</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$child</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$now</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Date</span></span><br />
<br />
&nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$now</span>.<span style="color: #003366;">Second</span> <span style="color: #000066;">-eq</span> <span style="color: #cc66cc;">0</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$children</span>.<span style="color: #003366;">Seconds</span>.<span style="color: #003366;">Tag</span> <span style="color: #000066;">-ne</span> <span style="color: #660033; font-weight: bold;">$true</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$children</span>.<span style="color: #003366;">Seconds</span>.<span style="color: #003366;">Tag</span> &nbsp;<span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$true</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$animate</span> <span style="color: #66cc66;">=</span> DoubleAnimation <span style="color: #000066;">-From</span> <span style="color: #660033; font-weight: bold;">$children</span>.<span style="color: #003366;">Seconds</span>.<span style="color: #003366;">StrokeDashArray</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span> <span style="color: #000066;">-To</span> <span style="color: #cc66cc;">0</span> <span style="color: #000066;">-Duration</span> <span style="color: #009900;">'00:01'</span> <span style="color: #000066;">-RepeatBehavior</span> <span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>system.<span style="color: #003366;">windows</span>.<span style="color: #003366;">media</span>.<span style="color: #003366;">animation</span>.<span style="color: #003366;">RepeatBehavior</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">Forever</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$children</span>.<span style="color: #003366;">Seconds</span>.<span style="color: #003366;">BeginAnimation</span><span style="color: #333;">&#40;</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Shapes</span>.<span style="color: #003366;">Shape</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">StrokeDashOffsetProperty</span>, <span style="color: #660033; font-weight: bold;">$animate</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$children</span>.<span style="color: #003366;">Seconds</span>.<span style="color: #003366;">Tag</span> &nbsp;<span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$false</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$now</span>.<span style="color: #003366;">Minute</span> <span style="color: #000066;">-eq</span> <span style="color: #cc66cc;">0</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$children</span>.<span style="color: #003366;">Minutes</span>.<span style="color: #003366;">Tag</span> <span style="color: #000066;">-ne</span> <span style="color: #660033; font-weight: bold;">$true</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$children</span>.<span style="color: #003366;">Minutes</span>.<span style="color: #003366;">Tag</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$true</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$animate</span> <span style="color: #66cc66;">=</span> DoubleAnimation <span style="color: #000066;">-From</span> <span style="color: #660033; font-weight: bold;">$children</span>.<span style="color: #003366;">Minutes</span>.<span style="color: #003366;">StrokeDashArray</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span> <span style="color: #000066;">-To</span> <span style="color: #cc66cc;">0</span> <span style="color: #000066;">-Duration</span> <span style="color: #009900;">'01:00'</span> <span style="color: #000066;">-RepeatBehavior</span> <span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>system.<span style="color: #003366;">windows</span>.<span style="color: #003366;">media</span>.<span style="color: #003366;">animation</span>.<span style="color: #003366;">RepeatBehavior</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">Forever</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$children</span>.<span style="color: #003366;">Minutes</span>.<span style="color: #003366;">BeginAnimation</span><span style="color: #333;">&#40;</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Shapes</span>.<span style="color: #003366;">Shape</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">StrokeDashOffsetProperty</span>, <span style="color: #660033; font-weight: bold;">$animate</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$children</span>.<span style="color: #003366;">Minutes</span>.<span style="color: #003366;">Tag</span> &nbsp;<span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$false</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$now</span>.<span style="color: #003366;">Hour</span> <span style="color: #000066;">-eq</span> <span style="color: #cc66cc;">12</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$children</span>.<span style="color: #003366;">Hours</span>.<span style="color: #003366;">Tag</span> <span style="color: #000066;">-ne</span> <span style="color: #660033; font-weight: bold;">$true</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$children</span>.<span style="color: #003366;">Hours</span>.<span style="color: #003366;">Tag</span> &nbsp;<span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$true</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$animate</span> <span style="color: #66cc66;">=</span> DoubleAnimation <span style="color: #000066;">-From</span> <span style="color: #660033; font-weight: bold;">$children</span>.<span style="color: #003366;">Hours</span>.<span style="color: #003366;">StrokeDashArray</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span> <span style="color: #000066;">-To</span> <span style="color: #cc66cc;">0</span> <span style="color: #000066;">-Duration</span> <span style="color: #009900;">'12:00'</span> <span style="color: #000066;">-RepeatBehavior</span> <span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>system.<span style="color: #003366;">windows</span>.<span style="color: #003366;">media</span>.<span style="color: #003366;">animation</span>.<span style="color: #003366;">RepeatBehavior</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">Forever</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$children</span>.<span style="color: #003366;">Hours</span>.<span style="color: #003366;">BeginAnimation</span><span style="color: #333;">&#40;</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Shapes</span>.<span style="color: #003366;">Shape</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">StrokeDashOffsetProperty</span>, <span style="color: #660033; font-weight: bold;">$animate</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$children</span>.<span style="color: #003366;">Hours</span>.<span style="color: #003366;">Tag</span> &nbsp;<span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$false</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$children</span>.<span style="color: #003366;">Time</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$Now</span>.<span style="color: #003366;">ToString</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;h:mm&quot;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$children</span>.<span style="color: #003366;">Day</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$Now</span>.<span style="color: #003366;">DayOfWeek</span>.<span style="color: #003366;">ToString</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">ToUpper</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$children</span>.<span style="color: #003366;">Date</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$Now</span>.<span style="color: #003366;">Day</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$children</span>.<span style="color: #003366;">Month</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$Now</span>.<span style="color: #003366;">ToString</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;MMMM&quot;</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">ToUpper</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>What do you think of that?</p>

	<p>The clever part (which, believe me, took a <span class="caps">LOT</span> of experimenting), is that I&#8217;m creating a StrokeDashArray and animating the StrokeDashOffsetProperty in such a way that the &#8220;stroke&#8221; of the Ellipse (the circle of the clock) gets drawn slowly over time (very slowly, in the case of the hour &#8220;hand&#8221;).  Now, as you can see, I&#8217;m still setting up the animations by hand (I just didn&#8217;t convert the code to use <code>Start-Animation</code>), and in the UpdateBlock, I actually reset the animations if they&#8217;re at zero.  I can write more about this if people have questions, but it&#8217;s nearly 2:30am, so I&#8217;ll take questions tomorrow  <img src='http://joelbennett.net/wordpress/wp-includes/' alt=':)' class='wp-smiley' /> </p>]]></content:encoded>
			<wfw:commentRss>http://joelbennett.net/the-obligatory-showui-clock/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Disabling Events in ShowUI</title>
		<link>http://joelbennett.net/disabling-events-in-showui/</link>
		<comments>http://joelbennett.net/disabling-events-in-showui/#comments</comments>
		<pubDate>Mon, 27 Jun 2011 18:35:20 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[PowerBoots]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[ShowUI]]></category>
		<category><![CDATA[UserInterface]]></category>
		<category><![CDATA[WinForms]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1701</guid>
		<description><![CDATA[Because some things just work better in WPF. Let&#8217;s say you had a form that collected a bunch of user input, and then had a button that would fire off some work. We&#8217;ll assume that you wanted to prevent people from firing off the work again before they know the results of the first time, [...]]]></description>
			<content:encoded><![CDATA[	<h1>Because some things just work better in <span class="caps">WPF</span>.</h1>

	<p>Let&#8217;s say you had a form that collected a bunch of user input, and then had a button that would fire off some work.  We&#8217;ll assume that you wanted to prevent people from firing off the work again before they know the results of the first time, so you&#8217;ll disable the button while you&#8217;re working. Something like this (leaving out the boring stuff about collecting the user&#8217;s input and displaying the results):</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">Import-<span style="font-style: normal;">Module</span></span> ShowUI<br />
<br />
StackPanel <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;TextBlock <span style="color: #000066;">-Name</span> Output<br />
&nbsp; &nbsp;Button <span style="color: #009900;">&quot;Click Me&quot;</span> <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">3</span> <span style="color: #000066;">-On_Click</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">IsEnabled</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$False</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Update the user about what we're doing:</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Output</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">+=</span> <span style="color: #009900;">&quot;We are doing some hard work...<span style="color: #000099; font-weight: bold;">`n</span>&quot;</span><br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Simulate doing some hard work ...</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$times</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$times</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">Sleep</span></span> <span style="color: #000066;">-Seconds</span> <span style="color: #cc66cc;">3</span><br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Let them know about all our hard work</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Output</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">+=</span> <span style="color: #009900;">&quot;Work completed $times time(s).<span style="color: #000099; font-weight: bold;">`n</span>&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">IsEnabled</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$True</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Show</span> <br />
&nbsp;</div>

	<p>It&#8217;s pretty simple. In fact, I think there&#8217;s only three things worth pointing out:</p>

	<ol>
		<li>There are variables for named controls.</li>
		<li>If you want to make sure a control has time to update it&#8217;s display while you&#8217;re executing your event handler, call UpdateLayout().</li>
	</ol>
	<ol>
		<li>You can disable and enable buttons.</li>
	</ol>

	<h3>Now, try doing the same thing with Windows Forms.</h3>

	<p>This was <em>my</em> first attempt:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>void<span style="color: #333;">&#93;</span><span style="color: #333;">&#91;</span>reflection.<span style="color: #003366;">assembly</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">LoadWithPartialName</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;System.Windows.Forms&quot;</span><span style="color: #333;">&#41;</span><br />
<br />
<span style="color: #666666; font-style: italic;"># Create the form</span><br />
<span style="color: #660033; font-weight: bold;">$form</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Forms</span>.<span style="color: #003366;">Form</span><br />
<span style="color: #660033; font-weight: bold;">$form</span>.<span style="color: #003366;">Size</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;250,250&quot;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## Create text</span><br />
<span style="color: #660033; font-weight: bold;">$output</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> system.<span style="color: #003366;">windows</span>.<span style="color: #003366;">forms</span>.<span style="color: #003366;">label</span><br />
<span style="color: #660033; font-weight: bold;">$output</span>.<span style="color: #003366;">Anchor</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Top,Left,Right&quot;</span><br />
<span style="color: #660033; font-weight: bold;">$output</span>.<span style="color: #003366;">AutoSize</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$True</span><br />
<br />
<span style="color: #666666; font-style: italic;">## Create button</span><br />
<span style="color: #660033; font-weight: bold;">$button</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> system.<span style="color: #003366;">windows</span>.<span style="color: #003366;">forms</span>.<span style="color: #003366;">button</span> <br />
<span style="color: #660033; font-weight: bold;">$button</span>.<span style="color: #003366;">Anchor</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Bottom,Right&quot;</span><br />
<span style="color: #660033; font-weight: bold;">$button</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Click Me&quot;</span><br />
<span style="color: #660033; font-weight: bold;">$button</span>.<span style="color: #003366;">Location</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;150,180&quot;</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$button</span>.<span style="color: #003366;">Add_Click</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$button</span>.<span style="color: #003366;">Enabled</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$False</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Output</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">+=</span> <span style="color: #009900;">&quot;We are doing some hard work...<span style="color: #000099; font-weight: bold;">`n</span>&quot;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">## Simulate doing work</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$times</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$times</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">Sleep</span></span> <span style="color: #000066;">-Seconds</span> <span style="color: #cc66cc;">5</span><br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Let them know about all our hard work</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Output</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">+=</span> <span style="color: #009900;">&quot;Work completed $times time(s).<span style="color: #000099; font-weight: bold;">`n</span>&quot;</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$button</span>.<span style="color: #003366;">Enabled</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$True</span><br />
<span style="color: #333;">&#125;</span><span style="color: #333;">&#41;</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$form</span>.<span style="color: #003366;">Controls</span>.<span style="color: #003366;">AddRange</span><span style="color: #333;">&#40;</span>@<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$output</span>, <span style="color: #660033; font-weight: bold;">$button</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## show the form</span><br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>void<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$form</span>.<span style="color: #003366;">showdialog</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp;</div>

	<p>Now, I bet you noticed that took a bit more code than the <span class="caps">WPF</span> version in <a href="http://ShowUI.CodePlex.com">ShowUI</a>, but the thing about it is that I also had to manually specify the positions of the controls (and I had to set the size of the form to make that work). And after all that, it still doesn&#8217;t actually work, and it has a few ugly behaviors:</p>

	<p>First of all, disabling the control doesn&#8217;t work, because Windows posts all click events as messages to the button&#8217;s message queue, and it doesn&#8217;t know that it should ignore them, because it only processes one at a time &#8212; when it&#8217;s done running the Click handler the first time, it notices there&#8217;s another event &#8230; and since it&#8217;s enabled, it processes it.  If you click it five times while it&#8217;s disabled, you&#8217;re going to get the work done five more times&#8230; eventually.</p>

	<p>On top of that, although the label updates without being told to, it didn&#8217;t push the button down the screen or resize the form, so eventually it overlaps the button, passes through the bottom of the form and disappears.</p>

	<p>The next thing I tried was to actually unhook the event handler before I disable the button &#8230; but that had no effect either, because (as I wrote earlier) the button doesn&#8217;t actually ignore the clicks while it&#8217;s doing the work &#8212; it just queues them up for processing later.</p>

	<p>Of course, I could do the work in a background job so that the button would return immediately, but that doesn&#8217;t meet the requirements I have for not queuing up extra work before the first work is done, and in fact, then I&#8217;d have extra work to do to create a time to check and see if the remote job was finished or not.</p>

	<p>[New] Thanks to <a href="http://huddledmasses.org/disabling-events-in-showui/comment-page-1/#comment-215538">SAPIENDavid</a>, I&#8217;ve realized the simple fix for the disabling problem, we just have to call <code>DoEvents</code> to empty the event queue <em>before</em> we re-enable the button.</p>

	<p>Here&#8217;s what <strong>did</strong> work:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">Type</span></span> <span style="color: #000066;">-Assembly</span> System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Forms</span><br />
<br />
<span style="color: #666666; font-style: italic;"># Create the form</span><br />
<span style="color: #660033; font-weight: bold;">$form</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Forms</span>.<span style="color: #003366;">Form</span> <span style="color: #000066;">-Property</span> @<span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; Size <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;200,70&quot;</span><br />
&nbsp; &nbsp; AutoSize <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$true</span> <br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;"># Create a FlowLayout</span><br />
<span style="color: #660033; font-weight: bold;">$panel</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Forms</span>.<span style="color: #003366;">FlowLayoutPanel</span> <span style="color: #000066;">-Property</span> @<span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; Dock <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Fill&quot;</span><br />
&nbsp; &nbsp; FlowDirection <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;TopDown&quot;</span><br />
&nbsp; &nbsp; AutoSize <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$true</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## Create text</span><br />
<span style="color: #660033; font-weight: bold;">$output</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> system.<span style="color: #003366;">windows</span>.<span style="color: #003366;">forms</span>.<span style="color: #003366;">label</span> <span style="color: #000066;">-Property</span> @<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; Dock <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Fill&quot;</span><br />
&nbsp; &nbsp; Text <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Click the button when you're ready to work.<span style="color: #000099; font-weight: bold;">`n</span>&quot;</span><br />
&nbsp; &nbsp; AutoSize <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$true</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## Create button</span><br />
<span style="color: #660033; font-weight: bold;">$button</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> system.<span style="color: #003366;">windows</span>.<span style="color: #003366;">forms</span>.<span style="color: #003366;">button</span> <span style="color: #000066;">-Property</span> @<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; Anchor <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Bottom,Right&quot;</span><br />
&nbsp; &nbsp; Text <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Click Me&quot;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$lastButtonClick</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">get-<span style="font-style: normal;">date</span></span><br />
<span style="color: #660033; font-weight: bold;">$button</span>.<span style="color: #003366;">Add_Click</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$button</span>.<span style="color: #003366;">Enabled</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$False</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Output</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">+=</span> <span style="color: #009900;">&quot;We are doing some hard work...<span style="color: #000099; font-weight: bold;">`n</span>&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">## Simulate doing work</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$times</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$times</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">Sleep</span></span> <span style="color: #000066;">-Seconds</span> <span style="color: #cc66cc;">5</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Let them know about all our hard work</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Output</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">+=</span> <span style="color: #009900;">&quot;Work completed $times time(s).<span style="color: #000099; font-weight: bold;">`n</span>&quot;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">#Process the pending messages before enabling the button</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Forms</span>.<span style="color: #003366;">Application</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">DoEvents</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$button</span>.<span style="color: #003366;">Enabled</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$True</span><br />
<span style="color: #333;">&#125;</span><span style="color: #333;">&#41;</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$panel</span>.<span style="color: #003366;">Controls</span>.<span style="color: #003366;">AddRange</span><span style="color: #333;">&#40;</span>@<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$output</span>, <span style="color: #660033; font-weight: bold;">$button</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><br />
<span style="color: #660033; font-weight: bold;">$form</span>.<span style="color: #003366;">Controls</span>.<span style="color: #003366;">AddRange</span><span style="color: #333;">&#40;</span>@<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$panel</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## show the form</span><br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>void<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$form</span>.<span style="color: #003366;">showdialog</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp;</div>

	<p>To fix the other problems, I added a FlowLayoutPanel (which is very different from the StackPanel in <span class="caps">WPF</span>, but still serves the same purpose), and made all the relevant bits autosize (it&#8217;s always surprising to me when I need to drop back to WinForms, how everything has to be <strong>told</strong> to autosize and fill empty space). </p>

	<p>That&#8217;s enough to take care of the output problem, and make the two solutions roughly equivalent (there&#8217;s still some differences, as you can see if you run them).</p>

	<p>For what it&#8217;s worth, this was a real world question from a user at our <a href="http://PowerShellGroup.org/virtual/live">Virtual User Group</a> this morning, and I just couldn&#8217;t help sharing how much easier user interfaces are to write in ShowUI. Clearly a designer like PrimalForms makes laying out the controls easier &#8212; but when it comes to the little things, you still have to figure out to make them work, and actually implement your event handlers correctly.</p>

	<p>Just for the record, here&#8217;s what it would take to implement the <span class="caps">WPF</span> solution without ShowUI:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">Type</span></span> <span style="color: #000066;">-Assembly</span> PresentationFramework<br />
<span style="color: #660033; font-weight: bold;">$window</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Window</span> <span style="color: #000066;">-Property</span> @<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; SizeToContent <span style="color: #66cc66;">=</span> <span style="color: #009900;">'WidthAndHeight'</span><br />
&nbsp; &nbsp; Content <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Controls</span>.<span style="color: #003366;">StackPanel</span><br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #660033; font-weight: bold;">$output</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Controls</span>.<span style="color: #003366;">TextBlock</span><br />
<span style="color: #660033; font-weight: bold;">$button</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Controls</span>.<span style="color: #003366;">Button</span> <span style="color: #000066;">-Property</span> @<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; Content <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Click Me&quot;</span><br />
&nbsp; &nbsp; Margin <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">3</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$button</span>.<span style="color: #003366;">Add_Click</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">IsEnabled</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$False</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Update the user about what we're doing:</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Output</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">+=</span> <span style="color: #009900;">&quot;We are doing some hard work...<span style="color: #000099; font-weight: bold;">`n</span>&quot;</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Window</span>.<span style="color: #003366;">UpdateLayout</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Simulate doing some hard work ...</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$times</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$times</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">Sleep</span></span> <span style="color: #000066;">-Seconds</span> <span style="color: #cc66cc;">3</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Let them know about all our hard work</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Output</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">+=</span> <span style="color: #009900;">&quot;Work completed $times time(s).<span style="color: #000099; font-weight: bold;">`n</span>&quot;</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">IsEnabled</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$True</span><br />
<span style="color: #333;">&#125;</span><span style="color: #333;">&#41;</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$window</span>.<span style="color: #003366;">Content</span>.<span style="color: #003366;">Children</span>.<span style="color: #003366;">Add</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$output</span><span style="color: #333;">&#41;</span><br />
<span style="color: #660033; font-weight: bold;">$window</span>.<span style="color: #003366;">Content</span>.<span style="color: #003366;">Children</span>.<span style="color: #003366;">Add</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$button</span><span style="color: #333;">&#41;</span><br />
<span style="color: #660033; font-weight: bold;">$window</span>.<span style="color: #003366;">ShowDialog</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp;</div>]]></content:encoded>
			<wfw:commentRss>http://joelbennett.net/disabling-events-in-showui/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>ShowUI: Handling Events and Producing Output</title>
		<link>http://joelbennett.net/showui-handling-events-and-producing-output/</link>
		<comments>http://joelbennett.net/showui-handling-events-and-producing-output/#comments</comments>
		<pubDate>Mon, 20 Jun 2011 05:22:10 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[PowerBoots]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[ShowUI]]></category>
		<category><![CDATA[UserInterface]]></category>
		<category><![CDATA[WinForms]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1694</guid>
		<description><![CDATA[Once you get past the basics of WPF and ShowUI, learning to use nested panels or grids to achieve the layouts you want, and start getting a grip on what controls are available by default, the next step to building useful user interfaces is going to be handling user interactions. In programming, we call those [...]]]></description>
			<content:encoded><![CDATA[	<p>Once you get past the basics of <span class="caps">WPF</span> and <a href="http://showui.codeplex.com/">ShowUI</a>, learning to use nested panels or grids to achieve the layouts you want, and start getting a grip on what controls are available by default, the next step to building useful user interfaces is going to be handling user interactions.</p>

	<p>In programming, we call those interactions &#8220;events.&#8221; An event in <span class="caps">WPF</span> covers all forms of user interactions like clicking a button, type in a textbox, or select items from a treeview or listbox, and even less obvious things like when the right or left mouse button get&#8217;s pressed down, or let up &#8230; or when the mouse moves, of when the control gets focus or looses it, or &#8220;touch&#8221; and &#8220;stylus&#8221; events, drag-and-drop events, etc.  There are also events that aren&#8217;t caused by users, like events for when databinding is updated, when the control is initialized, hidden, made visible, etc.</p>

	<p>In ShowUI, all events are handled by assigning scriptblocks to a parameter who&#8217;s name starts with &#8220;On_&#8221; like -On_Click or -On_GotFocus or -On_MouseLeftButtonDown or -On_TextInput &#8230; and so on.</p>

	<p>Let&#8217;s say that you want a quick dialog like this:</p>

	<p><img src="http://huddledmasses.org/images/ShowUI/ShowUI-10.png" class="float-right-block" alt="" width="225" height="118" /></p>

	<p>You&#8217;re going to need to handle the OK button click, of course, but in that scriptblock, you&#8217;re going to want to get the text from the textbox, and make sure that it gets returned when the window is closed &#8230; and you&#8217;re going to want to close the Window! </p>

	<p>We&#8217;re here to help! Within the event handler script blocks, ShowUI defines a bunch of variables for you to help you handle the event: <code>$this</code> is the source of the event, <code>$_</code> is the event arguments, and <code>$window</code> is the top-level window that contains your UI. Any named controls in your script are also exposed as variables, so if you started with this script:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
StackPanel <span style="color: #000066;">-ControlName</span> <span style="color: #009900;">&quot;Prompt&quot;</span> <span style="color: #000066;">-Margin</span> <span style="color: #009900;">&quot;8,0,8,8&quot;</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; Label <span style="color: #009900;">&quot;Please Enter Your Full Name:&quot;</span><br />
&nbsp; &nbsp; StackPanel <span style="color: #000066;">-Orientation</span> Horizontal <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; TextBox <span style="color: #000066;">-Name</span> FullName <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">100</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Button <span style="color: #009900;">&quot;OK&quot;</span> <span style="color: #000066;">-IsDefault</span> <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">50</span> <span style="color: #000066;">-Margin</span> <span style="color: #009900;">&quot;8,0,0,0&quot;</span> <span style="color: #000066;">-On_Click</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Do something to output the name!</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Show</span><br />
&nbsp;</div>

	<p>You&#8217;re going to be able to get the text from the TextBox using <code>$FullName.Text</code> because you know the TextBox has a property named &#8220;Text&#8221; (since it&#8217;s exposed as a parameter for you in the TextBox command), and because now you know that ShowUI creates a variable for all the named controls.</p>

	<p>In order to write output from a script like that one, you have to set the <code>Tag</code> property on the top level control (in this case, the StackPanel, which is obviously named &#8220;Prompt&#8221;). You can do that easily by hand, or you can use the Set-UIValue function.</p>

	<p>In order to close the window, you&#8217;re going to have to do one of two things: first, you can use the handy <code>Close-Control</code> function, or you can call the Close method on the window. The Close-Control function will look at the &#8220;parent&#8221; (and it&#8217;s parent) to try and find the window that needs to be closed &#8212; but if it can&#8217;t find one, it will just <strong>hide</strong> the parent, so if your button were several layers deep (unlike ours), you&#8217;d have to specify the top level control as a parameter.</p>

	<p>Here&#8217;s two versions of what it could have looked like when I was finished:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
StackPanel <span style="color: #000066;">-ControlName</span> <span style="color: #009900;">&quot;Prompt&quot;</span> <span style="color: #000066;">-Margin</span> <span style="color: #009900;">&quot;8,0,8,8&quot;</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; Label <span style="color: #009900;">&quot;Please Enter Your Full Name:&quot;</span><br />
&nbsp; &nbsp; StackPanel <span style="color: #000066;">-Orientation</span> Horizontal <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; TextBox <span style="color: #000066;">-Name</span> FullName <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">100</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Button <span style="color: #009900;">&quot;OK&quot;</span> <span style="color: #000066;">-IsDefault</span> <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">50</span> <span style="color: #000066;">-Margin</span> <span style="color: #009900;">&quot;8,0,0,0&quot;</span> <span style="color: #000066;">-On_Click</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Prompt</span>.<span style="color: #003366;">Tag</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$FullName</span>.<span style="color: #003366;">Text</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Window</span>.<span style="color: #003366;">Close</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-On_Loaded</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$FullName</span>.<span style="color: #003366;">Focus</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span> <span style="color: #000066;">-Show</span><br />
&nbsp;</div>

	<div class="posh code posh" style="font-family:monospace;"><br />
StackPanel <span style="color: #000066;">-ControlName</span> <span style="color: #009900;">&quot;Prompt&quot;</span> <span style="color: #000066;">-Margin</span> <span style="color: #009900;">&quot;8,0,8,8&quot;</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; Label <span style="color: #009900;">&quot;Please Enter Your Full Name:&quot;</span><br />
&nbsp; &nbsp; StackPanel <span style="color: #000066;">-Orientation</span> Horizontal <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; TextBox <span style="color: #000066;">-Name</span> FullName <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">100</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Button <span style="color: #009900;">&quot;OK&quot;</span> <span style="color: #000066;">-IsDefault</span> <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">50</span> <span style="color: #000066;">-Margin</span> <span style="color: #009900;">&quot;8,0,0,0&quot;</span> <span style="color: #000066;">-On_Click</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">UIValue</span></span> <span style="color: #660033; font-weight: bold;">$Prompt</span> <span style="color: #000066;">-Passthru</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Close-<span style="font-style: normal;">Control</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-On_Loaded</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$FullName</span>.<span style="color: #003366;">Focus</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span> <span style="color: #000066;">-Show</span><br />
&nbsp;</div>

	<p>One thing you&#8217;ll notice right away is that I cheated and actually added another event handler too: For the &#8220;Loaded&#8221; event on the StackPanel.  This event handler is called during the initialization of the user interface, and gives you a chance to do things like what I did here: set the initial keyboard focus where you want it (so the user can start typing as soon as the window pops up).</p>

	<p>However, if you run them both, you&#8217;ll notice another thing: the output is different.  In the second example I took advantage of the fact that Set-UIValue will actually call Get-UIValue if you don&#8217;t pass it a parameter!  The cool thing about Get-UIValue is that if it doesn&#8217;t find a &#8220;Tag&#8221; on the specified control, it will look through the children to find one, and create a hashtable out of the values it finds. So in this case, rather than write the code to get the value from the right textbox and set it myself, I just let the built-in features of ShowUI do their thing.</p>

	<h3>A bigger example</h3>

	<p>Of course, in neither example did I need to do anything with the button or with the actual parameters that are passed in to the button&#8217;s &#8220;Click&#8221; event &#8230; so perhaps one last (more complicated) example would be useful:</p>

	<p><img src="http://huddledmasses.org/images/ShowUI/ShowUI-11.png" class="float-right-block" alt="" width="185" height="167" /></p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Grid</span></span> <span style="color: #000066;">-ControlName</span> SelectUserGroups <span style="color: #000066;">-Columns</span> Auto,<span style="color: #66cc66;">*</span> <span style="color: #000066;">-Rows</span> <span style="color: #cc66cc;">4</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$GetGroups</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$user</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">QADUuser</span></span> <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">Text</span> <span style="color: #000066;">-SizeLimit</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$User</span>.<span style="color: #003366;">LogonName</span> <span style="color: #000066;">-eq</span> <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">Text</span> <span style="color: #000066;">-or</span> <span style="color: #660033; font-weight: bold;">$User</span>.<span style="color: #003366;">Email</span> <span style="color: #000066;">-eq</span> <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">Text</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">Foreground</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Black&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Group</span>.<span style="color: #003366;">ItemsSource</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">QADGroup</span></span> <span style="color: #000066;">-ContainsMember</span> <span style="color: #660033; font-weight: bold;">$user</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$UserName</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$user</span>.<span style="color: #003366;">LogonName</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$EmailAddress</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$user</span>.<span style="color: #003366;">Email</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">Foreground</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Red&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Group</span>.<span style="color: #003366;">ItemsSource</span> <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Label</span></span> <span style="color: #009900;">&quot;Name&quot;</span><br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Textbox</span></span> <span style="color: #000066;">-name</span> UserName <span style="color: #000066;">-minwidth</span> <span style="color: #cc66cc;">100</span> <span style="color: #000066;">-Column</span> <span style="color: #cc66cc;">1</span> <span style="color: #000066;">-On_LostFocus</span> <span style="color: #660033; font-weight: bold;">$GetGroups</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Label</span></span> <span style="color: #009900;">&quot;Email&quot;</span> <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Textbox</span></span> <span style="color: #000066;">-name</span> EmailAddress <span style="color: #000066;">-minwidth</span> <span style="color: #cc66cc;">100</span> &nbsp;<span style="color: #000066;">-Column</span> <span style="color: #cc66cc;">1</span> <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">1</span> &nbsp;<span style="color: #000066;">-On_LostFocus</span> <span style="color: #660033; font-weight: bold;">$GetGroups</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Label</span></span> <span style="color: #009900;">&quot;Group&quot;</span> <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">2</span><br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Listbox</span></span> <span style="color: #000066;">-name</span> <span style="color: #660033;">Group</span> <span style="color: #000066;">-Column</span> <span style="color: #cc66cc;">1</span> <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">2</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Button</span></span> <span style="color: #009900;">&quot;OK&quot;</span> <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">3</span> <span style="color: #000066;">-Column</span> <span style="color: #cc66cc;">1</span> <span style="color: #000066;">-On_Click</span> <span style="color: #333;">&#123;</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">ParentControl</span></span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">UIValue</span></span> <span style="color: #000066;">-Passthru</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Close-<span style="font-style: normal;">Control</span></span> <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Show</span><br />
&nbsp;</div>

	<p>Hopefully, by now this doesn&#8217;t need a whole lot of explanation, but let&#8217;s walk through it anyway.  First of all, if you&#8217;re not familiar with them, this script uses the excellent <a href="http://www.quest.com/powershell/activeroles-server.aspx">PowerShell Commands for Active Directory</a> from Quest software.  This doesn&#8217;t represent a full, complete, useful user interface &#8212; it&#8217;s more of an example that you can hopefully work from (and please, contribute ammendments <a href="http://poshcode.org/2737">on PoshCode</a>).</p>

	<p>You can see that I used a Grid with four rows and two Columns: with one column &#8220;Auto&#8220;sized, and one using up all the rest of the UI (you can resize the window to make the fields bigger).  The first thing in the grid is the definition of a scripblock which I assigned to a variable <code>$GetGroups</code>.  The reason I did that is because I wanted to reuse the scriptblock as the event handler for both the UserName and EmailAddress fields.</p>

	<p>Without dwelling a whole lot on it, you can see the last line is the &#8220;OK&#8221; button which get&#8217;s the parent grid and calls Set-UIValue to invoke, as before, the hashtable collection of all the textbox values.</p>

	<p>The interesting stuff is in that GetGroup event handler:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$user</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">QADUuser</span></span> <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">Text</span> <span style="color: #000066;">-SizeLimit</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$User</span>.<span style="color: #003366;">LogonName</span> <span style="color: #000066;">-eq</span> <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">Text</span> <span style="color: #000066;">-or</span> <span style="color: #660033; font-weight: bold;">$User</span>.<span style="color: #003366;">Email</span> <span style="color: #000066;">-eq</span> <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">Text</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">Foreground</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Black&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Group</span>.<span style="color: #003366;">ItemsSource</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">QADGroup</span></span> <span style="color: #000066;">-ContainsMember</span> <span style="color: #660033; font-weight: bold;">$user</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$UserName</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$user</span>.<span style="color: #003366;">LogonName</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$EmailAddress</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$user</span>.<span style="color: #003366;">Email</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">Foreground</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Red&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Group</span>.<span style="color: #003366;">ItemsSource</span> <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>You can see that first we call <code>Get-QADUser</code> with the Text of <code>$this</code> field.  By using <code>$this</code>, we make it so the event handler will work on both Textboxes, since it will get the text of whatever triggered the event handler.  Get-QADUser doesn&#8217;t return anything unless it finds a user, and setting the SizeLimit ensures that we won&#8217;t end up waiting for it to retrieve &#8220;all&#8221; the users just because the Textbox was left empty.  In fact, the point of that line is to make sure that we matched a user.</p>

	<p>On the next line, I&#8217;m making sure that either the LogonName or Email of the user that we found matches fully the text that the user typed. This makes sure that the user that we matched is a full match, so we know we&#8217;ve gotten the person typing into the form to type a full, complete username or email address.</p>

	<p>When it does match, we set the color to Black (in case it was an error and set to Red before), and we call Get-QADGroup to get all the groups that the user is a member of.  We set those groups as the source for the <code>$Group</code> listbox, and they&#8217;ll immediately show up for the user.  And finally, we update the few fields we&#8217;re showing from the user object we retrieved earlier. </p>

	<p>Of course, when it doesn&#8217;t match, we set the text red to indicate an error, and then we zero out the data on the group listbox.</p>

	<p>I hope this has helped some of you figure out event handlers in ShowUI &#8212; please feel free to ask questions in the comments below or on the <a href="http://showui.codeplex.com/discussions">ShowUI discussion boards</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://joelbennett.net/showui-handling-events-and-producing-output/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

