<?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; PowerShell</title>
	<atom:link href="http://huddledmasses.org/tag/powershell/feed/" rel="self" type="application/rss+xml" />
	<link>http://huddledmasses.org</link>
	<description>You can do more than breathe for free...</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='huddledmasses.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>Rich formatting for PowerShell help</title>
		<link>http://huddledmasses.org/rich-formatting-for-powershell-help/</link>
		<comments>http://huddledmasses.org/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://huddledmasses.org/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://huddledmasses.org/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://huddledmasses.org/did-you-know-powershell-can-use-selenium/</link>
		<comments>http://huddledmasses.org/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://huddledmasses.org/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://huddledmasses.org/powershell-3-finally-on-the-dlr/</link>
		<comments>http://huddledmasses.org/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://huddledmasses.org/wordpress/wp-includes/' alt=';-)' class='wp-smiley' /> </p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/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://huddledmasses.org/arrange-act-assert-intuitive-testing/</link>
		<comments>http://huddledmasses.org/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://huddledmasses.org/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://huddledmasses.org/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://huddledmasses.org/arrange-act-assert-intuitive-testing/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Working with multiple versions of PowerShell Modules</title>
		<link>http://huddledmasses.org/working-with-multiple-versions-of-powershell-modules/</link>
		<comments>http://huddledmasses.org/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://huddledmasses.org/working-with-multiple-versions-of-powershell-modules/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Disabling Events in ShowUI</title>
		<link>http://huddledmasses.org/disabling-events-in-showui/</link>
		<comments>http://huddledmasses.org/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://huddledmasses.org/disabling-events-in-showui/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>ShowUI: Handling Events and Producing Output</title>
		<link>http://huddledmasses.org/showui-handling-events-and-producing-output/</link>
		<comments>http://huddledmasses.org/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://huddledmasses.org/showui-handling-events-and-producing-output/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>ShowUI: the tutorial walkthrough</title>
		<link>http://huddledmasses.org/showui-tutorial-walkthrough/</link>
		<comments>http://huddledmasses.org/showui-tutorial-walkthrough/#comments</comments>
		<pubDate>Wed, 15 Jun 2011 04:44:07 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[PowerBoots]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[ShowUI]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[UserInterface]]></category>
		<category><![CDATA[WalkThrough]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1679</guid>
		<description><![CDATA[Ok, let&#8217;s be clear: you&#8217;ve seen this before, and this isn&#8217;t going to be the last you&#8217;ll see of it, because we&#8217;ve got much more in store in ShowUI, but for the first release of ShowUI, it&#8217;s obviously time to update this simple walkthrough of building simple user interfaces in PowerShell! An introduction to ShowUI [...]]]></description>
			<content:encoded><![CDATA[	<p>Ok, let&#8217;s be clear: you&#8217;ve seen this <a href="/powerboots-tutorial-walkthrough">before</a>, and this isn&#8217;t going to be the last you&#8217;ll see of it, because we&#8217;ve got much more in store in <a href="http://showui.codeplex.com">ShowUI</a>, but for the first release of ShowUI, it&#8217;s obviously time to update this simple walkthrough of building simple user interfaces in PowerShell!</p>

	<h2>An introduction to ShowUI</h2>

	<p>ShowUI is the next generation PowerShell module for building user interfaces in script. It&#8217;s the merger of my previous PowerShell UI project (called PowerBoots) with one by James Brundage, former Microsoft PowerShell team member and founder of <a href="http://start-automating.com">Start-Automating</a> (called <span class="caps">WPK</span>) which shipped in the Windows 7 resource kit.</p>

	<h4>If you want to follow along, you need to:</h4>

	<p>1. Get a copy of the <a href="http://showui.codeplex.com/releases/">ShowUI Module</a> from CodePlex <br />
2. Install it by putting the &#8220;ShowUI&#8221; folder in one of your &#8220;Modules&#8221; folders (list them by typing <code>$Env:PSMODULEPATH</code> in PowerShell v2, and feel free to create the one you want if it doesn&#8217;t exist). You should end up with something like <code>C:\Users\Jaykul\Documents\WindowsPowerShell\Modules\ShowUI\ShowUI.psm1</code><br />
3. Run PowerShell <span class="caps">ISE</span>, or use PowerShell.exe with the -<span class="caps">STA</span> switch (the best way to do this is to add it to the shortcut you use to launch PowerShell: open the properties dialog, and on the Shortcut tab, add &#8220; -STA&#8221; to the Target)<sup class="footnote"><a href="#fn1">1</a></sup>.</p>

	<p>Did I hear someone ask <strong>what <em>is</em> WPF?</strong> It was introduced as part of .Net 3.0 (and vastly improved in .Net 3.5, and again in 4.0), so you can expect to find it preinstalled on computers from Vista on, and of course you can download and install it on XP if it&#8217;s not already installed.  The only thing you really need to know about <span class="caps">WPF</span> for the purposes of this tutorial is that it is <strong>the</strong> new <span class="caps">GUI</span> toolkit for .Net, and that it is container based &#8212; you put elements into other elements to control the layout, rather like <span class="caps">HTML</span> and Java Swing&#8230; you can <strong>pick up the rest as we go along</strong>.</p>

	<h2>A simple ShowUI program</h2>

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

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Button</span></span> <span style="color: #000066;">-Content</span> <span style="color: #009900;">&quot;Hello World&quot;</span> <span style="color: #000066;">-Show</span><br />
&nbsp;</div>

	<p>This first example is a little bit more verbose than it needs to be, because the <code>-Content</code> parameter is positional, so the first non-named argument you pass will be used for that. The same is true for the -Children parameter of panels, and in fact, each of the other similar content parameters: Items, Blocks, and Inlines.</p>

	<p>Additionally, each control is aliased without the New- verb, so you could just call <code>Button</code> instead of <code>New-Button</code> &#8230; and of course, since our button doesn&#8217;t do anything, we could just as easily have used a Label, and written:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Label <span style="color: #009900;">&quot;Hello World&quot;</span> <span style="color: #000066;">-Show</span><br />
&nbsp;</div>

	<p>Note: &#8220;Label&#8221; is also the name of an executable for labelling drives in Windows, make sure ShowUI is imported before you run that command.</p>

	<p>One last point: ShowUI brings a lot of features from <span class="caps">WPK</span> to the table, and one you&#8217;ll use a lot is the ability to skip specifying the window and put the -Show parameter on almost any <span class="caps">WPF</span> element. This is partly because we&#8217;re back to running PowerShell 2.0 with the -<span class="caps">STA</span> switch, but in any case, we can now skip the &#8220;New-BootsWindow&#8221; command, and our examples can be that much simpler.</p>

	<h2>We can put controls in a stack</h2>

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

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">Show-<span style="font-style: normal;">UI</span></span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; StackPanel <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Button <span style="color: #009900;">&quot;A bed of clams&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Button <span style="color: #009900;">&quot;A coalition of cheetas&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Button <span style="color: #009900;">&quot;A gulp of swallows&quot;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>StackPanels are awesome. So are WrapPanels. Try that code with a WrapPanel instead of a StackPanel and see what the difference is (hint: try resizing the window). Actually, try it with a UniformGrid too. <span class="caps">WPF</span> has several other panel containers too: Grid, ToolBarPanel, TabPanel, DockPanel, Canvas &#8230; we&#8217;ll talk about some more of those later.</p>

	<h2>Ok, lets see some formatting</h2>

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

	<p>To scoot the buttons out from the edge we can use margins or padding: margins go on the outside of containers, padding goes on the inside. We can also specify the FontFamily, FontSize, FontWeight, and FontStyle, as well as Foreground and Background colors &#8230; </p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">Show-<span style="font-style: normal;">UI</span></span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; StackPanel <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">5</span> <span style="color: #000066;">-Background</span> Pink <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Button <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">2</span> <span style="color: #009900;">&quot;A bed of clams&quot;</span> <span style="color: #000066;">-FontFamily</span> Consolas <span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">24</span> <span style="color: #000066;">-FontWeight</span> Bold<br />
&nbsp; &nbsp; &nbsp; &nbsp; Button <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">2</span> <span style="color: #009900;">&quot;A coalition of cheetas&quot;</span> <span style="color: #000066;">-FontFamily</span> Arial <span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">20</span> <span style="color: #000066;">-FontStyle</span> Italic<br />
&nbsp; &nbsp; &nbsp; &nbsp; Button <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">2</span> <span style="color: #009900;">&quot;A gulp of swallows&quot;</span> <span style="color: #000066;">-FontFamily</span> <span style="color: #009900;">'Segoe UI'</span> <span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">18</span> <span style="color: #000066;">-Foreground</span> Crimson<br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>So you see, the pink background is on the StackPanel, which has a (default, white) margin around it.  If you wanted the whole background of the window to be pink, you would need to set the background of the Window instead of the StackPanel.</p>

<h3 style="clear: both;">An aside on Typography</h3>

	<p>ShowUI doesn&#8217;t need to create a full set of typography-specific top-level elements the way Shoes does, because we are based on <span class="caps">WPF</span>, which has a far more powerful typography system available than any we&#8217;ve used previously. So, with <span class="caps">WPF</span> we start by selecting a control based on how much text you want to put in it, and how much formatting you want to apply: Label and TextBox are the simplest, TextBlock supports limited text formattings, and FlowDocumentViewer or RichTextBox support full rich content. And of course, Hyperlink supports clicking  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';)' class='wp-smiley' />  </p>

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

	<p>For the typography elements, the content model changes a little bit.  There are basically two types of typographical elements: Inline and Block elements (where inline elements can&#8217;t contain block elements). In fact, he <a href="http://msdn.microsoft.com/en-us/library/bb613554.aspx">TextBlock Content Model</a> is similar to that of an inline element. It is actually a type-restricted &#8220;Items&#8221; container.  Instead of being able to have <em>anything</em> as content, it can only contain <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.inline.aspx">Inline</a> flow content elements such as <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.anchoredblock.aspx">AnchoredBlock</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.bold.aspx">Bold</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.hyperlink.aspx">Hyperlink</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.inlineuicontainer.aspx">InlineUIContainer</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.italic.aspx">Italic</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.linebreak.aspx">LineBreak</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.run.aspx">Run</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.span.aspx">Span</a>, and <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.underline.aspx">Underline</a>, and it will create a run automatically if you just put a text string in it. </p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">Show-<span style="font-style: normal;">UI</span></span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;StackPanel <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">10</span> <span style="color: #000066;">-Children</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; TextBlock <span style="color: #009900;">&quot;A Question&quot;</span> <span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">42</span> <span style="color: #000066;">-FontWeight</span> Bold <span style="color: #000066;">-Foreground</span> <span style="color: #009900;">&quot;#FF0088&quot;</span> <br />
&nbsp; &nbsp; &nbsp; TextBlock <span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">24</span> <span style="color: #000066;">-Inlines</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Bold <span style="color: #009900;">&quot;Q. &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&quot;Are you starting to dig &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Hyperlink <span style="color: #009900;">&quot;ShowUI?&quot;</span> <span style="color: #000066;">-NavigateUri</span> http:<span style="color: #66cc66;">//</span>huddledmasses.<span style="color: #003366;">org</span><span style="color: #66cc66;">/</span>tag<span style="color: #66cc66;">/</span>showui<span style="color: #66cc66;">/</span> `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">-On_RequestNavigate</span> <span style="color: #333;">&#123;</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Diagnostics.<span style="color: #666699; font-weight: bold;">Process</span><span style="color: #333;">&#93;</span></span>::<span style="color: #660033;">Start</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">NavigateUri</span>.<span style="color: #003366;">ToString</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; TextBlock <span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">16</span> <span style="color: #000066;">-Inlines</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Span <span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">24</span> <span style="color: #000066;">-FontWeight</span> Bold <span style="color: #000066;">-Inlines</span> <span style="color: #009900;">&quot;A. &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&quot;Leave me alone, I'm hacking here!&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>Note: If you want support for the full <a href="http://msdn.microsoft.com/en-us/library/aa970909.aspx">document model</a> (which allows Paragraphs and Lists), you need to use a <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.flowdocumentreader.aspx">FlowDocumentReader</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.flowdocumentpageviewer.aspx">FlowDocumentPageViewer</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox.aspx">RichTextBox</a>, or a <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.flowdocumentscrollviewer.aspx">FlowDocumentScrollViewer</a> ... there&#8217;s lots more information about those <a href="http://msdn.microsoft.com/en-us/library/aa970909.aspx">on msdn</a>.</p>

	<h2>Time for some artwork</h2>

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

	<div class="posh code posh" style="font-family:monospace;"><br />
Ellipse <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">60</span> <span style="color: #000066;">-Height</span> <span style="color: #cc66cc;">80</span> <span style="color: #000066;">-Margin</span> <span style="color: #009900;">&quot;20,10,60,20&quot;</span> <span style="color: #000066;">-Fill</span> Black <span style="color: #000066;">-Show</span><br />
&nbsp;</div>

	<p>In <span class="caps">WPF</span>, everything always starts out white, and you must position things based on the container. You can see that the Margin can be specified as a single value as in the previous example, or as separate values for Left, Top, Right, Bottom.  Oddly, to satisfy PowerShell&#8217;s type-casting, you have to quote them so they&#8217;re a single comma-separated string, instead of four separate values.</p>

	<h2>Some more advanced drawing</h2>

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

	<div class="posh code posh" style="font-family:monospace;"><br />
Canvas <span style="color: #000066;">-Height</span> <span style="color: #cc66cc;">100</span> <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">100</span> <span style="color: #000066;">-Children</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;Rectangle <span style="color: #000066;">-Margin</span> <span style="color: #009900;">&quot;10,10,0,0&quot;</span> <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">45</span> <span style="color: #000066;">-Height</span> <span style="color: #cc66cc;">45</span> <span style="color: #000066;">-Stroke</span> <span style="color: #009900;">&quot;#689945&quot;</span> <span style="color: #000066;">-StrokeThickness</span> <span style="color: #cc66cc;">2</span> <span style="color: #000066;">-Fill</span> <span style="color: #009900;">&quot;#336699&quot;</span><br />
&nbsp; &nbsp;Polygon <span style="color: #000066;">-Stroke</span> Pink <span style="color: #000066;">-StrokeThickness</span> <span style="color: #cc66cc;">2</span> <span style="color: #000066;">-Fill</span> DarkRed <span style="color: #000066;">-Points</span> <span style="color: #009900;">&quot;10,60&quot;</span>, <span style="color: #009900;">&quot;50,60&quot;</span>, <span style="color: #009900;">&quot;50,50&quot;</span>, <span style="color: #009900;">&quot;65,65&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&quot;50,80&quot;</span>, <span style="color: #009900;">&quot;50,70&quot;</span>, <span style="color: #009900;">&quot;10,70&quot;</span>, <span style="color: #009900;">&quot;10,60&quot;</span> <br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Show</span><br />
&nbsp;</div>

	<p>When you want to start getting clever and overlapping things, you need to use a Canvas container. The Canvas can contain multiple items which are all absolutely positioned, but unlike most other containers, it doesn&#8217;t automatically expand to contain it&#8217;s children, so you typically have to set it&#8217;s size.</p>

	<p>We also have to set the Stroke and Fill.  These are the two colors that make up every object, and again, if we don&#8217;t set them, they default to white. Note that you can use named colors, or you can specify a hex value using &#8220;#RRGGBB&#8221; or &#8220;#AARRGGBB&#8221; to set the alpha channel. The StrokeThickness controls the line thickness.  </p>

	<p>One other thing to notice is that we positioned the Rectangle by using the <code>Margin</code>, but we positioned the arrow, which we built using a Polygon, based purely on the x,y coordinates of the points.  The available shapes are Ellipse, Line, Path, Polygon, Polyline, and Rectangle.  You can, of course, make nearly any shape you want with the Polygon.</p>

	<p>There are other more advanced shapes available in external libraries, and we can even do 3D, use gradient or image fills&#8230;</p>

	<h3>We can even get images straight off the web</h3>

	<div class="posh code posh" style="font-family:monospace;"><br />
&nbsp; &nbsp;Image <span style="color: #000066;">-Source</span> http:<span style="color: #66cc66;">//</span>huddledmasses.<span style="color: #003366;">org</span><span style="color: #66cc66;">/</span>images<span style="color: #66cc66;">/</span>PowerBoots<span style="color: #66cc66;">/</span>IMG_3298.<span style="color: #003366;">jpg</span> <span style="color: #000066;">-MaxWidth</span> <span style="color: #cc66cc;">400</span> <span style="color: #000066;">-AsJob</span><br />
&nbsp;</div>

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

	<p><span class="caps">WPF</span> loads the image on a background thread, and caches it in memory, so the window will show up and be responsive while you&#8217;re waiting for the image, and because we&#8217;ve specified <code>-AsJob</code>, you can actually continue using PowerShell while the image loads. Note: it will load much faster the second time you run that script.  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';)' class='wp-smiley' /> </p>

	<h3>Events</h3>

	<p>If you were paying attention to the typography example, you&#8217;ll have noticed that we introduced event handling without making a big fuss about it. Event handlers in PowerBoots are specified in much the same way that Properties are, except that their parameter names always start with &#8220;On_&#8221; and they take a script block.  The Hyperlink element in a <span class="caps">WPF</span> window doesn&#8217;t automatically open a browser (because you can use it to change &#8220;pages&#8221; in a <span class="caps">WPF</span> application), so to make simple web links work, you have to handle the &#8220;RequestNavigate&#8221; event as shown above.</p>

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

	<div class="posh code posh" style="font-family:monospace;"><br />
WrapPanel <span style="color: #000066;">-ControlName</span> <span style="color: #009900;">'Click Counter'</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; Button <span style="color: #009900;">&quot;Push Me&quot;</span> <span style="color: #000066;">-On_Click</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #333;">&#123;</span>Click Counter<span style="color: #333;">&#125;</span>.<span style="color: #003366;">Tag</span> <span style="color: #66cc66;">=</span> $<span style="color: #333;">&#123;</span>Click Counter<span style="color: #333;">&#125;</span>.<span style="color: #003366;">Tag</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$CountLabel</span>.<span style="color: #003366;">Content</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;You clicked the button $(${Click Counter}.Tag) times!&quot;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; Label <span style="color: #009900;">&quot;Nothing pushed so far&quot;</span> <span style="color: #000066;">-Name</span> CountLabel<br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Show</span><br />
&nbsp;</div>

	<p>In order to update your user interface when an event triggers, you&#8217;ll need to have variables that point at the control(s) you want to affect.  In ShowUI event handlers, you get a <code>$this</code> variable which points at the object that caused the event, a <code>$_</code> variable which is the event arguments, and a <code>$window</code> variable for the top-level window &#8230; you also get variables for each named control in your window.  </p>

	<p><span class="caps">WPF</span> Elements all have a <code>Tag</code> property which can be used to store any object, so as you can see in the previous example, you can use that to keep track of things &#8230; but more importantly, the value of the <code>Tag</code> on the top-level control will be output. In other words, when the window is closed, this example actually outputs to the PowerShell pipeline how many times you clicked the button.</p>

	<h3>Working with User Input</h3>

	<p>There are helper functions in ShowUI for working with that output value: <code>Set-UIValue</code> and <code>Get-UIValue</code>, and Get-UIValue is particularly helpful because if the control it&#8217;s called on has no value, it collects the values of all the child controls, so you can use it to output a whole form at once:</p>

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

	<p>I&#8217;ve made this example more complicated than it needed to be to demonstrate some best practices.  We could have made it much simpler by using a UniformGrid control instead of a GridControl, thus avoiding needing to set the -Row and -Column special parameters, but I wanted to show those to you anyway, and the form looks a lot better when the two columns can have different sizes:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Grid <span style="color: #000066;">-ControlName</span> <span style="color: #009900;">'Your Information'</span> <span style="color: #000066;">-Columns</span> Auto,<span style="color: #66cc66;">*</span> <span style="color: #000066;">-Rows</span> <span style="color: #cc66cc;">7</span> <span style="color: #000066;">-MinHeight</span> <span style="color: #cc66cc;">200</span> <span style="color: #000066;">-MinWidth</span> <span style="color: #cc66cc;">250</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; Label <span style="color: #009900;">&quot;First 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> First <span style="color: #000066;">-Column</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; Label <span style="color: #009900;">&quot;Last Name&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> Last <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">1</span> <span style="color: #000066;">-Column</span> <span style="color: #cc66cc;">1</span><br />
<br />
&nbsp; &nbsp; Label <span style="color: #009900;">&quot;Address&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;">TextBox</span></span> <span style="color: #000066;">-Name</span> Address <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">2</span> <span style="color: #000066;">-Column</span> <span style="color: #cc66cc;">1</span><br />
<br />
&nbsp; &nbsp; Label <span style="color: #009900;">&quot;City&quot;</span> <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">3</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> City <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">3</span> <span style="color: #000066;">-Column</span> <span style="color: #cc66cc;">1</span><br />
<br />
&nbsp; &nbsp; Label <span style="color: #009900;">&quot;State&quot;</span> <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">4</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> State <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">4</span> <span style="color: #000066;">-Column</span> <span style="color: #cc66cc;">1</span><br />
<br />
&nbsp; &nbsp; Label <span style="color: #009900;">&quot;Zip&quot;</span> <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">5</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> Zip <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">5</span> <span style="color: #000066;">-Column</span> <span style="color: #cc66cc;">1</span><br />
<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;">-IsDefault</span> <span style="color: #000066;">-On_Click</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <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><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">6</span> <span style="color: #000066;">-Column</span> <span style="color: #cc66cc;">1</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Show</span> <span style="color: #000066;">-On_Loaded</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$First</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><br />
&nbsp;</div>

	<p>You&#8217;ll notice how easily I specified the width of the columns, but the <span class="caps">COUNT</span> of the rows in the Grid panel. If you provide an array of values, they&#8217;re converted to GridLengths, but if you provide just one, it&#8217;s treated as the count. You can make grid columnds and rows AUTOmatically size to their contents, and you can make them * width to make them take up any extra space.  You can even split the extra space by setting * on more than one column (and specify the proportion by using numbers, like: 1*, 2*).</p>

	<p>I should also point out that if you use MinHeight and MinWidth instead of the Height and Width values, your controls will be able to size up to fill space when the window is resized! Try that script with Width and Height instead and resize the window to see the difference.</p>

	<p>We used the Set-UIValue shortcut in this form, which brings up another point: when creating data forms, you only need to name the controls you want output from. Then you&#8217;ll be able to just call Set-UIValue on the parent to collect all the values from the controls and output them as a hashtable!</p>

	<p>Finally, remember your defaults: set the focus to something so the user doesn&#8217;t <span class="caps">HAVE</span> to click to get started, and set a button to -IsDefault with a On_Click handler so that when the user hits enter they can submit the form.</p>

	<h3>Further directions</h3>

	<p>There&#8217;s a lot more possible with ShowUI: we can use gradients for colors, create data templates and styles, and even make chromeless windows, but you have the basics for getting started already.</p>

	<p>This first release of ShowUI is still missing some features from both <span class="caps">WPK</span> and PowerBoots, but we&#8217;ll get to them as we progress.  For now we&#8217;d like to invite you to come <a href="http://showui.codeplex.com/releases/">download the latest release</a>, and <a href="http://showui.codeplex.com/workitem/list/basic">write up or vote up the features</a> that you want the most in the next version.  Keep an eye on the release page and on the discussions and we&#8217;ll be cranking out new releases on a monthly basis for now.</p>

	<p>I hope you&#8217;ve enjoyed this tour through ShowUI, and will be able to start applying it soon for fun and profit.</p>

	<p id="fn1" class="footnote"><sup>1</sup> The -<span class="caps">STA</span> switch is necessary because the .Net framework requires the <acronym title="Single Threaded Apartment">STA</acronym> threading model in order to do graphical user interfaces. PowerShell <span class="caps">ISE</span> doesn&#8217;t require it because unlike the PowerShell.exe command console (which is a Win32 native &#8220;console&#8221; application that hosts the .Net framework), PowerShell <span class="caps">ISE</span> is a .Net framework &#8220;graphical&#8221; application, and is therefore running in the <span class="caps">STA</span> model already.</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/showui-tutorial-walkthrough/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>The problem with calling legacy/native apps from PowerShell</title>
		<link>http://huddledmasses.org/the-problem-with-calling-legacy-or-native-apps-from-powershell/</link>
		<comments>http://huddledmasses.org/the-problem-with-calling-legacy-or-native-apps-from-powershell/#comments</comments>
		<pubDate>Thu, 10 Mar 2011 22:16:14 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Interoperability]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Shell]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1664</guid>
		<description><![CDATA[This post is an explanation of the major problems with invoking native apps from PowerShell 2.0, and the simple work-around. There is going to be a little bit of code and then quite a bit of sample output (along with some pointers so you don&#8217;t have to play spot the differences). In order to avoid [...]]]></description>
			<content:encoded><![CDATA[	<p>This post is an explanation of the major problems with invoking native apps from PowerShell 2.0, and the simple work-around.  There is going to be a little bit of code and then quite a bit of sample output (along with some pointers so you don&#8217;t have to play spot the differences). In order to avoid keeping you waiting while I get to the workaround, I&#8217;ll let out the secret right here at the top, because it is really very simple: use Start-Process and a here-string to make sure PowerShell does not mess with your arguments (more on this later).</p>

	<p>So, first, to demonstrate the differences, lets create an executable which will use various methods to access its command-line parameters and print them out.  We will do this in C# although you could do it in C++ and see some variations because the .Net runtime does some parsing of it&#8217;s own, which is a little weird &#8212; but this simple example should be enough to demonstrate the differences.</p>

	<p>The code here is in PowerShell format, so you can simply paste it into PowerShell and get an executable as output, which you can then move to C:\Windows\System32 for testing purposes.</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;">-Type</span> @<span style="color: #009900;">&quot;<br />
using System;<br />
internal class ArgsTest <br />
{<br />
&nbsp; &nbsp;private static void Main(string[] args)<br />
&nbsp; &nbsp;{<br />
&nbsp; &nbsp; &nbsp; Console.WriteLine();<br />
&nbsp; &nbsp; &nbsp; /* <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;I've commented this out because (at least in C#)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;it is the same as Environment.GetCommandLineArgs() <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Except that GetCommandLineArgs shows parameter 0 as the executable path<br />
&nbsp; &nbsp; &nbsp; */<br />
&nbsp; &nbsp; &nbsp; // Console.WriteLine(&quot;</span><span style="color: #666699; font-weight: bold;">Using</span> args:<span style="color: #009900;">&quot;);<br />
&nbsp; &nbsp; &nbsp; // Console.WriteLine();<br />
&nbsp; &nbsp; &nbsp; // for (int i = 0; i &lt; args.Length; i++)<br />
&nbsp; &nbsp; &nbsp; // {<br />
&nbsp; &nbsp; &nbsp; // &nbsp; &nbsp;Console.WriteLine(&quot;</span> &nbsp; Arg <span style="color: #333;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#125;</span> <span style="color: #333399; font-weight: bold; font-style: italic;">is</span>: <span style="color: #333;">&#123;</span><span style="color: #cc66cc;">1</span><span style="color: #333;">&#125;</span><span style="color: #009900;">&quot;, i, args[i]);<br />
&nbsp; &nbsp; &nbsp; // }<br />
&nbsp; &nbsp; &nbsp; // Console.WriteLine();<br />
&nbsp; &nbsp; &nbsp; // Console.WriteLine();<br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; Console.WriteLine(&quot;</span>CommandLine:<span style="color: #009900;">&quot;);<br />
&nbsp; &nbsp; &nbsp; Console.WriteLine();<br />
&nbsp; &nbsp; &nbsp; Console.WriteLine(&quot;</span> &nbsp; <span style="color: #009900;">&quot; + Environment.CommandLine);<br />
&nbsp; &nbsp; &nbsp; Console.WriteLine();<br />
&nbsp; &nbsp; &nbsp; Console.WriteLine();<br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; Console.WriteLine(&quot;</span>CommandLineArgs:<span style="color: #009900;">&quot;);<br />
&nbsp; &nbsp; &nbsp; Console.WriteLine();<br />
&nbsp; &nbsp; &nbsp; string[] arguments = Environment.GetCommandLineArgs();<br />
&nbsp; &nbsp; &nbsp; for (int i = 0; i &lt; arguments.Length; i++)<br />
&nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Console.WriteLine(&quot;</span> &nbsp; Arg <span style="color: #333;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#125;</span> <span style="color: #333399; font-weight: bold; font-style: italic;">is</span>: <span style="color: #333;">&#123;</span><span style="color: #cc66cc;">1</span><span style="color: #333;">&#125;</span><span style="color: #009900;">&quot;, i, arguments[i]);<br />
&nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; Console.WriteLine();<br />
&nbsp; &nbsp; &nbsp; Console.WriteLine();<br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; System.Threading.Thread.Sleep(10000);<br />
&nbsp; &nbsp;}<br />
}<br />
&quot;</span>@ <span style="color: #000066;">-OutputAssembly</span> ArgsTest.<span style="color: #003366;">exe</span> <span style="color: #000066;">-OutputType</span> ConsoleApplication<br />
&nbsp;</div>

	<p>To test things, we are going to invoke it in PowerShell, in <span class="caps">CMD</span>.exe (<span class="caps">DOS</span>), and using the Run dialog (and finally, using Start-Process, to demonstrate that it has the same output as the Run dialog).</p>

	<h2>A note about path problems</h2>

	<p>In order to see the differences in how paths are handled, I put ArgsTest.exe in C:\Windows\System32\ &#8212; this allows me to call it with no path at all, with a full path, or with a partial path.  That turned out to be important because <span class="highlight">the first difference of PowerShell is that it <strong>always</strong> executes the app with a <span class="caps">FULL</span> path, even if you&#8217;re calling it with a partial path</span>.  </p>

	<p>The reason for this is simple: PowerShell doesn&#8217;t ever set the &#8220;CurrentDirectory&#8221; in it&#8217;s operating evironment when you Set-Location (presumably because they figured that often that location would be in a non-FileSystem provider).  Because it doesn&#8217;t set the current directory, it always needs to invoke applications and scripts with their full path name.</p>

	<h2>The test case</h2>

	<p>We will execute the following (long) command-line:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
ArgsTest.<span style="color: #003366;">exe</span> <span style="color: #000066;">-this</span> <span style="color: #333399; font-weight: bold; font-style: italic;">is</span>, a, <span style="color: #660033; font-weight: bold;">$Home</span>, <span style="color: #66cc66;">%</span>UserProfile<span style="color: #66cc66;">%</span> &nbsp; <span style="color: #66cc66;">/</span>test &nbsp;<span style="color: #000066;">-extra</span> spaces <span style="color: #66cc66;">/</span>slashes:<span style="color: #009900;">&quot;including spaces&quot;</span> <span style="color: #000066;">-dashes</span>:<span style="color: #009900;">&quot;including 'quotes'&quot;</span> <span style="color: #000066;">-and</span>:<span style="color: #009900;">'some colons'</span> <span style="color: #66cc66;">/</span>with:<span style="color: #009900;">'single $Home quotes'</span><br />
&nbsp;</div>

	<p>And to get us started, lets look at what that looks like when you execute it from cmd:</p>

	<div class="none code none" style="font-family:monospace;"><br />
CommandLine:<br />
<br />
&nbsp; &nbsp;ArgsTest.exe &nbsp;-this is, a, $Home, C:\Users\jbennett &nbsp; /test &nbsp;-extra spaces /slashes:&quot;including spaces&quot; -dashes:&quot;including 'quotes'&quot; -and:'some colons' /with:'single $Home quotes'<br />
<br />
<br />
CommandLineArgs:<br />
<br />
&nbsp; &nbsp;Arg 0 is: ArgsTest.exe<br />
&nbsp; &nbsp;Arg 1 is: -this<br />
&nbsp; &nbsp;Arg 2 is: is,<br />
&nbsp; &nbsp;Arg 3 is: a,<br />
&nbsp; &nbsp;Arg 4 is: $Home,<br />
&nbsp; &nbsp;Arg 5 is: C:\Users\jbennett<br />
&nbsp; &nbsp;Arg 6 is: /test<br />
&nbsp; &nbsp;Arg 7 is: -extra<br />
&nbsp; &nbsp;Arg 8 is: spaces<br />
&nbsp; &nbsp;Arg 9 is: /slashes:including spaces<br />
&nbsp; &nbsp;Arg 10 is: -dashes:including 'quotes'<br />
&nbsp; &nbsp;Arg 11 is: -and:'some<br />
&nbsp; &nbsp;Arg 12 is: colons'<br />
&nbsp; &nbsp;Arg 13 is: /with:'single<br />
&nbsp; &nbsp;Arg 14 is: $Home<br />
&nbsp; &nbsp;Arg 15 is: quotes'<br />
&nbsp;</div>

	<p>Notice that in this output, Environment.CommandLine is exactly what was typed!<br />
Now, let&#8217;s see the same thing from the run dialog:</p>

	<div class="none code none" style="font-family:monospace;"><br />
CommandLine:<br />
<br />
&nbsp; &nbsp;&quot;C:\Windows\system32\ArgsTest.exe&quot; -this is, a, $Home, C:\Users\jbennett &nbsp; /test &nbsp;-extra spaces /slashes:&quot;including spaces&quot; -dashes:&quot;including 'quotes'&quot; -and:'some colons' /with:'single $Home quotes'<br />
<br />
<br />
CommandLineArgs:<br />
<br />
&nbsp; &nbsp;Arg 0 is: C:\Windows\system32\ArgsTest.exe<br />
&nbsp; &nbsp;Arg 1 is: -this<br />
&nbsp; &nbsp;Arg 2 is: is,<br />
&nbsp; &nbsp;Arg 3 is: a,<br />
&nbsp; &nbsp;Arg 4 is: $Home,<br />
&nbsp; &nbsp;Arg 5 is: C:\Users\jbennett<br />
&nbsp; &nbsp;Arg 6 is: /test<br />
&nbsp; &nbsp;Arg 7 is: -extra<br />
&nbsp; &nbsp;Arg 8 is: spaces<br />
&nbsp; &nbsp;Arg 9 is: /slashes:including spaces<br />
&nbsp; &nbsp;Arg 10 is: -dashes:including 'quotes'<br />
&nbsp; &nbsp;Arg 11 is: -and:'some<br />
&nbsp; &nbsp;Arg 12 is: colons'<br />
&nbsp; &nbsp;Arg 13 is: /with:'single<br />
&nbsp; &nbsp;Arg 14 is: $Home<br />
&nbsp; &nbsp;Arg 15 is: quotes'<br />
&nbsp;</div>

	<p>The thing I want you to notice is that the results are <em>almost</em> exactly the same.  The only difference is that cmd shows you just &#8220;ArgsTest.exe&#8221; which is what was actually typed, whereas the Run dialog inserts the full path to the executable.</p>

	<p>But now, take a look at that same command in PowerShell:</p>

	<div class="none code none" style="font-family:monospace;"><br />
CommandLine:<br />
<br />
&nbsp; &nbsp;&quot;C:\Windows\system32\ArgsTest.exe&quot; &nbsp;-this is a C:\Users\jbennett %UserProfile% /test -extra spaces &quot;/slashes:including spaces&quot; &quot;-dashes:&quot;including 'quotes'&quot;&quot; &quot;-and:&quot;some colons&quot;&quot; &quot;/with:single $Home quotes&quot;<br />
<br />
<br />
CommandLineArgs:<br />
<br />
&nbsp; &nbsp;Arg 0 is: C:\Windows\system32\ArgsTest.exe<br />
&nbsp; &nbsp;Arg 1 is: -this<br />
&nbsp; &nbsp;Arg 2 is: is<br />
&nbsp; &nbsp;Arg 3 is: a<br />
&nbsp; &nbsp;Arg 4 is: C:\Users\jbennett<br />
&nbsp; &nbsp;Arg 5 is: %UserProfile%<br />
&nbsp; &nbsp;Arg 6 is: /test<br />
&nbsp; &nbsp;Arg 7 is: -extra<br />
&nbsp; &nbsp;Arg 8 is: spaces<br />
&nbsp; &nbsp;Arg 9 is: /slashes:including spaces<br />
&nbsp; &nbsp;Arg 10 is: -dashes:including<br />
&nbsp; &nbsp;Arg 11 is: 'quotes'<br />
&nbsp; &nbsp;Arg 12 is: -and:some<br />
&nbsp; &nbsp;Arg 13 is: colons<br />
&nbsp; &nbsp;Arg 14 is: /with:single $Home quotes<br />
&nbsp;</div>

	<h2>A Diagnosis</h2>

	<p>The one thing we expected, of course, is that Powershell interpreted the $Home variable instead of the <span>UserProfile</span> variable, but that&#8217;s <em>hardly</em> the most significant difference &#8230;</p>

	<p>In an attempt to translate what you wrote into what <strong>they think</strong> you meant, PowerShell has rewritten all of the quoting in the command line!</p>

	<ul>
		<li>First, since PowerShell understands that commas are simply array separators, they simply remove them completely when calling a &#8220;native&#8221; app. PowerShell is actually taking the array, and converting the array into a string with space separators.  They don&#8217;t seem to respect <code>$OFS</code> when doing this, so I don&#8217;t really know what the justification is for doing it.</li>
		<li>Second, PowerShell changes single quoted strings into double quoted strings (without expanding the variables) to make sure that what PowerShell thinks of as strings are also thought of as strings by the target application (of course, they wouldn&#8217;t be strings in <span class="caps">DOS</span>, but this is PowerShell). If your executable depends on getting single quotes in the arguments, you&#8217;re going to have some pain because of that, otherwise, you won&#8217;t even notice.</li>
		<li>Third, PowerShell actually <span class="caps">MOVES</span> some quotes, like the one after /slashes &#8230; Obviously PowerShell doesn&#8217;t treat / as a switch, so it treats them as any other string character. Since in PowerShell, if you don&#8217;t quote the <span class="caps">WHOLE</span> string, you can still start quoting halfway through it when you realize you need to type spaces &#8230; they move the quote to the start of the string so that your app will see the same thing a cmdlet would have. </li>
	</ul>
	<ul>
		<li>Finally, PowerShell <span class="caps">INSERTS</span> extra quotes around -dashes.  <em>This is the one thing I can&#8217;t really explain right now.</em> PowerShell inserts extra quotes which result in what looks like a real problem in the CommandLine string due to double-double quotes &#8230; but somehow those escape parsing as an escaped double quote character when you look at the output of GetCommandLineArgs.</li>
	</ul>

	<h2>Work arounds</h2>

	<p>You can get the same results from GetCommandLineArgs when invoking from a PowerShell command by quoting your single quotes and commas, knowing that you need to use $Env: instead of %%, and escaping your dollar signs:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
ArgsTest.<span style="color: #003366;">exe</span> <span style="color: #000066;">-this</span> <span style="color: #009900;">&quot;is,&quot;</span> <span style="color: #009900;">&quot;a,&quot;</span> <span style="color: #009900;">&quot;<span style="color: #000099; font-weight: bold;">`$</span>Home,&quot;</span> <span style="color: #660033; font-weight: bold;">$Env</span>:UserProfile &nbsp; <span style="color: #66cc66;">/</span>test &nbsp;<span style="color: #000066;">-extra</span> spaces <span style="color: #66cc66;">/</span>slashes:<span style="color: #009900;">&quot;including spaces&quot;</span> <span style="color: #009900;">&quot;-dashes:including 'quotes'&quot;</span> <span style="color: #009900;">&quot;-and:'some&quot;</span> <span style="color: #009900;">&quot;colons'&quot;</span> <span style="color: #009900;">&quot;/with:'single&quot;</span> `<span style="color: #660033; font-weight: bold;">$Home</span> <span style="color: #009900;">&quot;quotes'&quot;</span><br />
&nbsp;</div>

	<p>However, if the app you&#8217;re calling relies on parsing CommandLine, instead of using args[] or GetCommandLineArgs(), then you have to do it differently, and you can&#8217;t ever know that until you try it&#8230;</p>

	<p>The best workaround, therefore is to use Start-Process.  Of course, if you have <span>UserProfile</span> or other environment variables, then you still have to replace those with $Env: and escape your other dollar signs, but if you don&#8217;t &#8230; you can just use a single-quote here-string.  Note the minor difference:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">Process</span></span> ArgsTest @<span style="color: #009900;">'<br />
-this is, a, $Home, %UserProfile% &nbsp; /test &nbsp;-extra spaces /slashes:&quot;including spaces&quot; -dashes:&quot;including '</span>quotes<span style="color: #009900;">'&quot; -and:'</span>some colons<span style="color: #009900;">' /with:'</span><span style="color: #003366; font-weight: bold;">single</span> <span style="color: #660033; font-weight: bold;">$Home</span> quotes<span style="color: #009900;">'<br />
'</span>@<br />
&nbsp;</div>

	<div class="none code none" style="font-family:monospace;"><br />
CommandLine:<br />
<br />
&nbsp; &nbsp;&quot;C:\Windows\system32\ArgsTest.exe&quot; -this is, a, $Home, %UserProfile% &nbsp; /test &nbsp;-extra spaces /slashes:&quot;including spaces&quot; -dashes:&quot;including 'quotes'&quot; -and:'some colons' /with:'single $Home quotes'<br />
<br />
<br />
CommandLineArgs:<br />
<br />
&nbsp; &nbsp;Arg 0 is: C:\Windows\system32\ArgsTest.exe<br />
&nbsp; &nbsp;Arg 1 is: -this<br />
&nbsp; &nbsp;Arg 2 is: is,<br />
&nbsp; &nbsp;Arg 3 is: a,<br />
&nbsp; &nbsp;Arg 4 is: $Home,<br />
&nbsp; &nbsp;Arg 5 is: %UserProfile%<br />
&nbsp; &nbsp;Arg 6 is: /test<br />
&nbsp; &nbsp;Arg 7 is: -extra<br />
&nbsp; &nbsp;Arg 8 is: spaces<br />
&nbsp; &nbsp;Arg 9 is: /slashes:including spaces<br />
&nbsp; &nbsp;Arg 10 is: -dashes:including 'quotes'<br />
&nbsp; &nbsp;Arg 11 is: -and:'some<br />
&nbsp; &nbsp;Arg 12 is: colons'<br />
&nbsp; &nbsp;Arg 13 is: /with:'single<br />
&nbsp; &nbsp;Arg 14 is: $Home<br />
&nbsp; &nbsp;Arg 15 is: quotes'<br />
&nbsp;</div>

	<h4>And finally, we have it!</h4>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">Process</span></span> ArgsTest @<span style="color: #009900;">&quot;<br />
-this is, a, <span style="color: #000099; font-weight: bold;">`$</span>Home, $Env:UserProfile &nbsp; /test &nbsp;-extra spaces /slashes:&quot;</span>including spaces<span style="color: #009900;">&quot; -dashes:&quot;</span>including <span style="color: #009900;">'quotes'</span><span style="color: #009900;">&quot; -and:'some colons' /with:'single <span style="color: #000099; font-weight: bold;">`$</span>Home quotes'<br />
&quot;</span>@<br />
&nbsp;</div>

	<div class="plain code plain" style="font-family:monospace;"><br />
CommandLine:<br />
<br />
&nbsp; &nbsp;&quot;C:\Windows\system32\ArgsTest.exe&quot; -this is, a, $Home, C:\Users\jbennett &nbsp; /test &nbsp;-extra spaces /slashes:&quot;including spaces&quot; -dashes:&quot;including 'quotes'&quot; -and:'some colons' /with:'single $Home quotes' <br />
<br />
<br />
CommandLineArgs:<br />
<br />
&nbsp; &nbsp;Arg 0 is: C:\Windows\system32\ArgsTest.exe<br />
&nbsp; &nbsp;Arg 1 is: -this<br />
&nbsp; &nbsp;Arg 2 is: is,<br />
&nbsp; &nbsp;Arg 3 is: a,<br />
&nbsp; &nbsp;Arg 4 is: $Home,<br />
&nbsp; &nbsp;Arg 5 is: C:\Users\jbennett<br />
&nbsp; &nbsp;Arg 6 is: /test<br />
&nbsp; &nbsp;Arg 7 is: -extra<br />
&nbsp; &nbsp;Arg 8 is: spaces<br />
&nbsp; &nbsp;Arg 9 is: /slashes:including spaces<br />
&nbsp; &nbsp;Arg 10 is: -dashes:including 'quotes'<br />
&nbsp; &nbsp;Arg 11 is: -and:'some<br />
&nbsp; &nbsp;Arg 12 is: colons'<br />
&nbsp; &nbsp;Arg 13 is: /with:'single<br />
&nbsp; &nbsp;Arg 14 is: $Home<br />
&nbsp; &nbsp;Arg 15 is: quotes'<br />
<br />
&nbsp;</div>

	<p>As you can see, the only difference is the value in Arg 5 &#8230; but this last attempt actually results in a completely compatible CommandLine and CommandLineArgs. Of course, if you need to capture the output, then you need to append: <code>-Wait -RedirectStandardOutput StdOut.log; gc StdOut.log</code> &#8230; but that won&#8217;t work if you need to use the application interactively (although, if you just want to script an interactive app, there is also a <code>-RedirectStandardInput</code> parameter).</p>

	<p>In any case, if your app is non-interactive, you can use something like this:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #66cc66;">&amp;</span><span style="color: #333;">&#123;</span> <br />
<span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">Process</span></span> ArgsTest @<span style="color: #009900;">&quot;<br />
-this is, a, <span style="color: #000099; font-weight: bold;">`$</span>Home, $Env:UserProfile &nbsp; /test &nbsp;-extra spaces /slashes:&quot;</span>including spaces<span style="color: #009900;">&quot; -dashes:&quot;</span>including <span style="color: #009900;">'quotes'</span><span style="color: #009900;">&quot; -and:'some colons' /with:'single <span style="color: #000099; font-weight: bold;">`$</span>Home quotes'<br />
&quot;</span>@ <span style="color: #000066;">-NoNewWindow</span> <span style="color: #000066;">-Wait</span> <span style="color: #000066;">-RedirectStandardOutput</span> stdout.<span style="color: #003366;">log</span>; <span style="color: #660033;">gc</span> stdout.<span style="color: #003366;">log</span>; <span style="color: #660033;">rm</span> stdout.<span style="color: #003366;">log</span><br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<h4>But still</h4>

	<p>If you need to run an app in the PowerShell window, interact with it (e.g.: answer a prompt), and then capture, process, or redirect the output &#8230; you need to invoke the app &#8220;natively&#8221; ... not using Start-Process. The bottom line is that PowerShell <em>still</em> needs a way to invoke apps that works as simply as Start-Process, but that invokes them inline in the shell.  In the meantime, you should generate that <code>ArgsTest</code> executable (<a href="http://PoshCode.org/get/2555">from poshcode</a>) to help you figure out the right combination of quotation magic to convince PowerShell 2.0 to pass the right values!</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/the-problem-with-calling-legacy-or-native-apps-from-powershell/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>PowerBoots (Next Generation UI) WPF for PowerShell slides</title>
		<link>http://huddledmasses.org/powerboots-presentation-2011-02/</link>
		<comments>http://huddledmasses.org/powerboots-presentation-2011-02/#comments</comments>
		<pubDate>Mon, 14 Feb 2011 04:29:41 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[PowerBoots]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[ShowUI]]></category>
		<category><![CDATA[UserInterface]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1657</guid>
		<description><![CDATA[I gave a LiveMeeting presentation on PowerBoots last week, and these are the slides. I&#8217;m sorry to say that the recording did not work out, so you&#8217;ll have to wait for the next time I present it to see the demos. Luckily, the demo scripts I used are mostly the samples which are available in [...]]]></description>
			<content:encoded><![CDATA[	<p>I gave a LiveMeeting presentation on PowerBoots last week, and these are the slides. I&#8217;m sorry to say that the recording did not work out, so you&#8217;ll have to wait for the next time I present it to see the demos. Luckily, the demo scripts I used are mostly the samples which <em>are</em> available in the PowerBoots distributions.</p>

	<ul>
		<li><a href="/downloads/PowerBoots/PowerBoots-2011.pptx" title="2.13Mb">PowerPoint Deck</a></li>
	</ul>
	<ul>
		<li><a href="/downloads/PowerBoots/PowerBoots-2011.pdf" title="973Kb"><span class="caps">PDF</span> Deck with notes</a></li>
	</ul>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/powerboots-presentation-2011-02/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

