<?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; Development</title>
	<atom:link href="http://joelbennett.net/tag/development/feed/" rel="self" type="application/rss+xml" />
	<link>http://joelbennett.net</link>
	<description>The internet home of Joel "Jaykul" Bennett...</description>
	<lastBuildDate>Sat, 28 Jan 2012 21:37:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<cloud domain='joelbennett.net' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>Visual Studio Not Responding (beeping) when editing ASPX</title>
		<link>http://joelbennett.net/visual-studio-not-responding-beeping-when-editing-aspx/</link>
		<comments>http://joelbennett.net/visual-studio-not-responding-beeping-when-editing-aspx/#comments</comments>
		<pubDate>Fri, 30 Apr 2010 14:14:06 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[BugBug]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Workaround]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1455</guid>
		<description><![CDATA[I&#8217;m just going to share the solution, in case anyone else encounters this after installing Office 2007 or 2010 Yesterday I came across a very strange bug which was causing Visual Studio to lock up after a few (30?) seconds every time I opened an ASPX file for editing. I even uninstalled a few VS [...]]]></description>
			<content:encoded><![CDATA[	<p>I&#8217;m just going to share the solution, in case anyone else encounters this after installing Office 2007 or 2010</p>

	<p>Yesterday I came across a very strange bug which was causing Visual Studio to lock up after a few (30?) seconds every time I opened an <span class="caps">ASPX</span> file for editing.  I even uninstalled a few VS addons I thought might have caused it… but that didn’t help (I&#8217;m reinstalling Resharper as I type this).</p>

	<p>After much frustration and a couple of Bing searches … it turned out to be a problem with MS Office 2007 components. This was a bit of a surprise, since I <em>do not have Office 2007 installed</em>. However, I <em>do</em> have <a href="http://office.microsoft.com/communicator">Office Communicator</a> and <a href="http://office.microsoft.com/livemeeting">Live Meeting</a> which are Office 2007 products &#8212; and I had just installed Office 2010, which may have actually caused the problem.</p>

	<p><a href="http://abdullin.com/journal/2009/5/12/visual-studio-2008-locks-or-freezes-in-aspx.html">Rinat Abdullin wrote about the problem earlier</a> and there&#8217;s a <a href="http://stackoverflow.com/questions/227230/visual-studio-solution-explorer-locks-up">thread on StackOverflow</a> which is at least related.</p>

	<p>Basically: you get “beeping” when you try to click in the text editor for aspx, or on the Solution Explorer … (sometimes, but not always, Visual Studio will actually be marked as “Not Responding” by Windows) – and, when this happens, you look in your TaskManager and see this Setup.exe process.</p>

	<p>This one &#8212; in your <code>%CommonProgramFiles%</code> or <code>%CommonProgramFiles(x86)%</code>:<br />
<code>C:\Program Files (x86)\Common Files\Microsoft Shared\OFFICE12\Office Setup Controller\SETUP.EXE</code></p>

	<p>Shutting down Visual Studio and then running that installer &#8220;as administrator&#8221; manually has solved the problem for me. I just hit “Repair” and that was that. Others (including <a href="http://abdullin.com/journal/2009/5/12/visual-studio-2008-locks-or-freezes-in-aspx.html">Rinat</a>) have said they had to reinstall Office &#8212; I suspect that probably depends on whether or not you had Office 2007 installed in the first place.</p>

<div class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/546f0c58-0e03-4f09-81f2-600318c429c7/" title="Reblog this post [with Zemanta]"><img class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=546f0c58-0e03-4f09-81f2-600318c429c7" alt="Reblog this post [with Zemanta]" /></a><span class="zem-script more-related pretty-attribution"><script type="text/javascript" src="http://static.zemanta.com/readside/loader.js" defer="defer"></script></span></div>]]></content:encoded>
			<wfw:commentRss>http://joelbennett.net/visual-studio-not-responding-beeping-when-editing-aspx/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Windows PowerShell 2.0 SDK Released</title>
		<link>http://joelbennett.net/windows-powershell-2-0-sdk-released/</link>
		<comments>http://joelbennett.net/windows-powershell-2-0-sdk-released/#comments</comments>
		<pubDate>Wed, 23 Dec 2009 20:53:36 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[APIs]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[SDK]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1360</guid>
		<description><![CDATA[So, the Windows PowerShell 2.0 Software Development Kit is basically a collection of samples, and it has been released separately from the Windows Platform SDK for a change, making the download a tiny 2.35MB &#8230; There are lots of examples in there in C# (no other languages), and honestly, some of them ought to make [...]]]></description>
			<content:encoded><![CDATA[	<p>So, the <a href="http://support.microsoft.com/kb/968929">Windows PowerShell 2.0</a> <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=50633A1F-A665-425D-923C-1A269F8AC084" title="SDK">Software Development Kit</a> is basically a collection of samples, and it has been released separately from the Windows Platform <span class="caps">SDK</span> for a change, making the download a tiny 2.35MB &#8230;</p>

	<p>There are lots of examples in there in C# (no other languages), and honestly, some of them ought to make it into <span class="caps">PSCX</span> or some other project where people could grab pre-compiled versions  <img src='http://joelbennett.net/wordpress/wp-includes/' alt=';)' class='wp-smiley' /> </p>

	<p>	<ul>
		<li>A Template for creating PSProviders, and a sample PSProvider (for Access databases).</li>
		<li>A sample of participating in Transactions (a set of &#8220;transacted comment&#8221; cmdlets for creating comments that go along with a transaction).</li>
		<li>Example cmdlets: Select-Object, Select-String, Get-Process, Stop-Process</li>
		<li>Dealing with issues with Serialization in PowerShell
		<li>PowerShell Eventing:
	<ul>
		<li>Deriving from ObjectEventRegistrationBase to create Register-FileSystemEvent</li>
	</ul></li>
	<ul>
		<li>Receiving notifications from PowerShell Events on remote computers.</li>
		<li>Hosting APIs, including (among others):
		<li>Restricted runspaces</li>
		<li>Runspace pools</li>
		<li>Remote runspaces (and remote runspace pools)</li>
		<li>Running commands in parallel or sequentially</li>
		<li>Calling Cmdlets and passing parameters</li>
	</ul></li>
	</ul>
	<ul>
		<li>Reproducing the default PowerShell.exe output</li>
	</ul></li></p>

	<p>Honestly, if you&#8217;re a developer that&#8217;s been wondering about learning to code cmdlets, or host PowerShell as a scripting engine, now&#8217;s the time.</p>]]></content:encoded>
			<wfw:commentRss>http://joelbennett.net/windows-powershell-2-0-sdk-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Windows Compatability Libraries Galore (including PowerShell 2 for XP)</title>
		<link>http://joelbennett.net/windows-compatability-libraries-powershell-2-for-xp/</link>
		<comments>http://joelbennett.net/windows-compatability-libraries-powershell-2-for-xp/#comments</comments>
		<pubDate>Fri, 11 Sep 2009 15:50:04 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[licensing]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Windows 7]]></category>
		<category><![CDATA[Windows XP]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1272</guid>
		<description><![CDATA[Microsoft has been very busy this year &#8230; and in these last couple of months before the general availability of Windows 7, they&#8217;re trying very hard to crank out the tools necessary to encourage development of Windows 7 applications. The first tool out of the gate, of course, was the Windows API Code Pack for [...]]]></description>
			<content:encoded><![CDATA[	<p>Microsoft has been very busy this year &#8230; and in these last couple of months before the general availability of Windows 7, they&#8217;re trying very hard to crank out the tools necessary to encourage development of Windows 7 applications.</p>

	<p>The first tool out of the gate, of course, was the <a href="http://code.msdn.microsoft.com/WindowsAPICodePack">Windows <span class="caps">API</span> Code Pack</a> for the .Net framework.  A nice library that makes it simple to write applications that take advantage of new Windows Vista and Windows 7 features like Libraries, Task panes and jump lists.  Of course, that library has a <strong>major</strong> problem: it&#8217;s got an ugly and confusing license. Rather than using one of the standard Microsoft Open Source Licenses, the team used a license from the dark ages that features the vague &#8220;Excluded Licenses&#8221; clause and other ugly terminology.</p>

	<p>More recently, Microsoft has announced a <strong>Platform Update for Vista</strong> which is intended to allow Vista to run most applications designed for Windows 7. The Platform Update is a set of runtime libraries which includes the Windows Ribbon control; Automation Manager Library; DirectX updates for hardware acceleration; DirectCompute for hardware-accelerated parallel computing support; the <span class="caps">XPS</span> printing library; the Windows Automation API; and the Windows Portable Devices Platform, which standardizes data transfers across apps and portable devices.  This important package will be made available through Windows Update (and applications will be able to prompt you to download it) and it is <a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=081c218f-c845-47d1-b124-71f80bf21638">in public beta</a> with <a href="http://windowsteamblog.com/blogs/windows7/archive/2009/09/10/announcing-new-tools-for-it-professionals-amp-developers-transitioning-to-windows-7.aspx">more information on the Windows Team blog</a>.  A couple of pieces of that will be made available for Windows XP; particularly the Automation <span class="caps">API</span>, which allows accessibility tools and test automation tools to access Windows user interface in a consistent way &#8212; this will mean that the next release of <a href="http://wasp.codeplex.com">WASP</a> will work flawlessly across Windows XP, Vista, and Windows 7.</p>

	<p>Also in the works is the <strong>Remote Desktop Connection 7.0</strong> (RDC7) client for Windows XP and Windows Vista to allow these older clients to take advantage of all new server features in Windows 7 and Windows Server 2008, including multi-monitor support and media redirection. There&#8217;s <a href="http://blogs.msdn.com/rds/archive/2009/08/21/remote-desktop-connection-7-for-windows-7-windows-xp-windows-vista.aspx">more information about that on the Remote Desktop team blog</a>.</p>

	<p>And finally, the release candidate for the <a href="https://connect.microsoft.com/windowsmanagement/Downloads">Windows Management Framework</a> (<span class="caps">WMF</span>) is also available. The <span class="caps">WMF</span> will include WinRM (Microsoft&#8217;s implementation of the WS-Management spec), PowerShell 2.0, and <acronym title="	Background Intelligent Transfer Service">BITS</acronym> for Server 2008, Windows Vista, Server 2003, and of course, Windows XP.  This means that if you&#8217;re on Vista, or still using Windows XP, you can now upgrade from the PowerShell 2.0 <span class="caps">CTP</span> 3 to this release candidate and expect remoting to work!</p>

	<p>All of these should be seeing final releases in the very near future, and some of them possibly by the time Windows 7 is released to the public.</p>

<h6 class="zemanta-related-title">Related articles by Zemanta</h6><ul class="zemanta-article-ul"><li class="zemanta-article-ul-li"><a href="http://www.downloadsquad.com/2009/08/27/make-a-bootable-usb-installer-for-windows-xp-vista-7-with-wint/">Make a bootable <span class="caps">USB</span> installer for Windows XP, Vista, 7 with WinToFlash</a> (downloadsquad.com)</li><li class="zemanta-article-ul-li"><a href="http://r.zemanta.com/?u=http%3A//www.infoworld.com/d/windows/microsoft-intel-build-fast-efficient-windows-7-machines-107%3Fsource%3Drss_infoworld_news&amp;a=7369232&amp;rid=4ead389d-07aa-4ec7-b077-ab697671869f&amp;e=fe20c64bed88033a7fa1d59661073792">Microsoft, Intel build fast, efficient Windows 7 machines</a> (infoworld.com)</li><li class="zemanta-article-ul-li"><a href="http://techblissonline.com/free-windows-7/">Free Windows 7 Upgrade</a> (techblissonline.com)</li><li class="zemanta-article-ul-li"><a href="http://r.zemanta.com/?u=http%3A//www.infoworld.com/d/windows/users-abandon-xp-vista-windows-7-062%3Fsource%3Drss_infoworld_news&amp;a=7369241&amp;rid=4ead389d-07aa-4ec7-b077-ab697671869f&amp;e=15525dcfe2606a7bce437222e8e50c1a">Users abandon XP for Vista, Windows 7</a> (infoworld.com)</li><li class="zemanta-article-ul-li"><a href="http://huddledmasses.org/fun-with-pinvoke-and-aero-peek/">Fun with PInvoke and Aero Peek</a> (huddledmasses.org)</li><li class="zemanta-article-ul-li"><a href="http://news.cnet.com/8301-13860_3-10347289-56.html?part=rss&amp;subj=news">Microsoft: Windows 7 not affected by latest flaw</a> (news.cnet.com)</li></ul>

<div class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/4ead389d-07aa-4ec7-b077-ab697671869f/" title="Reblog this post [with Zemanta]"><img class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=4ead389d-07aa-4ec7-b077-ab697671869f" alt="Reblog this post [with Zemanta]" /></a><span class="zem-script more-related pretty-attribution"><script type="text/javascript" src="http://static.zemanta.com/readside/loader.js" defer="defer"></script></span></div>]]></content:encoded>
			<wfw:commentRss>http://joelbennett.net/windows-compatability-libraries-powershell-2-for-xp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PowerBoots: The tutorial walkthrough</title>
		<link>http://joelbennett.net/powerboots-tutorial-walkthrough/</link>
		<comments>http://joelbennett.net/powerboots-tutorial-walkthrough/#comments</comments>
		<pubDate>Thu, 02 Apr 2009 16:21:13 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[PowerBoots]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[PowerTips]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[WalkThrough]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=963</guid>
		<description><![CDATA[Updated to PowerBoots 0.1 An introduction to PowerBoots Please excuse me if I start by just copying the basic ideas of the Shoes Tutorial, but I figured that since PowerBoots is inspired by Shoes, that was as good a place as any to start. PowerBoots (or just &#8220;Boots&#8221;) is a PowerShell 2.0 module with functions [...]]]></description>
			<content:encoded><![CDATA[	<h3> <img src='http://joelbennett.net/wordpress/wp-includes/' alt='[new]' class='wp-smiley' />  Updated to PowerBoots 0.1</h3>

	<h2>An introduction to PowerBoots</h2>

	<p>Please excuse me if I start by just copying the basic ideas of the <a href="http://shoooes.net/tutorial/">Shoes Tutorial</a>, but I figured that since PowerBoots is inspired by Shoes, that was as good a place as any to start.  PowerBoots (or just &#8220;Boots&#8221;) is a PowerShell 2.0 module with functions for writing Windows Presentation Framework (<span class="caps">WPF</span>) applications in the PowerShell scripting language.  You should get <a href="http://boots.CodePlex.com">the latest version of PowerBoots</a> before continuing, and install it by putting the &#8220;PowerBoots&#8221; folder in one of your &#8220;Modules&#8221; folders (list them by typing <code>$Env:PSMODULEPATH</code> in PowerShell v2). </p>

	<p><del>Don&#8217;t forget to start PowerShell.exe with the <span class="caps">STA</span> parameter</del> (This is no longer required in PowerBoots 0.1).</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), 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 Boots program</h2>

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

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">BootsWindow</span></span> <span style="color: #000066;">-SizeToContent</span> WidthAndHeight <span style="color: #000066;">-Content</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;Button <span style="color: #000066;">-Content</span> <span style="color: #009900;">&quot;Push Me&quot;</span> <br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>I&#8217;ve recently <strong>changed</strong> this first example to pass a script block to the content instead of using parentheses, because although PowerBoots supports running in an <span class="caps">MTA</span> PowerShell host, the only UI-creation command that is MTA-safe is the New-BootsWindow command. All of the &#8220;new&#8221; functions must then be inside of a scriptblock which is passed to the New-BootsWindow cmdlet and executed on that <span class="caps">STA</span> thread. You can use parenthesis ( and ) as a container instead, but that requires the host to be in <span class="caps">STA</span> threading mode (run: PowerShell -<span class="caps">STA</span>) so the controls can be created:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">BootsWindow</span></span> <span style="color: #000066;">-SizeToContent</span> WidthAndHeight <span style="color: #000066;">-Content</span> <span style="color: #333;">&#40;</span> <br />
&nbsp; &nbsp;Button <span style="color: #000066;">-Content</span> <span style="color: #009900;">&quot;Push Me&quot;</span><br />
<span style="color: #333;">&#41;</span><br />
&nbsp;</div>

	<p>This first example is a bit uglier than the Shoes syntax, so lets see if we can&#8217;t clean it up some. 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 parameters: Items, Blocks, and Inlines.</p>

	<p>We have used a function <code>New-BootsWindow</code> which has an alias <code>Boots</code>. Boots takes all the same parameters as the <code>Window</code> function mentioned previously, but it uses slightly more useful defaults, and has a few other major benefits as well, the first of which is that it automatically &#8220;shows&#8221; the window, and the second is that it supports an <code>-Async</code> parameter which allows the window to come out in a new thread so that you can continue using PowerShell while the window remains alive and responsive.  There is one catch: New-BootsWindow <em>cannot</em> take it&#8217;s content on the pipeline (the <em>old function</em>, now renamed &#8220;Out-BootsWindow&#8221; can take pipeline content, but is a script function, and requires -<span class="caps">STA</span> mode) &#8212; you have to specify it as a ScriptBlock. So now that we know this, we can rewrite our first example like this:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Boots <span style="color: #333;">&#123;</span> Button <span style="color: #009900;">&quot;Push Me&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>Just for the record, the simplest Boots program would just be a simple popup dialog to put some text in a Window, like:  <code>Boots { $msg }</code> &#8230;</p>

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

	<p><img src="http://huddledmasses.org/images/PowerBoots/PowerBoots2.png" class="float-right-block" alt="" width="156" height="128" /></p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Boots <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;StackPanel <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; Button <span style="color: #009900;">&quot;A bed of clams&quot;</span><br />
&nbsp; &nbsp; &nbsp; Button <span style="color: #009900;">&quot;A coalition of cheetas&quot;</span><br />
&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.  This brings up another point: those positional parameters we mentioned earlier: Content, Children, Items, Blocks, and Inlines, are also set to accept the value from the pipeline.  Not only that, but they are intelligent about whether or not the content model accepts multiple items! So we can actually rewrite that script like this, and get the same results:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Boots <span style="color: #333;">&#123;</span> <span style="color: #009900;">&quot;A bed of clams&quot;</span>, <span style="color: #009900;">&quot;A coalition of cheetas&quot;</span>, <span style="color: #009900;">&quot;A gulp of swallows&quot;</span> <span style="color: #66cc66;">|</span> Button <span style="color: #66cc66;">|</span> StackPanel <span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>Now we&#8217;re really onto something! <span id="more-963"></span> For most of the rest of these examples, I&#8217;m going to stick with the former syntax, because with the indenting and parenthesis, it&#8217;s much easier for you to follow, especially if you&#8217;re not already familiar with <span class="caps">WPF</span>.  For instance, check out what this does:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Boots <span style="color: #333;">&#123;</span> <span style="color: #009900;">&quot;A bed of clams&quot;</span>, <span style="color: #009900;">&quot;A coalition of cheetas&quot;</span>, <span style="color: #009900;">&quot;A gulp of swallows&quot;</span> <span style="color: #66cc66;">|</span> StackPanel <span style="color: #66cc66;">|</span> Button <span style="color: #333;">&#125;</span><br />
&nbsp;</div>

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

	<p><img src="http://huddledmasses.org/images/PowerBoots/PowerBoots3.png" class="float-right-block" alt="" width="174" height="152" /></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.</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Boots <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;">&#40;</span><br />
&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><br />
&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><br />
&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><br />
&nbsp; &nbsp;<span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## Or, on one line:</span><br />
<br />
Boots <span style="color: #333;">&#123;</span> <span style="color: #009900;">&quot;A bed of clams&quot;</span>, <span style="color: #009900;">&quot;A coalition of cheetas&quot;</span>, <span style="color: #009900;">&quot;A gulp of swallows&quot;</span> <span style="color: #66cc66;">|</span><br />
&nbsp; &nbsp;Button <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">|</span> StackPanel <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">5</span> <span style="color: #000066;">-Background</span> Pink <span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>So you see, the pink background is on the StackPanel, which has a (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>

	<h2>Time for some artwork</h2>

	<p><img src="http://huddledmasses.org/images/PowerBoots/PowerBoots4.png" class="float-right-block" alt="" width="176" height="164" /></p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Boots <span style="color: #333;">&#123;</span> 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: #333;">&#125;</span><br />
&nbsp;</div>

	<p>In Boots, 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/PowerBoots/PowerBoots5.png" class="float-right-block" alt="" width="148" height="158" /></p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Boots <span style="color: #333;">&#123;</span><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;">&#40;</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> Purple <span style="color: #000066;">-StrokeThickness</span> <span style="color: #cc66cc;">2</span> <span style="color: #000066;">-Fill</span> Red<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;">&#41;</span> <span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>We use a Canvas for this because it can contain multiple items which are all absolutely positioned.  Unlike 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, if we don&#8217;t set them, they default to white. The StrokeThickness controls the line thickness.  Notice that we positioned the Rectangle by using the <code>Margin</code>, and 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 any shape you want to 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 />
Boots <span style="color: #333;">&#123;</span><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: #66cc66;">|</span> <br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Title</span> <span style="color: #009900;">&quot;Now those are some powerful boots!&quot;</span> <span style="color: #000066;">-Async</span><br />
&nbsp;</div>

	<p><img src="http://huddledmasses.org/images/PowerBoots/PowerBoots6.png" class="float-right-block" alt="" width="436" height="730" /></p>

	<p>Boots 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>-Async</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://joelbennett.net/wordpress/wp-includes/' alt=';)' class='wp-smiley' /> </p>

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

	<p>PowerBoots doesn&#8217;t try 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 ever used.  So instead of having a bunch of named elements like banner, and title, and caption, and para and whatnot, we have controls based on how much text you want to put in them, and how much formatting you want to apply: Label is simplest, TextBlock supports limited text formattings, and FlowDocument supports full rich content. And of course, Hyperlink supports clicking  <img src='http://joelbennett.net/wordpress/wp-includes/' alt=';)' class='wp-smiley' /> </p>

	<p><img src="http://huddledmasses.org/images/PowerBoots/PowerBoots7.png" class="float-right-block" alt="" width="466" height="202" /></p>

	<p>For the typography elements, the content model changes a bit.  There are basically two types: Inline and Block elements. The <a href="http://msdn.microsoft.com/en-us/library/bb613554.aspx">TextBlock Content Model</a> is similar to that of a FlowDocument, 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 />
Boots <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;">&#40;</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;">&#40;</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;PowerBoots?&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>powerboots<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: #333;">&#41;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#41;</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;">&#40;</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;">&#41;</span><br />
<span style="color: #333;">&#41;</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>

	<h3>Events</h3>

	<p>If you were paying attention to that previous example, you&#8217;ll notice we just introduced event handling. Event handlers in PowerBoots are specified in much the same way that Properties are.  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/PowerBoots/PowerBoots8.png" class="float-right-block" alt="" width="202" height="76" /></p>

	<p>In order to update your user interface when an event triggers, you&#8217;ll need to have a variable that points at the control(s) you want to affect.  You get a <code>$this</code> variable for free which points at the object that caused the event (eg: the Hyperlink in our previous example), but otherwise you need to handle this yourself. You can do that one of two ways: you can set a variable the way you normally would, and then use the variable in the form, or you can specify the variable name using the <code>-OutVariable</code> parameter.  Personally I prefer the latter, as it messes up the flow of code less, but it has the downside that the output variable is always an array, even when there&#8217;s only one item.</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Boots <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$global</span>:Count <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span><br />
&nbsp; &nbsp;WrapPanel &nbsp;$<span style="color: #333;">&#40;</span><br />
&nbsp; &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; &nbsp;<span style="color: #660033; font-weight: bold;">$global</span>:Count<span style="color: #66cc66;">++</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$label</span>.<span style="color: #003366;">Content</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;You clicked the button ${global:Count} times!&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$script</span>:label <span style="color: #66cc66;">=</span> Label <span style="color: #009900;">&quot;Nothing pushed so far&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$label</span> <span style="color: #666666; font-style: italic;"># You have to actually write-output the label</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Title</span> <span style="color: #009900;">&quot;Test App&quot;</span> <span style="color: #000066;">-On_Closing</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$global</span>:BootsOutput <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$global</span>:Count; <span style="color: #660033;">rm</span> variable:Count <span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>I&#8217;ve made these examples slightly more complicated than they had to be to demonstrate some best practices.  When the Window is closed, the Out-Boots function returns the $BootsOutput variable &#8212; so if you want to <strong>output</strong> something from your gui, you need to set that variable.  You can, of course, access global scope variables using the scope prefix <code>$global:variableName</code>, so you can set many different variables which you read later in your script.  The catch is, sometimes the variables <em>have</em> to be explicitly set to script or even global scope in order to refer to the same variable in all of the event handlers&#8230;</p>

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

	<p>However, if you want to have this block of code actually <strong>output</strong> something into the pipeline, you&#8217;ll always want to use the <code>$BootsOutput</code> variable.  You can do that directly, the way the example above does, or you can simply use Write-Output! Inside the Out-Boots function, he Write-Output cmdlet just appends to the $BootsOutput variable &#8230; so it works pretty much exactly the way you would expect it to.</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Boots <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;WrapPanel <span style="color: #000066;">-On_Load</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$Count</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span> <span style="color: #333;">&#125;</span> $<span style="color: #333;">&#40;</span><br />
&nbsp; &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; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Output</span></span> <span style="color: #333;">&#40;</span><span style="color: #66cc66;">++</span><span style="color: #660033; font-weight: bold;">$count</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># You have to use array notation ...</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$block</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">Inlines</span>.<span style="color: #660033;">Clear</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span>; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$block</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">Inlines</span>.<span style="color: #003366;">Add</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;You clicked the button $count times!&quot;</span><span style="color: #333;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; TextBlock <span style="color: #009900;">&quot;Nothing pushed so far&quot;</span> <span style="color: #000066;">-OutVariable</span> script:block <span style="color: #000066;">-VerticalAlignment</span> Center<br />
&nbsp; &nbsp;<span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>The first example outputs <strong>just</strong> the count of how many times you clicked.  The second outputs a series of numbers from 1 to however many times you click.  It&#8217;s your choice of how to work with it.</p>

	<h3>We can have fun with colors</h3>

	<p><img src="http://huddledmasses.org/images/PowerBoots/PowerBoots10.png" class="float-right-block" alt="" width="318" height="286" /></p>

	<p>Boots gives you access to all the capabilities of the Windows Presentation Framework, but in some cases that comes at a cost, because we haven&#8217;t simplified their composability.  So we have RadialGradientBrush and LinearGradientBrush, but you have to specify the GradientStops etc &#8230;</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Boots <span style="color: #000066;">-Background</span> <span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp;RadialGradientBrush $<span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; GradientStop <span style="color: #000066;">-Offset</span> <span style="color: #cc66cc;">0</span> <span style="color: #000066;">-Color</span> <span style="color: #009900;">&quot;#F00&quot;</span><br />
&nbsp; &nbsp; &nbsp; GradientStop <span style="color: #000066;">-Offset</span> <span style="color: #cc66cc;">1</span> <span style="color: #000066;">-Color</span> <span style="color: #009900;">&quot;#F90&quot;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;Label <span style="color: #009900;">&quot;Boots&quot;</span> <span style="color: #000066;">-HorizontalAlignment</span> Center `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">-VerticalAlignment</span> Center `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">-Foreground</span> White <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">80</span> `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">-FontWeight</span> Bold &nbsp;<span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">40</span><br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>We also have</p>

	<h3>So what does an InputBox look like?</h3>

	<p><img src="http://huddledmasses.org/images/PowerBoots/PowerBoots11.png" class="float-right-block" title="PowerBoots11.png" alt="PowerBoots11.png" width="260" height="86" /></p>

	<p>Well, the simplest possible input box is just a TextBox, with the Width set (if you don&#8217;t set the Width, a TextBox will adjust to fit it&#8217;s contents, which can be really distracting).  All you need to do is this:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Boots <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;TextBox <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">220</span> <br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Title</span> <span style="color: #009900;">&quot;Enter your name&quot;</span> <span style="color: #000066;">-On_Close</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Output</span></span> <span style="color: #660033; font-weight: bold;">$BootsWindow</span>.<span style="color: #003366;">Content</span>.<span style="color: #003366;">Text</span> <br />
<span style="color: #333;">&#125;</span> <br />
&nbsp;</div>

	<p><img src="http://huddledmasses.org/images/PowerBoots/PowerBoots12.png" class="float-right-block" title="PowerBoots12.png" alt="PowerBoots12.png" width="200" height="76" /></p>

	<p>Of course, the problem with that is that it pops up this rather confusing window, when what we really wanted was a prompt, and an &#8220;Ok&#8221; button, and some event handling to make the thing behave the way we expect it to.  So lets try our first complicated form. </p>

	<p>I&#8217;ll warn you ahead of time of one thing I&#8217;m going to do here. I&#8217;m using the &#8220;Border&#8221; element to apply a colored border to the StackPanel (because it doesn&#8217;t have it&#8217;s own border parameters), but then I&#8217;m also using the WindowStyle and AllowsTransparency properties to remove the normal window chrome, creating the bare little popup you see in the screenshot. I handle the mouse down event on the main window to allow the user to drag the window around by clicking anywhere on it (except in the TextBox or on the Button, of course). Now it looks slick, and &#8212;you know&#8212; it works!</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">BootsInput</span></span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<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;">$Prompt</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Please enter your name:&quot;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Remove-<span style="font-style: normal;">Variable</span></span> textBox <span style="color: #000066;">-ErrorAction</span> SilentlyContinue<br />
&nbsp; &nbsp;Boots <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; Border <span style="color: #000066;">-BorderThickness</span> <span style="color: #cc66cc;">4</span> <span style="color: #000066;">-BorderBrush</span> <span style="color: #009900;">&quot;#BE8&quot;</span> <span style="color: #000066;">-Background</span> <span style="color: #009900;">&quot;#EFC&quot;</span> <span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;StackPanel <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">10</span> &nbsp;$<span style="color: #333;">&#40;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Label <span style="color: #660033; font-weight: bold;">$Prompt</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StackPanel <span style="color: #000066;">-Orientation</span> Horizontal $<span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TextBox <span style="color: #000066;">-OutVariable</span> global:textbox <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">150</span> <span style="color: #000066;">-On_KeyDown</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &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;">$_</span>.<span style="color: #003366;">Key</span> <span style="color: #000066;">-eq</span> <span style="color: #009900;">&quot;Return&quot;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Output</span></span> <span style="color: #660033; font-weight: bold;">$textbox</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">Text</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$BootsWindow</span>.<span style="color: #003366;">Close</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Button <span style="color: #009900;">&quot;Ok&quot;</span> <span style="color: #000066;">-On_Click</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Output</span></span> <span style="color: #660033; font-weight: bold;">$textbox</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">Text</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$BootsWindow</span>.<span style="color: #003366;">Close</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span> <span style="color: #000066;">-On_Load</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$textbox</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</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; &nbsp;<span style="color: #000066;">-WindowStyle</span> None <span style="color: #000066;">-AllowsTransparency</span> <span style="color: #660033; font-weight: bold;">$true</span> `<br />
&nbsp; &nbsp;<span style="color: #000066;">-On_PreviewMouseLeftButtonDown</span> <span style="color: #333;">&#123;</span> <br />
&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;">$_</span>.<span style="color: #003366;">Source</span> <span style="color: #000066;">-notmatch</span> <span style="color: #009900;">&quot;.*\.(TextBox|Button)&quot;</span><span style="color: #333;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$BootsWindow</span>.<span style="color: #003366;">DragMove</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</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><br />
&nbsp;</div>

	<p>Hopefully you can follow that, although it&#8217;s obviously over the top  <img src='http://joelbennett.net/wordpress/wp-includes/' alt=':)' class='wp-smiley' /> . We handle the KeyDown event on the TextBox (if the Key is the Return key), and we also handle the click on the Button.  In both cases, we&#8217;ll write out the text that was entered, and use the special $BootsWindow variable to close the window.  We also handle the Load event for the window, to make sure the focus is on the TextBox, so you can just start typing.</p>

	<h3>A final example</h3>

	<p><img src="http://huddledmasses.org/images/PowerBoots/PowerBoots13.png" class="float-right-block" title="PowerBoots13.png" alt="PowerBoots13.png" width="248" height="740" /></p>

	<p>I&#8217;ve got quite a few more examples I want to show off in part two of this tutorial, but to get you thinking about ways to integrate this with your routine tasks, and give you some ideas of what you can do with what you know already, let me give you this example of browsing photos, with a visual indication of how big the image file is. Of course, the point is that you could be visualizing, you know &#8230; anything.</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> &nbsp;<span style="color: #666666; font-style: italic;"># To get the Double-Click time</span><br />
<br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">GraphLabel</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 />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">PARAM</span> <span style="color: #333;">&#40;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Position<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</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;">$Label</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Name&quot;</span>, <br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Position<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</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;">$Value</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Length&quot;</span>, <br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Position<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">2</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">ScriptBlock</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$DoubleClickAction</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$null</span>, <br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">Int</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$max</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$null</span>, <br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">Int</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$width</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">200</span>, <br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">double</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$margin</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">2</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">Int</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$DoubleClickTime</span> <span style="color: #66cc66;">=</span> $<span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Forms</span>.<span style="color: #003366;">SystemInformation</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">DoubleClickTime</span><span style="color: #333;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>ValueFromPipeline<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#91;</span>Alias<span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;IO&quot;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">PSObject</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;">$InputObject</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">BEGIN</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$maxx</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$max</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">PROCESS</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #660033; font-weight: bold;">$maxx</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$maxx</span><span style="color: #66cc66;">=</span>@<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$InputObject</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #660033; font-weight: bold;">$Value</span> <span style="color: #333;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">foreach</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$io</span> <span style="color: #666699; font-weight: bold;">in</span> <span style="color: #660033; font-weight: bold;">$InputObject</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## This is the core part of the script ...</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## For each input, generate a grid panel with a label and a rectangle in the background</span><br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GridPanel <span style="color: #000066;">-tag</span> @<span style="color: #333;">&#123;</span>item<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$io</span>; action<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$DoubleClickAction</span><span style="color: #333;">&#125;</span> <span style="color: #000066;">-width</span> <span style="color: #660033; font-weight: bold;">$Width</span> <span style="color: #000066;">-margin</span> <span style="color: #660033; font-weight: bold;">$margin</span> $<span style="color: #333;">&#40;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Label <span style="color: #660033; font-weight: bold;">$io</span>.<span style="color: #660033; font-weight: bold;">$Label</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Rectangle <span style="color: #000066;">-HorizontalAlignment</span> Left <span style="color: #000066;">-Fill</span> <span style="color: #009900;">&quot;#9F00&quot;</span> `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">-Width</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Width</span> <span style="color: #66cc66;">*</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$io</span>.<span style="color: #009900;">&quot;$Value&quot;</span> <span style="color: #66cc66;">/</span> <span style="color: #660033; font-weight: bold;">$maxx</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#41;</span> <span style="color: #000066;">-On_MouseLeftButtonDown</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &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;">$this</span>.<span style="color: #003366;">Tag</span>.<span style="color: #003366;">Action</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <span style="color: #666666; font-style: italic;"># They passed in a doubleclick action, so lets handle it</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &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;">$global</span>:ClickTime <span style="color: #000066;">-and</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">Now</span> <span style="color: #66cc66;">-</span> <span style="color: #660033; font-weight: bold;">$ClickTime</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">TotalMilliseconds</span> <span style="color: #000066;">-lt</span> <span style="color: #660033; font-weight: bold;">$global</span>:DoubleClickTime<span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># We invoke the scriptblock </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># and pass it the original input object </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># and the grid panel object</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&amp;</span><span style="color: #660033; font-weight: bold;">$This</span>.<span style="color: #003366;">Tag</span>.<span style="color: #003366;">Action</span> <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">Tag</span>.<span style="color: #003366;">Item</span> <span style="color: #660033; font-weight: bold;">$this</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &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; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$global</span>:ClickTime <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">Now</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</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><br />
<br />
<span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">Alias</span></span> GraphLabel <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">GraphLabel</span></span><br />
<br />
<span style="color: #666666; font-style: italic;">## Example 1: list of processes with most RAM usage</span><br />
<span style="color: #666666; font-style: italic;">## DoubleClickAction is `kill`</span><br />
Boots <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #660033;">ps</span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">sort</span> PM <span style="color: #000066;">-Desc</span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">Select</span> <span style="color: #000066;">-First</span> <span style="color: #cc66cc;">20</span> <span style="color: #66cc66;">|</span> <br />
&nbsp; &nbsp; &nbsp; GraphLabel ProcessName PM <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033;">Kill</span> <span style="color: #660033; font-weight: bold;">$Args</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">Id</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$global</span>:panel<span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">Children</span>.<span style="color: #003366;">Remove</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Args</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #66cc66;">|</span> <br />
&nbsp; &nbsp;StackPanel <span style="color: #000066;">-ov</span> global:panel<br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #666666; font-style: italic;">## Example 2: list of images, with file size indicated</span><br />
<span style="color: #666666; font-style: italic;">## DoubleClickAction is `open`</span><br />
Boots <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #660033;">ls</span> ~<span style="color: #66cc66;">/</span>Pictures<span style="color: #66cc66;">/</span> <span style="color: #000066;">-recurse</span> <span style="color: #000066;">-Include</span> <span style="color: #66cc66;">*</span>.<span style="color: #003366;">jpg</span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">Sort</span> Length <span style="color: #000066;">-Desc</span> <span style="color: #66cc66;">|</span> <span style="color: #66cc66;">%</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #660033; font-weight: bold;">$Max</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$Max</span><span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Length</span><span style="color: #333;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; StackPanel <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">200</span> <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">5</span> $<span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Image <span style="color: #000066;">-Source</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">FullName</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GraphLabel Name Length <span style="color: #000066;">-Max</span> <span style="color: #660033; font-weight: bold;">$Max</span> <span style="color: #000066;">-IO</span> <span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <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;">$args</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">FullName</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;">&#41;</span> <br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span> <span style="color: #66cc66;">|</span> WrapPanel <br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">800</span><br />
&nbsp;</div>

	<p>Notice that I had to manually handle the concept of a double click, because StackPanels don&#8217;t have a Click or DoubleClick event, just MouseDown and MouseUp.  I could have stuck the stackpanel into something that does, but there&#8217;s really no need.  Also, the <code>[Diagnostics.Process]::Start</code> is the equivalent of typing the name into the run dialog.  I&#8217;m just <em>executing</em> the jpg, which makes it open in the default editor.  It&#8217;s just a sample, after all.</p>

	<h3>End note</h3>

	<p>The current version of Boots does not add threading support, which means that when you run something through Boots, execution of your script stops until the window is closed.  You can get around this somewhat in <a href="http://huddledmasses.org/wpf-from-powershell-updating-windows/">various different ways</a>, but future releases <em>will</em> support running the <span class="caps">WPF</span> window in a separate thread, and even communicating to it &#8230; at the expense of a slightly different syntax. If you need that functionality, feel free to let me know &#8230; I could use some motivation.</p>

	<p>I hope you&#8217;ve enjoyed this tour through PowerBoots, and will be able to start applying it for fun and profit.</p>]]></content:encoded>
			<wfw:commentRss>http://joelbennett.net/powerboots-tutorial-walkthrough/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>A guide to PowerShell&#8217;s Advanced Functions</title>
		<link>http://joelbennett.net/a-guide-to-advanced-functions/</link>
		<comments>http://joelbennett.net/a-guide-to-advanced-functions/#comments</comments>
		<pubDate>Tue, 03 Mar 2009 03:44:03 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Advanced Functions]]></category>
		<category><![CDATA[CTP3]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[PowerShell 2]]></category>
		<category><![CDATA[PowerShell Functions]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[WalkThrough]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1116</guid>
		<description><![CDATA[Someone asked on the PowerShell Newsgroup about writing Advanced Functions, and specifically: looking for a &#8230; guide to putting together an advanced function that is visible and usable every time I start Powershell. By visible I mean that when I do a &#8216;get-command&#8217; I want my [advanced function]s to be listed alongside all the regular [...]]]></description>
			<content:encoded><![CDATA[	<p>Someone asked on the <a href="news://msnews.microsoft.com:119/microsoft.public.windows.powershell">PowerShell Newsgroup</a> about writing Advanced Functions, and specifically:</p>

	<blockquote>
		<p>looking for a &#8230; guide to putting together an advanced function that is visible and usable every time I start Powershell. By visible I mean that when I do a &#8216;get-command&#8217; I want my [advanced function]s to be listed alongside all the regular cmdlets. What makes that possible? ... what do I need to do to make that happen? Whats the difference between an [advanced function] and a module?</p>
	</blockquote>

	<p>There are lots of articles on the Microsoft <a href="http://blogs.msdn.com/powershell/">PowerShell team blog</a> about both topics, but it seems there&#8217;s not really been any sort of step-by-step written, so I posted this to the newsgroup, and since the person who asked the original question said he found it useful, I figured I&#8217;d share it here&#8230;</p>

<span id="more-1116"></span>

	<h2>An Aside: Advanced Functions vs. Modules</h2>

	<p>Advanced functions and script modules are unrelated topics. A script module could have nothing but old PowerShell 1 -type functions, hypothetically, it could even not have any functions at all.  Advanced functions can be defined in a plain .ps1 script, in your profile, or by typing them in on the command-line, etc. The two things are essentially unrelated except for both being new features in PowerShell 2.0.</p>

	<h2>How To write an Advanced Function</h2>

	<h3>Step 1: Have something you want to do in a function.</h3>

	<p>I can&#8217;t emphasize enough how important it is that you should have a specific goal here. Writing a script for a specific purpose is much easier, because you don&#8217;t have to think about whether or not specific features are necessary, etc.</p>

	<p>For the purposes of this example, I want a function to start executables and return me the process object (so I can wait for it to finish, or whatever). I will call it Start-Process.</p>

	<h3>Step 2: Write a first step for the logic of your function. </h3>

	<p>Be sure to specify the parameters using the <code>function Name{Param(...)}</code> syntax rather than <code>function Name(...){}</code>, then you will be able to easily convert it to an advanced function by adding <code>[CmdletBinding()]</code> on the line right before the <code>Param</code> like this:</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;">Start-<span style="font-style: normal;">Process</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: #660033; font-weight: bold;">$app</span>,<span style="color: #660033; font-weight: bold;">$params</span><span style="color: #333;">&#41;</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;">$param</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <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;">$app</span>, <span style="color: #660033; font-weight: bold;">$param</span> <span style="color: #333;">&#41;</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; <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;">$app</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span></div>

	<h3>Step 3: Start writing documentation and auto-help, including your parameters.</h3>

	<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;">Start-<span style="font-style: normal;">Process</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #666666; font-style: italic;">################################################################</span><br />
<span style="color: #666666; font-style: italic;">#.Synopsis</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;Starts an application, with optional command-line parameters.</span><br />
<span style="color: #666666; font-style: italic;">#.Parameter App</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;The path to the application you want to start</span><br />
<span style="color: #666666; font-style: italic;">#.Parameter Params</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;The string consisting of all the parameters to pass to App</span><br />
<span style="color: #666666; font-style: italic;">################################################################</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: #660033; font-weight: bold;">$app</span>,<span style="color: #660033; font-weight: bold;">$params</span><span style="color: #333;">&#41;</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;">$param</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <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;">$app</span>, <span style="color: #660033; font-weight: bold;">$param</span> <span style="color: #333;">&#41;</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; <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;">$app</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span></div>

	<h3>Step 4: Exploit advanced features by marking up your parameters.</h3>

	<p>In my case, I had a look at the signature of the start method by running <code>[Diagnostics.Process]::Start.OverloadDefinitions</code> and discovered that it can take credentials too.  So I wanted an <em>optional</em> credential parameter. I specified it without a <strong>position</strong>, and made it part of a non-default parameter set.</p>

	<p>I also wanted the <code>params</code> parameter to just take any additional parameters which I pass to the function, and pass them on to the process that I&#8217;m starting. </p>

	<p>Finally, I wanted the &#8220;App&#8221; parameter to be able to come from the pipeline. Specifically, I wanted to be able to pass the output of Get-Command or Get-ChildItem to it. In order to enable that, I defined an alias on it to the relevant properties of the objects which come from those two commands</p>

	<p>Hopefully you&#8217;re getting the idea of what&#8217;s possible here &#8230;</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;">Start-<span style="font-style: normal;">Process</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #666666; font-style: italic;">################################################################</span><br />
<span style="color: #666666; font-style: italic;">#.Synopsis</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;Starts an application, with optional command-line parameters.</span><br />
<span style="color: #666666; font-style: italic;">#.Parameter App</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;The path to the application you want to start</span><br />
<span style="color: #666666; font-style: italic;">#.Parameter Params</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;The string consisting of all the parameters to pass to App</span><br />
<span style="color: #666666; font-style: italic;">#.Parameter Credential</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;PSCredential containing valid login to &quot;run as&quot; another user.</span><br />
<span style="color: #666666; font-style: italic;">################################################################</span><br />
<span style="color: #333;">&#91;</span>CmdletBinding<span style="color: #333;">&#40;</span>DefaultParameterSetName<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;NoCreds&quot;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
<span style="color: #666699; font-weight: bold;">param</span><span style="color: #333;">&#40;</span><br />
&nbsp; <span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Position<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span>,Mandatory<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ValueFromPipelineByPropertyName<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; <span style="color: #333;">&#91;</span>Alias<span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;FullName&quot;</span>,<span style="color: #009900;">&quot;Path&quot;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; <span style="color: #660033; font-weight: bold;">$app</span><br />
,<br />
&nbsp; <span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Mandatory<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span>,ParameterSetName<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;RunAs&quot;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; <span style="color: #333;">&#91;</span>Alias<span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;PSCredential&quot;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Management</span>.<span style="color: #003366;">Automation</span>.<span style="color: #003366;">PSCredential</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Credential</span><br />
,<br />
&nbsp; <span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Position<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">3</span>, Mandatory<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$false</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ValueFromRemainingArguments<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span> <span style="color: #333;">&#41;</span><br />
&nbsp; <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;">&#91;</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$params</span><br />
<span style="color: #333;">&#41;</span><br />
<br />
&nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$credential</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$cred</span><span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$credential</span>.<span style="color: #003366;">GetNetworkCredential</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</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;">$params</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$app</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$<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: #333399; font-weight: bold; font-style: italic;">join</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot; &quot;</span>,<span style="color: #660033; font-weight: bold;">$params</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$cred</span>.<span style="color: #003366;">UserName</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$credential</span>.<span style="color: #003366;">Password</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$cred</span>.<span style="color: #003366;">Domain</span> <span style="color: #333;">&#41;</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; <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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$app</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$cred</span>.<span style="color: #003366;">UserName</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$credential</span>.<span style="color: #003366;">Password</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$cred</span>.<span style="color: #003366;">Domain</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&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; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$params</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$app</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$<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: #333399; font-weight: bold; font-style: italic;">join</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot; &quot;</span>,<span style="color: #660033; font-weight: bold;">$params</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#41;</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; <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;">$app</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span></div>

	<h3>Bonus Step: Make the function be automatically defined</h3>

	<p>To be honest, there are a lot of ways to do this, here&#8217;s a few:</p>

	<h4>Option 1. Define it in your Profile.</h4>

	<p>Include the full text of the function in your Profile.ps1 script or Microsoft.PowerShell_profile.ps1 &#8230; just run <code>notepad $Profile</code> and paste the whole function in at the top, then save.</p>

	<h4>Option 2. Dot-source it in your Profile.</h4>

	<p>Save the function into a script file, e.g.: &#8220;StartProcessFunction.ps1&#8221; in your DocumentsWindowsPowerShell folder (next to your profile script) then put the line: <code>. StartProcessFunction.ps1</code> into your profile script to dot-source it. Using the dot causes the script to be loaded into the context as though you had put the contents of the script in, instead of the dot-source line. </p>

	<h4>Option 3. Import this function from a script file.  </h4>

	<p>This is basically the same as Option 2, except instead of &#8220;.&#8221; you can use <code>Import-Module</code>.  The Import-Module cmdlet, when used on a file with the .ps1 extension, is basically the same as dot-sourcing it, so there&#8217;s not a whole lot of benefit over dot-sourcing in this case.</p>

	<h4>Option 4. Put the function in a module.</h4>

	<p>You should <strong>only</strong> do this if you think <em>you might want to add additional functions</em> to the module. For instance, lets say you want to write a few other functions for working with process objects.  If you put them together in the same module (say: &#8220;ProcessUtility&#8221;) they can share script-scoped variables or private functions, and can be identified as being part of the same module using the <code>Get-Command -Module ProcessUtility</code> command. </p>

	<p>If you just have the single function, there&#8217;s really no point in changing it to a Module, but I&#8217;m not aware of any downside, either.</p>

	<h4>Option 4a. Make a module by just saving as a <code>.psm1</code></h4>

	<p>All you do is save the function into a module file, e.g.: &#8220;ProcessUtility.psm1&#8221; in your DocumentsWindowsPowerShell folder and then put the line: <code>Import-Module ProcessUtility.psm1</code> into your profile script.</p>

	<h4>Option 4b. Make a module folder.</h4>

	<p>This is by far the most extensible way to create modules. You paste the function into &#8220;ProcessUtility.psm1&#8221; and then save it into a folder &#8220;ProcessUtility&#8221; which must be created in one of your module folders. You can figure out where the module folders are by checking <code>$Env:PSMODULEPATH</code> &#8212; the normal spot is your DocumentsWindowsPowerShellModules. </p>

	<p>Once that&#8217;s done, you can import with the simpler command <code>Import-Module ProcessUtility</code> without any extension. </p>

	<p>The nice thing about creating the folder is that you can put any additional resources in that folder with it, and get at them using the <code>$PsScriptRoot</code> to define the folder. You can even add additional functions by putting them in separate files (which you can Import-Module from the main file, or you can define using a .psd1, but that&#8217;s <a href="http://huddledmasses.org/powershell-modules-metadata-and-mysteries/">another article</a>).</p>]]></content:encoded>
			<wfw:commentRss>http://joelbennett.net/a-guide-to-advanced-functions/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>PowerBoots: Loading XAML Windows in PowerShell 1.0 or 2.0</title>
		<link>http://joelbennett.net/powerboots-loading-xaml-windows-in-powershell-10-or-20/</link>
		<comments>http://joelbennett.net/powerboots-loading-xaml-windows-in-powershell-10-or-20/#comments</comments>
		<pubDate>Fri, 13 Feb 2009 07:20:07 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[PowerBoots]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[UserInterface]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[XAML]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1059</guid>
		<description><![CDATA[Awhile back I wrote a series of posts about WPF From PowerShell From PowerShell&#8221; which were about how you could load XAML in previous PowerShell 2 CTPs to create WPF user interfaces &#8230; a few people have mentioned loading XAML in PowerBoots, and a couple of people have posted other samples showing XAML even since [...]]]></description>
			<content:encoded><![CDATA[	<p>Awhile back I wrote a series of posts about <a href="http://huddledmasses.org/?s=&#34;WPF"><span class="caps">WPF</span> From PowerShell</a> From PowerShell&#8221; which were about how you could load <span class="caps">XAML</span> in previous PowerShell 2 CTPs to create <span class="caps">WPF</span> user interfaces &#8230; a few people have mentioned loading <span class="caps">XAML</span> in <a href="http://boots.CodePlex.com">PowerBoots</a>, and a couple of people have posted other samples showing <span class="caps">XAML</span> even since I published the most recent release, so I figure it&#8217;s time to point out that you really can load that <span class="caps">XAML</span> into Boots, and get all the threading and other support.  </p>

	<p>Just for fun, I&#8217;m going to rehash an earlier post about <a href="http://huddledmasses.org/wpf-from-powershell-updating-windows/">updating windows</a> to show how you can go about this using PowerBoots, and hopefully show that it&#8217;s a little easier (and a lot more async).  Compare and contrast the code in this article with that one, just for fun.</p>

	<h2>This works with any version of PowerShell</h2>

	<p>Unlike the original article, most this code (except where explicitly mentioned) works on either PowerShell v2 with PowerBoots, or PowerShell 1.0 with <strong>PoshWPF</strong>, a snapin that is part of the PowerBoots module but is also released separately&#8230; Also, unlike those previous posts, this does <em>not</em> require you to be running PowerShell.exe with the -<span class="caps">STA</span> switch, since the New-BootsWindow cmdlet takes care of threading for us.</p>

<span id="more-1059"></span>

	<h3>The simple splash screen</h3>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #660033; font-weight: bold;">$Splash</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">BootsWindow</span></span> <span style="color: #000066;">-Async</span> <span style="color: #000066;">-Passthru</span> <span style="color: #000066;">-SourceTemplate</span> @<span style="color: #009900;">&quot;<br />
&lt;window xmlns=&quot;</span>http:<span style="color: #66cc66;">//</span>schemas.<span style="color: #003366;">microsoft</span>.<span style="color: #003366;">com</span><span style="color: #66cc66;">/</span>winfx<span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2006</span><span style="color: #66cc66;">/</span>xaml<span style="color: #66cc66;">/</span>presentation<span style="color: #009900;">&quot; <br />
&nbsp; &nbsp; &nbsp; &nbsp; windowstyle=&quot;</span>None<span style="color: #009900;">&quot; allowstransparency=&quot;</span>True<span style="color: #009900;">&quot; opacity=&quot;</span><span style="color: #cc66cc;">0.8</span><span style="color: #009900;">&quot; <br />
&nbsp; &nbsp; &nbsp; &nbsp; topmost=&quot;</span>True<span style="color: #009900;">&quot; sizetocontent=&quot;</span>WidthAndHeight<span style="color: #009900;">&quot; <br />
&nbsp; &nbsp; &nbsp; &nbsp; windowstartuplocation=&quot;</span>CenterOwner<span style="color: #009900;">&quot; showintaskbar=&quot;</span>False<span style="color: #009900;">&quot;&gt;<br />
&nbsp; &nbsp;&lt;img source=&quot;</span>http:<span style="color: #66cc66;">//</span>dilbert.<span style="color: #003366;">com</span><span style="color: #66cc66;">/</span>dyn<span style="color: #66cc66;">/</span>str_strip<span style="color: #66cc66;">/</span>000000000<span style="color: #66cc66;">/</span>00000000<span style="color: #66cc66;">/</span>0000000<span style="color: #66cc66;">/</span>000000<span style="color: #66cc66;">/</span><span style="color: #cc66cc;">40000</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">1000</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">200</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">41215</span><span style="color: #66cc66;">/</span>41215.<span style="color: #003366;">strip</span>.<span style="color: #003366;">print</span>.<span style="color: #003366;">gif</span><span style="color: #009900;">&quot; <br />
&nbsp; &nbsp; &nbsp; &nbsp; height=&quot;</span><span style="color: #cc66cc;">177</span><span style="color: #009900;">&quot;&gt;<br />
&lt;/window&gt;<br />
&quot;</span>@ <span style="color: #000066;">-Content</span> <span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$Args</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">Content</span><span style="color: #333;">&#125;</span> <span style="color: #000066;">-On_MouseDown</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">DragMove</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><br />
<span style="color: #666666; font-style: italic;"># Imagine this is your script, working ...</span><br />
<span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">Sleep</span></span> <span style="color: #cc66cc;">3</span><br />
<span style="color: #666666; font-style: italic;"># And now you're done, and want to close it</span><br />
<span style="color: #0066cc; font-style: italic;">Invoke-<span style="font-style: normal;">BootsWindow</span></span> <span style="color: #660033; font-weight: bold;">$Splash</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$Splash</span>.<span style="color: #003366;">Close</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span></div>

	<p>This is, of course, not a great example of why you would want to use <span class="caps">XAML</span>, since it reads virtually the same as it would if you wrote it in PowerBoots (if you were on PowerShell v2):</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666666; font-style: italic;">## This requires PowerBoots which is (as of this writing) is still v2 only...</span><br />
<span style="color: #660033; font-weight: bold;">$window</span> <span style="color: #66cc66;">=</span> Boots <span style="color: #000066;">-Async</span> <span style="color: #000066;">-Passthru</span> &nbsp;<span style="color: #000066;">-Content</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp;Image <span style="color: #000066;">-Height</span> <span style="color: #cc66cc;">177</span> <span style="color: #000066;">-Source</span> http:<span style="color: #66cc66;">//</span>dilbert.<span style="color: #003366;">com</span><span style="color: #66cc66;">/</span>dyn<span style="color: #66cc66;">/</span>str_strip<span style="color: #66cc66;">/</span>000000000<span style="color: #66cc66;">/</span>00000000<span style="color: #66cc66;">/</span>0000000<span style="color: #66cc66;">/</span>000000<span style="color: #66cc66;">/</span><span style="color: #cc66cc;">40000</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">1000</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">200</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">41215</span><span style="color: #66cc66;">/</span>41215.<span style="color: #003366;">strip</span>.<span style="color: #003366;">print</span>.<span style="color: #003366;">gif</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-WindowStyle</span> None <span style="color: #000066;">-AllowsTransparency</span> <span style="color: #660033; font-weight: bold;">$True</span> <span style="color: #000066;">-Opacity</span> <span style="color: #cc66cc;">0.8</span> <span style="color: #000066;">-Topmost</span> <span style="color: #660033; font-weight: bold;">$True</span> <span style="color: #000066;">-WindowStartupLocation</span> CenterOwner <span style="color: #000066;">-ShowInTaskbar</span> <span style="color: #660033; font-weight: bold;">$False</span> <span style="color: #000066;">-On_MouseDown</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">DragMove</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;"># Imagine this is your script, working ...</span><br />
<span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">Sleep</span></span> <span style="color: #cc66cc;">3</span><br />
<span style="color: #666666; font-style: italic;"># And now you're done, and want to close it</span><br />
<span style="color: #0066cc; font-style: italic;">Invoke-<span style="font-style: normal;">BootsWindow</span></span> <span style="color: #660033; font-weight: bold;">$Splash</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$Splash</span>.<span style="color: #003366;">Close</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span></div>

	<p>The big difference, of course, is that this is fully aync, so unlike the splash screen in the original post, we don&#8217;t have to do any tricks to get it to render, and we are able to add a MouseDown handler so you can drag the splash screen around while you wait for it to go away&#8230;</p>

	<h2>And now, for something completely different</h2>

	<p>Let&#8217;s skip over the very useful information I already wrote about the properties you can set on the &#8220;Window&#8221; object in <span class="caps">WPF</span> (all of which are parameters to New-BootsWindow, by the way) and cut straight to the second example &#8230; I&#8217;ve tweaked an old <span class="caps">WPF</span> sample I had and ported it to PowerShell, <em>and now, adapted it for PoshWpf</em> ... the <a href="http://huddledmasses.org/wpf-from-powershell-updating-windows/clock/" rel="attachment wp-att-528">xaml file is here</a> and I&#8217;ll show you the code just as soon as you take a look at the screenshot (trust me, the code will make more sense if you&#8217;ve seen the output):</p>

	<p><img src="http://huddledmasses.org/images/2008-05-09_0029.png" title="A clock, with working CPU and RAM bars" alt="A clock, with working CPU and RAM bars" width="783" height="312" /></p>

	<p>So basically, the UI is defined in a href=&#8216;http://HuddledMasses.org/wpf-from-powershell-updating-windows/clock/&#8217; rel=&#8216;attachment wp-att-528&#8217;&gt;that xaml file, and all we need to do in the script is update the time, update the <span class="caps">CPU</span> bar, and update the <span class="caps">RAM</span> bar&#8230;</p>

	<h3>Please notice the content block</h3>

	<p>PowerBoots passes the window object itself into the content scriptblock, and the content block <strong>must</strong> output the content you want in the window. Of course, if you&#8217;re loading a fully defined window in <span class="caps">XAML</span>, you don&#8217;t want to change the Window&#8217;s content, but you have to return something, so you should just return &#8230; the content that&#8217;s already in the window. Ie: <code>$Args[0].Content</code></p>

	<p>One other point, for those of you who are going to become <span class="caps">XAML</span> masters. In PowerBoots you can define just the styles and templates as resources in a separate <span class="caps">XAML</span> file which you can load with the <code>Add-BootsTemplate</code>, and then you can still create the UI in <span class="caps">XAML</span> or useing PowerBoots syntax.  In my case, this <span class="caps">XAML</span> was edited  using <a href="http://www.kaxaml.com/">kaxaml</a> (it could have been Expression Blend, or Visual Studio), and kaxaml doesn&#8217;t make it easy to separate out resource files, so I just left it all in one file:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;Initializing Performance Counters, please have patience&quot;</span> <span style="color: #000066;">-fore</span> Cyan<br />
<span style="color: #666666; font-style: italic;">### Import PoshWpf module</span><br />
<span style="color: #0066cc; font-style: italic;">Import-<span style="font-style: normal;">Module</span></span> PowerBoots<br />
<span style="color: #666666; font-style: italic;">### Or, on v1:</span><br />
<span style="color: #666666; font-style: italic;"># Add-PSSnapin PoshWpf</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$script</span>:cpu <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;">Diagnostics</span>.<span style="color: #003366;">PerformanceCounter</span> <span style="color: #009900;">&quot;Processor&quot;</span>, <span style="color: #009900;">&quot;% Processor Time&quot;</span>, <span style="color: #009900;">&quot;_Total&quot;</span><br />
<span style="color: #660033; font-weight: bold;">$script</span>:ram <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;">Diagnostics</span>.<span style="color: #003366;">PerformanceCounter</span> <span style="color: #009900;">&quot;Memory&quot;</span>, <span style="color: #009900;">&quot;Available KBytes&quot;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## get initial values, because the counters don't work until the second call</span><br />
<span style="color: #660033; font-weight: bold;">$null</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$script</span>:cpu.<span style="color: #003366;">NextValue</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
<span style="color: #660033; font-weight: bold;">$null</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$script</span>:ram.<span style="color: #003366;">NextValue</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
<span style="color: #660033; font-weight: bold;">$script</span>:maxram <span style="color: #66cc66;">=</span> <span style="color: #333;">&#40;</span><span style="color: #660033;">gwmi</span> Win32_OperatingSystem<span style="color: #333;">&#41;</span>.<span style="color: #003366;">TotalVisibleMemorySize</span><br />
<br />
<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;Loading XAML window...&quot;</span> <span style="color: #000066;">-fore</span> Cyan<br />
<span style="color: #666666; font-style: italic;">## Load the XAML and show the window. It won't be updating itself yet...</span><br />
<span style="color: #660033; font-weight: bold;">$clock</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">BootsWindow</span></span> <span style="color: #000066;">-Async</span> <span style="color: #000066;">-Passthru</span> <span style="color: #000066;">-Content</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$Args</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">Content</span> <br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-FileTemplate</span> <span style="color: #009900;">&quot;C:\Users\Joel\Documents\WindowsPowerShell\Scripts\Demo\clock.xaml&quot;</span> <br />
<br />
<span style="color: #666666; font-style: italic;">## Create a script block which will update the UI by changing the Resources!</span><br />
<span style="color: #660033; font-weight: bold;">$counter</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>;<br />
<span style="color: #660033; font-weight: bold;">$updateBlock</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># Update the clock</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$clock</span>.<span style="color: #003366;">Resources</span><span style="color: #333;">&#91;</span><span style="color: #009900;">&quot;Time&quot;</span><span style="color: #333;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">Now</span>.<span style="color: #003366;">ToString</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;hh:MM.ss&quot;</span><span style="color: #333;">&#41;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># We only want to update the counters at most once a second</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># Otherwise their values are invalid and ...</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># The CPU counter fluctuates from 0 to the real number</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;">$counter</span><span style="color: #66cc66;">++</span> <span style="color: #000066;">-eq</span> <span style="color: #cc66cc;">4</span> <span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$counter</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Update the CPU counter with the absolute value and the percentage</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$cu</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$cpu</span>.<span style="color: #003366;">NextValue</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$clock</span>.<span style="color: #003366;">Resources</span>.<span style="color: #003366;">CpuP</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$cu</span> <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">100</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$clock</span>.<span style="color: #003366;">Resources</span>.<span style="color: #003366;">Cpu</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;{0:0.0}%&quot;</span> <span style="color: #000066;">-f</span> <span style="color: #660033; font-weight: bold;">$cu</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Update the RAM counter with the absolute value and the percentage</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$rm</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$ram</span>.<span style="color: #003366;">NextValue</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$clock</span>.<span style="color: #003366;">Resources</span>.<span style="color: #003366;">RamP</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$rm</span> <span style="color: #66cc66;">/</span> <span style="color: #660033; font-weight: bold;">$maxram</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$clock</span>.<span style="color: #003366;">Resources</span>.<span style="color: #003366;">Ram</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;{0:0.00}Mb&quot;</span> <span style="color: #000066;">-f</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$rm</span><span style="color: #66cc66;">/</span>1MB<span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## Now we need to call that scriptblock on a timer. That's easy, but it</span><br />
<span style="color: #666666; font-style: italic;">## must be done on the window's thread, so we use Invoke-BootsWindow.</span><br />
<span style="color: #666666; font-style: italic;">## Notice the first argument is the window we want to run the script in</span><br />
<span style="color: #0066cc; font-style: italic;">Invoke-<span style="font-style: normal;">BootsWindow</span></span> <span style="color: #660033; font-weight: bold;">$clock</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## We'll create a timer</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$global</span>:timer <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;">Threading</span>.<span style="color: #003366;">DispatcherTimer</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## Which will fire 4 times every second</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$timer</span>.<span style="color: #003366;">Interval</span> <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>TimeSpan<span style="color: #333;">&#93;</span></span><span style="color: #009900;">&quot;0:0:0.25&quot;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## And will invoke the $updateBlock</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$timer</span>.<span style="color: #003366;">Add_Tick</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$updateBlock</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## Now start the timer running</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$timer</span>.<span style="color: #660033;">Start</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## And just like that, the $UpdateBlock is running 4x a second</span><br />
<span style="color: #666666; font-style: italic;">## and the clock is working. &nbsp;Pretty cool, right?</span><br />
<span style="color: #666666; font-style: italic;">## what's really cool is ... we still have full control in the console</span><br />
<span style="color: #666666; font-style: italic;">## so we can add these events afterward:</span><br />
<br />
<span style="color: #666666; font-style: italic;">## If we wanted to, say, handle mouse events to let you drag the window or close it ...</span><br />
<span style="color: #0066cc; font-style: italic;">Invoke-<span style="font-style: normal;">BootsWindow</span></span> <span style="color: #660033; font-weight: bold;">$clock</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$clock</span>.<span style="color: #003366;">Add_MouseLeftButtonDown</span><span style="color: #333;">&#40;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Handled</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$true</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$clock</span>.<span style="color: #003366;">DragMove</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #666666; font-style: italic;"># WPF Magic!</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$clock</span>.<span style="color: #003366;">Add_MouseRightButtonDown</span><span style="color: #333;">&#40;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Handled</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$true</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$timer</span>.<span style="color: #003366;">Stop</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> &nbsp;<span style="color: #666666; font-style: italic;"># we'd like to stop that timer now, thanks.</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$clock</span>.<span style="color: #003366;">Close</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #666666; font-style: italic;"># and close the windows</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span> <span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<h2>So, meet Register-BootsEvent</h2>

	<p>If you&#8217;ve read the previous PowerBoots articles, you know that you can specify events as parameters to the PowerBoots functions, like <code>-On_Click</code> or <code>-On_MouseLeftButtonDown</code>, but in the case where you&#8217;re loading <span class="caps">XAML</span>, you can&#8217;t specify events in the <span class="caps">XAML</span> (WPF&#8217;s XamlReader won&#8217;t read <span class="caps">XAML</span> with events on it, or even with the <code>Class</code> specified, because you can&#8217;t use the associated code file anyway).  </p>

	<p>In any case, what I&#8217;m adding to the next version of PowerBoots/PoshWpf are two functions: </p>

	<ol>
		<li>Find-BootsControl takes a boots window and the <strong>Name</strong> of a control, and will (try to) find the named control. Of course, for that to work, you need to know the name of the control (your best bet is to name it yourself, but in the next version I may also issue names for elements as they are created if you don&#8217;t specify the name).</li>
	</ol>
	<ol>
		<li>Register-BootsEvent takes either a control, OR a boots window and control name, plus an event name and a ScriptBlock.  If you pass the window and name, it calls Find-BootsControl to find the control; once it has a control, it registers the ScriptBlock as a handler for the specified event.</li>
	</ol>

	<p>For now, both of these require that you&#8217;ve assigned a name to the control, but I&#8217;m working on setting things up so controls would be automatically named (based on their type, as with the old WinForms designer: like Button1, Button2, etc.), or allowing an XPath-like selection without named controls. In any case, this will work nicely for generating event handlers for <span class="caps">WPF</span> imported from <span class="caps">XAML</span>, and you can even specify the element directly (ie: if you don&#8217;t specify the -Name parameter, the event will be hooked on whatever element you pass in) so that last block of code can be written like this in the next release:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666666; font-style: italic;">## If we wanted to, say, handle mouse events to let you drag the window or close it ...</span><br />
<span style="color: #0066cc; font-style: italic;">Register-<span style="font-style: normal;">BootsEvent</span></span> <span style="color: #660033; font-weight: bold;">$clock</span> <span style="color: #000066;">-Event</span> MouseLeftButtonDown <span style="color: #000066;">-Action</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Handled</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$true</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">DragMove</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #666666; font-style: italic;"># WPF Magic!</span><br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #0066cc; font-style: italic;">Register-<span style="font-style: normal;">BootsEvent</span></span> <span style="color: #660033; font-weight: bold;">$clock</span> <span style="color: #000066;">-Event</span> MouseRightButtonDown <span style="color: #000066;">-Action</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Handled</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$true</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$timer</span>.<span style="color: #003366;">Stop</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> &nbsp;<span style="color: #666666; font-style: italic;"># we'd like to stop that timer now, thanks.</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">Close</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #666666; font-style: italic;"># and close the windows</span><br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>]]></content:encoded>
			<wfw:commentRss>http://joelbennett.net/powerboots-loading-xaml-windows-in-powershell-10-or-20/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PowerBoots and Attached Properties</title>
		<link>http://joelbennett.net/powerboots-and-attached-properties/</link>
		<comments>http://joelbennett.net/powerboots-and-attached-properties/#comments</comments>
		<pubDate>Sun, 08 Feb 2009 05:53:49 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Attached Properties]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[PowerBoots]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Syntax]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1055</guid>
		<description><![CDATA[WPF uses a concept called &#8220;Attached Properties&#8221; to handle certain things, like when you put controls into a DockPanel. Basically, anything you put inside a DockPanel has a property &#8220;Dock&#8221; which you can set &#8230; but because the property is actually defined by the DockPanel, it doesn&#8217;t show up in PowerBoots, so you can&#8217;t just [...]]]></description>
			<content:encoded><![CDATA[	<p><span class="caps">WPF</span> uses a concept called &#8220;Attached Properties&#8221; to handle certain things, like when you put controls into a DockPanel. Basically, anything you put inside a DockPanel has a property &#8220;Dock&#8221; which you can set &#8230; but because the property is actually defined by the DockPanel, it doesn&#8217;t show up in PowerBoots, so you can&#8217;t just say <code>-Dock &#34;Bottom&#34;</code> when you create the child items &#8230; at least, not yet.</p>

	<p>I&#8217;m still thinking about the best way for me to expose this in PowerBoots, because the problem is that attached properties can get attached several ways, and not just on direct children of an element. So I wanted to show you a way that you can do it <em>for now</em> so you can at least use the DockPanel effectively:</p>

	<div class="posh code posh" style="font-family:monospace;">Boots <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;DockPanel <span style="color: #000066;">-LastChildFill</span> <span style="color: #660033; font-weight: bold;">$true</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; DockPanel <span style="color: #000066;">-LastChildFill</span> <span style="color: #660033; font-weight: bold;">$true</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Button <span style="color: #009900;">&quot;OK&quot;</span> <span style="color: #000066;">-Padding</span> <span style="color: #009900;">&quot;2,0,2,0&quot;</span> <span style="color: #66cc66;">|</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ForEach<span style="color: #66cc66;">-</span>Object <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">SetValue</span><span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &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;">Controls</span>.<span style="color: #003366;">DockPanel</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">DockProperty</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &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;">Controls</span>.<span style="color: #003366;">Dock</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">Right</span><span style="color: #333;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$_</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TextBox <span style="color: #000066;">-HorizontalAlignment</span> Stretch<br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #66cc66;">|</span> ForEach<span style="color: #66cc66;">-</span>Object <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">SetValue</span><span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &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;">Controls</span>.<span style="color: #003366;">DockPanel</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">DockProperty</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &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;">Controls</span>.<span style="color: #003366;">Dock</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">Bottom</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$_</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; TextBox <span style="color: #000066;">-AcceptsReturn</span> <span style="color: #660033; font-weight: bold;">$true</span> <span style="color: #000066;">-minWidth</span> <span style="color: #cc66cc;">300</span> <span style="color: #000066;">-minHeight</span> <span style="color: #cc66cc;">200</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span></div>

	<h2>A better way &#8230;</h2>

	<p>Here&#8217;s what I&#8217;m thinking about for the next release of PowerBoots (you can take this and use it now, if you like it, but I&#8217;m really looking for, uhm &#8230; better ideas).  Basically, I have written a function: <code>Set-AttachedProperty</code>, which takes an attached property and a value, and passes through the element on the pipeline. You may want to use this in conjunction with the module I published awhile back for creating type accelerators, because it lets you run a line like this:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">Accelerator</span></span> DockPanel System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Controls</span>.<span style="color: #003366;">DockPanel</span></div>

	<p>Which will let you substitute <code>[DockPanel]</code> for <code>[System.Windows.Controls.DockPanel]</code> &#8230; and of course, you can use it for all the types you want to use attached properties from, and it&#8217;s a real lifesaver if you need to use a bunch of them.  Of course, in this particular example, we really only need to use a single attached property, so it&#8217;s enough to define that property ahead of time:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #660033; font-weight: bold;">$DockProperty</span> <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Controls</span>.<span style="color: #003366;">DockPanel</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">DockProperty</span></div>

	<p>Then you can use the <code>Set-AttachedProperty</code> function through an alias <code>sap</code>, and rewrite that huge block above like this:</p>

	<div class="posh code posh" style="font-family:monospace;">Boots <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;DockPanel <span style="color: #000066;">-LastChildFill</span> <span style="color: #660033; font-weight: bold;">$true</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; DockPanel <span style="color: #000066;">-LastChildFill</span> <span style="color: #660033; font-weight: bold;">$true</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Button <span style="color: #009900;">&quot;OK&quot;</span> <span style="color: #000066;">-Padding</span> <span style="color: #009900;">&quot;2,0,2,0&quot;</span> <span style="color: #66cc66;">|</span> sap <span style="color: #660033; font-weight: bold;">$DockProperty</span> Right<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TextBox <span style="color: #000066;">-HorizontalAlignment</span> Stretch<br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #66cc66;">|</span> sap <span style="color: #660033; font-weight: bold;">$DockProperty</span> Bottom<br />
&nbsp; &nbsp; &nbsp; TextBox <span style="color: #000066;">-AcceptsReturn</span> <span style="color: #660033; font-weight: bold;">$true</span> <span style="color: #000066;">-minWidth</span> <span style="color: #cc66cc;">300</span> <span style="color: #000066;">-minHeight</span> <span style="color: #cc66cc;">200</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span></div>

	<p>Pretty slick, right? And it will even print out the list of values in the error message if you invoke it with an invalid value against an enum property like dock:  <code>sap $DockProperty &#34;&#34;</code> &#8230; The problem I have with it is that you have to predefine your <code>$DockProperty</code> variable, and you can&#8217;t just define it against the root class.  So I&#8217;m trying to find a way to tweak the dynamic property generation to make it so that the pipe into <code>| sap $DockProperty Bottom</code>  can just be a parameter to the original element: <code>-Dock Bottom</code> &#8230; if I can&#8217;t find that, in the worst case scenario, I&#8217;ll just add an -AttachedProperties parameter with a hashtable of $DockProperty,&#8220;Bottom&#8221; or something &#8230; <em>what do you think?</em>  </p>

	<p>In any case, here&#8217;s the sap function, for now, and remember to use it as part of the pipeline:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">AttachedProperty</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #333;">&#91;</span>CmdletBinding<span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
<span style="color: #666699; font-weight: bold;">PARAM</span><span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Position<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">0</span>,Mandatory<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span><span style="color: #333;">&#41;</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;">DependencyProperty</span><span style="color: #333;">&#93;</span></span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Property</span><br />
,<br />
&nbsp; &nbsp;<span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Mandatory<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span>,ValueFromPipeline<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Element</span><br />
<span style="color: #333;">&#41;</span><br />
<span style="color: #666699; font-weight: bold;">DYNAMICPARAM</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$paramDictionary</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;">Management</span>.<span style="color: #003366;">Automation</span>.<span style="color: #003366;">RuntimeDefinedParameterDictionary</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Param1</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;">Management</span>.<span style="color: #003366;">Automation</span>.<span style="color: #003366;">RuntimeDefinedParameter</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Param1</span>.<span style="color: #003366;">Name</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Value&quot;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># $Param1.Attributes.Add( (New-ParameterAttribute -Position 1) )</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Param1</span>.<span style="color: #003366;">Attributes</span>.<span style="color: #003366;">Add</span><span style="color: #333;">&#40;</span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> System.<span style="color: #003366;">Management</span>.<span style="color: #003366;">Automation</span>.<span style="color: #003366;">ParameterAttribute</span> <span style="color: #000066;">-Property</span> @<span style="color: #333;">&#123;</span> Position <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span> <span style="color: #333;">&#125;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Param1</span>.<span style="color: #003366;">ParameterType</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$Property</span>.<span style="color: #003366;">PropertyType</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$paramDictionary</span>.<span style="color: #003366;">Add</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;Value&quot;</span>, <span style="color: #660033; font-weight: bold;">$Param1</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">return</span> <span style="color: #660033; font-weight: bold;">$paramDictionary</span><br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #666699; font-weight: bold;">PROCESS</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Element</span>.<span style="color: #003366;">SetValue</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Property</span>, <span style="color: #660033; font-weight: bold;">$Param1</span>.<span style="color: #003366;">Value</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Element</span><br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Alias</span></span> sap <span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">AttachedProperty</span></span><br />
&nbsp;</div>]]></content:encoded>
			<wfw:commentRss>http://joelbennett.net/powerboots-and-attached-properties/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PowerBoots: PowerShell GUIs are now multi-threading</title>
		<link>http://joelbennett.net/powerboots-now-multi-threaded/</link>
		<comments>http://joelbennett.net/powerboots-now-multi-threaded/#comments</comments>
		<pubDate>Sat, 07 Feb 2009 23:21:24 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[DSL]]></category>
		<category><![CDATA[GUIs]]></category>
		<category><![CDATA[PowerBoots]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[User Interface]]></category>
		<category><![CDATA[Visifire]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1045</guid>
		<description><![CDATA[Announcing the release of PowerBoots 0.1 This release of PowerBoots is the most exciting release software I&#8217;ve cranked out in awhile. It finally has almost all of the features that I have thought of so far (we&#8217;re still missing proper support for attached properties). You can create pretty much any WPF element, including ones I [...]]]></description>
			<content:encoded><![CDATA[	<h2>Announcing the release of PowerBoots 0.1</h2>

	<p>This release of PowerBoots is the most exciting release software I&#8217;ve cranked out in awhile. It finally has almost all of the features that I have thought of so far (we&#8217;re still missing proper support for attached properties).</p>

	<ul>
		<li>You can create pretty much any <span class="caps">WPF</span> element, including ones I haven&#8217;t thought of adding yet (use Add-BootsFunction to add a new assembly or a single type).</li>
		<li>You can create graphical user interfaces from <a class="zem_slink" href="http://www.microsoft.com/powershell" title="Windows PowerShell" rel="homepage">PowerShell</a> 1.0 all the way to the latest PowerShell 2.0 <span class="caps">CTP</span>.</li>
		<li>You can create your <span class="caps">WPF</span> GUIs <code>-Async</code> <span class="em1">in their own threads</span>, or inline in <a href="http://PoshConsole.CodePlex.com">PoshConsole</a>, or as synchronous dialogs that return values when they close.</li>
		<li>You can capture screenshots of your UIs (eg: <a href="http://huddledmasses.org/powerboots-output-graphs-to-images-from-powershell/">generate .jpg charts from Visifire</a>). You can even do so <strong>without displaying them!</strong></li>
		<li>You can take advantage of <a class="zem_slink" href="http://en.wikipedia.org/wiki/Extensible_Application_Markup_Language" title="Extensible Application Markup Language" rel="wikipedia">XAML</a> Data Templates to generate graphical representations of any objects in the PowerShell pipeline.</li>
	</ul>
	<ul>
		<li>You can add to, or modify your running GUIs from the command line.</li>
	</ul>

	<p>You can read the &#8220;and much much more&#8221; between the lines right?</p>

<object height="280" width="640"> 
<param name="movie" value="http://content.screencast.com/users/Jaykul/folders/Jing/media/5688481d-eb1e-4d47-b91b-31dc9584e465/jingswfplayer.swf" />
<param name="quality" value="high" /> 
<param name="bgcolor" value="#FFFFFF" />
<param name="flashVars" value="thumb=http://content.screencast.com/users/Jaykul/folders/Jing/media/5688481d-eb1e-4d47-b91b-31dc9584e465/FirstFrame.jpg&amp;width=640&amp;height=280&amp;content=http://content.screencast.com/users/Jaykul/folders/Jing/media/5688481d-eb1e-4d47-b91b-31dc9584e465/PowerBootsMonitoring.swf" />
<param name="allowFullScreen" value="true" /> 
<param name="scale" value="showall" />
<param name="allowScriptAccess" value="always" />
<param name="base" value="http://content.screencast.com/users/Jaykul/folders/Jing/media/5688481d-eb1e-4d47-b91b-31dc9584e465/" />  <embed src="http://content.screencast.com/users/Jaykul/folders/Jing/media/5688481d-eb1e-4d47-b91b-31dc9584e465/jingswfplayer.swf" quality="high" bgcolor="#FFFFFF" type="application/x-shockwave-flash" allowscriptaccess="always" flashvars="thumb=http://content.screencast.com/users/Jaykul/folders/Jing/media/5688481d-eb1e-4d47-b91b-31dc9584e465/FirstFrame.jpg&amp;width=640&amp;height=280&amp;content=http://content.screencast.com/users/Jaykul/folders/Jing/media/5688481d-eb1e-4d47-b91b-31dc9584e465/PowerBootsMonitoring.swf" allowfullscreen="true" base="http://content.screencast.com/users/Jaykul/folders/Jing/media/5688481d-eb1e-4d47-b91b-31dc9584e465/" scale="showall" height="280" width="640"></embed>
</object><span id="more-1045"></span>

	<h3>Breaking Changes and New Features</h3>

	<p>The big new thing in this release is that I&#8217;ve added a compiled module (PoshWpf) which contains several cmdlets, and gives us some major new functionality in exchange for a break in the syntax.  The syntax change is pretty simple, really, and the new threading functionality (and PowerShell 1.0 support for the cmdlets) is awesome (if I do say so myself)  <img src='http://joelbennett.net/wordpress/wp-includes/' alt=':)' class='wp-smiley' /> .</p>

	<h4>New-BootsWindow replaces Out-Boots</h4>

	<p>New-BootsWindow is a compiled cmdlet, and in addition to all of the features already present in the now deprecated Out-Boots, it features a few important new features. Incidentally, I&#8217;ve actually left the old Out-Boots function in the code, just in case, but I&#8217;ve renamed it to Out-BootsWindow, and changed the &#8220;Boots&#8221; alias to point at the new New-BootsWindow.</p>

	<h5>ScriptBlock Content</h5>

	<p>The biggest change to your scripts is going to be a change from piping objects into Out-Boots, to creating your objects in a scriptblock parameter to New-BootsWindow. This allows the script to create the UI elements entirely within the new thread.  Since we&#8217;ve switched the &#8220;Boots&#8221; alias to point to New-BootsWindow, anything you used to write like this: <code> ... | Boots</code> you should now be able to write like this: <code>Boots { ... }</code> and get the same result. Additionally, to make the syntax of our little <span class="caps">DSL</span> just a little bit slicker, I now allow you to pass scriptblocks for just about everything (particularly for the main content parameters).  An example that looked like this, before:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
StackPanel <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">10</span> <span style="color: #000066;">-Children</span> $<span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp;TextBlock <span style="color: #009900;">&quot;The 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;TextBlock <span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">24</span> <span style="color: #000066;">-Inlines</span> $<span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; Hyperlink $<span style="color: #333;">&#40;</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;Can PowerBoots do async threads?&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#41;</span> <span style="color: #000066;">-NavigateUri</span> <span style="color: #009900;">&quot;_&quot;</span> <span style="color: #000066;">-On_RequestNavigate</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$global</span>:Answer<span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">Visibility</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Visible&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#41;</span><br />
&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;">&#40;</span><br />
&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; <span style="color: #009900;">&quot;Not this way!&quot;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#41;</span> <span style="color: #000066;">-OV</span> global:Answer <span style="color: #000066;">-Visibility</span> Collapsed<br />
<span style="color: #333;">&#41;</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Out-<span style="font-style: normal;">BootsWindow</span></span> </div>

	<p>Can actually end up like this, now (and produce the same output):</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Boots <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: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; TextBlock <span style="color: #009900;">&quot;The 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: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Hyperlink <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Bold <span style="color: #009900;">&quot;Q. &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&quot;Can PowerBoots do async threads?&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</span> <span style="color: #000066;">-NavigateUri</span> <span style="color: #009900;">&quot; &quot;</span> <span style="color: #000066;">-On_RequestNavigate</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$global</span>:Answer<span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">Visibility</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Visible&quot;</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: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Span <span style="color: #009900;">&quot;A. &quot;</span> <span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">24</span> <span style="color: #000066;">-FontWeight</span> Bold <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&quot;Oh yes we can!&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #000066;">-OV</span> global:Answer <span style="color: #000066;">-Visibility</span> Collapsed<br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span> </div>

	<h5>The Async Parameter</h5>

	<p>If you specify <code>-Async</code>, the new popup window is spawned in a new thread, and all of it&#8217;s events etc. are handled on that separate thread.  This is actually what happens anyway when you run New-BootsWindow from a host that&#8217;s not running in <code>STA</code> mode, but if you don&#8217;t specify -Async, we fake the synchronous result so that you can use <code>Write-Output</code> within a dialog to prompt and get a result &#8220;returned&#8221; from New-BootsWindow. The cool thing about <code>-Async</code> is that not only can you continue using your console, you can run additional windows, and you can even use the new <code>Invoke-BootsWindow</code> cmdlet to run scripts within that window&#8217;s thread so you can change the <span class="caps">GUI</span> content, etc.</p>

	<h5>The Passthru Parameter</h5>

	<p>The <code>-Passthru</code> parameter causes the New-BootsWindow to output the window object that it has created to the pipeline.  It&#8217;s really only useful when you also specify the <code>-Async</code> parameter, since otherwise the window will be closed before you can use it.  Furthermore, because it&#8217;s a UI element, you can&#8217;t really do very much directly on the Window object without the use of our other cmdlets</p>

	<h4>The BootsWindow Collection</h4>

	<p>When you create a New-BootsWindow, the PowerBoots module (or snapin, for v1 users) tracks the window for you, so you can get ahold of it again later to run scripts against it, take screenshots, close it, etc.  To enable this we have three new cmdlets:</p>

	<ul>
		<li><code>Get-BootsWindow</code> returns the windows, and supports filtering by title or index.</li>
		<li><code>Remove-BootsWindow</code> will clear the list of windows that have already been closed, when called without parameters, or will close (and remove) windows that are still running if you pass a specific window, index, or window title.</li>
		<li><code>Invoke-BootsWindow</code> lets you run scriptblocks in a specific window&#8217;s thread</li>
	</ul>
	<ul>
		<li><code>Export-BootsImage</code> lets you take a screen capture of the contents of a window (or of a specific control)</li>
	</ul>

	<h4>Add and Remove Templates</h4>

	<p>There are three new cmdlets for managing <span class="caps">XAML</span> source files.  Add, Remove, and Get BootsTemplate. These allow you to specify additional <span class="caps">XAML</span> files (beyond the default.xaml), and those templates are always automatically loaded into each new window, so you can specify styles, resources, data templates and even new <span class="caps">WPF</span> Templates (I&#8217;ll throw up an example using the ones from <a href="http://WpfThemes.CodePlex.com"><span class="caps">WPF</span> Themes</a> later this weekend).</p>

	<h2>Download and Source now hosted on CodePlex</h2>

	<p>Well, that&#8217;s enough for the highlights for now. I&#8217;ve included the first blush efforts of a ps1xml format and data types files (which get loaded automatically with the module on PowerShell 2, but not v1), and I haven&#8217;t yet written up the help files for the compiled cmdlets.  Look for more help and examples and maybe even some cleaned up sample videos over the weekend. Please file any problems you have as bugs on <a href="http://boots.codeplex.com">Boots.CodePlex.com</a>, where you can find the <a href="http://boots.codeplex.com/Release/ProjectReleases.aspx">latest release</a>.</p>

	<p>I do want to point out that I&#8217;ve left the version number at 0.1 &#8230; I really want to add some explicit support for attached properties, and I&#8217;m sure I&#8217;ve missed a bug or two somewhere along the line  <img src='http://joelbennett.net/wordpress/wp-includes/' alt=';)' class='wp-smiley' />  lets consider this another <strong>solid</strong> beta release.</p>

<div class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/0b00377a-7c0c-4703-abb8-4effc7afedbd/" title="Zemified by Zemanta"><img class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=0b00377a-7c0c-4703-abb8-4effc7afedbd" alt="Reblog this post [with Zemanta]" /></a></div>]]></content:encoded>
			<wfw:commentRss>http://joelbennett.net/powerboots-now-multi-threaded/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PowerShell Modules, Metadata, and Mysteries</title>
		<link>http://joelbennett.net/powershell-modules-metadata-and-mysteries/</link>
		<comments>http://joelbennett.net/powershell-modules-metadata-and-mysteries/#comments</comments>
		<pubDate>Wed, 04 Feb 2009 00:09:34 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Rants]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/powershell-modules-metadata-and-mysteries/</guid>
		<description><![CDATA[I&#8217;ve finally finished the first public release of PoshWpf, and was getting ready to ship it as part of a new release of my PowerBoots module. The idea is that I need PoshWpf itself to be a stand-alone module so that it can be used in production with PowerShell 1.0, until such time as PowerShell [...]]]></description>
			<content:encoded><![CDATA[	<p>I&#8217;ve finally finished the first public release of PoshWpf, and was getting ready to ship it as part of a new release of my <a href="/PowerBoots">PowerBoots</a> module.  The idea is that I need PoshWpf itself to be a stand-alone module so that it can be used in production with PowerShell 1.0, until such time as PowerShell 2.0 goes gold, and we can get that into production use, so we can use PowerBoots.  PowerBoots is a script module, and it uses dynamic parameters, so most of it can&#8217;t really be done in v1, but you can use PoshWpf as a snapin, including the New-PowerBoots cmdlet, to do asynchronous multi-threaded UIs like my &#8220;ping monitor demo.&#8221;</p>

	<p>So I need to update the .psd1 Module Metadata file for PowerBoots to include PoshWpf &#8230; I <a href="http://www.nivot.org/2008/12/30/PowerShellCTP3AndModuleManifests.aspx">read up</a> on Oisin Grehan&#8217;s blog about the possible properties in the module manifest, and decide that I could do this by packaging PoshWpf as a NestedModule in PowerBoots.  There&#8217;s not a whole lot of point in distributing PoshWpf as it&#8217;s own module &#8212; Version 1 users will need to install it as a Snapin anyway, since PowerShell 1.0 doesn&#8217;t support modules, and I expect PowerShell 2.0 users to use it with PowerBoots.</p>

	<h2>Nested Modules</h2>

	<p>So I open up the PowerBoots.psd1 file, and add the line: <code>NestedModules=&#34;PoshWpf&#34;</code> and fire up PowerShell and run <code>Import-Module PowerBoots</code> &#8230; and get this Error:<br />
<span class="em2">Import-Module : The module to process &#8216;PowerBoots&#8217;, listed in module manifest &#8216;C:\Users\Joel\Documents\WindowsPowerShell\Modules\PowerBoots\PowerBoots.psd1&#8217; was not processed because no valid module was found in any module directory.</span></p>

	<p>Now, I have no idea what that&#8217;s supposed to mean, except that I know the way that the PowerBoots.psm1 script gets parsed is that the PowerBoots.psd1 metadata includes a line: <code>ModuleToProcess=&#34;PowerBoots.psm1&#34;</code> &#8230; I don&#8217;t know why the error doesn&#8217;t say &#8220;.psm1&#8221;, and I don&#8217;t know why <em>that</em> would be the error when what I added was the PoshWpf line, but I comment my line out and everything works, so I decide that maybe I need to say &#8220;PoshWpf.dll&#8221; (following the example that works of loading the PowerBoots script by specifying the full extension).</p>

	<p>So I move PoshWpf.dll from it&#8217;s subdirectory right into the PowerBoots folder, and I change my line to: <code>NestedModules=&#34;PoshWpf.dll&#34;</code> and fire up PowerShell and run <code>Import-Module PowerBoots</code> &#8230; and get this Error: <span class="em2">Import-Module : An item with the same key has already been added.</span></p>

	<p>Wow, I&#8217;m two for two: two attempts to load my module, two <em>completely incomprehensible</em> errors.</p>

<span id="more-1040"></span>

	<h2>Required Modules</h2>

	<p>I noticed on Oisin&#8217;s site earlier that he mentioned something I didn&#8217;t know.  There&#8217;s a <code>RequiredModules</code> property for module metadata, which I knew about, but although it will not try to load the module, Oisin&#8217;s post mentions that &#8220;this could optionally be done using a script in ScriptsToProcess&#8221; ... so I check that out.</p>

	<p>I put PoshWpf.dll in it&#8217;s own Module folder: C:\Users\Joel\Documents\WindowsPowerShell\Modules\PoshWpf and I check that I can load it: <code>Import-Module PoshWpf</code> and then check that I can load PowerBoots: <code>Import-Module PowerBoots</code>.  Success!  Now to put in a script, with just that one line. I call it &#8220;PoshWpf.ps1&#8221; and put it the PowerBoots folder, and then add this line: <code>ScriptsToProcess=&#34;PoshWpf.ps1&#34;</code> and &#8230; get another error <span class="em2">Import-Module : The required module &#8216;PoshWpf&#8217; is not loaded. Load the module or remove the module from &#8216;RequiredModules&#8217; in the file &#8216;C:\Users\Joel\Documents\WindowsPowerShell\Modules\PowerBoots\PowerBoots.psd1&#8217;.</span></p>

	<p>This time it&#8217;s obvious: despite what Oisin said, the script doesn&#8217;t run before the requirement is validated, so it doesn&#8217;t work the way I thought he meant.  I switch the order of the lines around to see if it makes any difference, but it doesn&#8217;t.  I decide that if I can just load the PoshWpf module, maybe I don&#8217;t care if it&#8217;s marked as &#8220;required&#8221; (it&#8217;s not technically required, you can still use Out-Boots &#8212; it&#8217;s just deprecated).  So I comment out the requirement &#8230; and that works.</p>

	<h2>But that&#8217;s not nested </h2>

	<p>So at this point I&#8217;m almost ready to declare this a partial success and move on.  But I&#8217;m an obsessive guy, and I don&#8217;t like the fact that people who don&#8217;t read this whole article won&#8217;t know how to get a list of all the PowerBoots modules (ie: they have to run <code>Get-Command -Module PowerBoots, PoshWpf</code> because the modules are completely separate, as far as PowerShell is concerned). So I really wanted to make it a nested module.</p>

	<blockquote>
		<p>Somewhere around this point, I went to bed.  When I woke up, I was having a conversation with someone on <span class="caps">IRC</span> that made me think of using @() notation&#8230;</p>
	</blockquote>

	<p>So I went back to the beginning and try the line as: NestedModules=@("PoshWpf") but I still got the same error as before: <span class="em2">Import-Module : The module to process &#8216;PowerBoots&#8217;, listed in module manifest &#8216;C:\Users\Joel\Documents\WindowsPowerShell\Modules\PowerBoots\PowerBoots.psd1&#8217; was not processed because no valid module was found in any module directory.</span> This time I got curious, so I ran it with -Debug and -Verbose on, and suddenly I was even more confused.  The verbose output starts with the news that the loader is loading <strong>both</strong> PowerBoots.psd1 and PowerBoots.psm1, and then lists all of the functions created by PowerBoots.psm1 &#8230;</p>

	<p>After a little digging, it&#8217;s clear that what they meant was that PoshWpf wasn&#8217;t being loaded &#8230; because PowerBoots itself is loaded fine.  The error message is just completely <strong>wrong</strong>, and very frustrating.  So now I&#8217;m thinking maybe I&#8217;m just not specifying the path to the nested module right&#8230;</p>

	<p>I tried putting &#8220;.\PoshWpf&#8221; (since at this point I have PoshWpf in a subfolder of PowerBoots), and I got the old <span class="em2">Import-Module : An item with the same key has already been added</span> error again! <strong>Aarrgh</strong></p>

	<h2>Losing patience</h2>

	<p>In frustration, I changed my profile to pre-set $VerbosePreference and $DebugPreference and $WarningPreference to &#8220;Continue&#8221; so that I could get as much information as possible (since I was having to restart PowerShell to be sure that the &#8220;item with the same key&#8221; error wasn&#8217;t fake). The next thing I tried was to set the full path to the dll: <code>NestedModules=&#34;PoshWpf\PoshWpf.dll&#34;</code> and suddenly it seemed to work.  The <strong>Verbose</strong> output included the exporint and importing of the cmdlets from PoshWpf &#8230; but I was still getting the &#8220;item with the same key&#8221; error at the end.</p>

	<p>That made me kind-of curious, since the error appears to terminate loading, so to aid in debugging I commented out some code in my PowerBoots module, because that .psm1 file exports hundreds of functions, which makes it hard to tell exactly what&#8217;s happening&#8230;  and discovered that the functions from PoshWpf.dll are being double-imported &#8230; so that&#8217;s probably what caused the duplicate key error.  I try a few things, and finally resort to <code>Set-PSDebug -Step</code> &#8230;</p>

	<p>Only to discover that when the PoshWpf.psd1 file says: <code>ExportedCmdlets=&#34;Out-Wpf&#34;</code>, the system seems to export and import <em>all</em> of the cmdlets&#8230; plus all the functions from the parent module that were already exported! I have no idea what&#8217;s going on, so I&#8217;m blogging, and contacting some MVPs, maybe they&#8217;ve seen more info than I have.</p>

	<h3> <img src='http://joelbennett.net/wordpress/wp-includes/' alt='[new]' class='wp-smiley' />  A solution!</h3>

	<p>After talking to a couple people, it&#8217;s pretty clear that what I&#8217;m doing should work, but doesn&#8217;t. I switch back to my earlier attempt: move PoshWpf.dll into the root PowerBoots directory, and set the <code>NestedModules=&#34;PoshWpf&#34;</code> &#8230; of course I get the same error as before, but this time I&#8217;m running in full verbose, debug and trace mode, and I can see that all the cmdlets are actually being loaded, but the script functions are being hit oddly.</p>

	<p>In a fit of confusion I try commenting out my <code>ModuleToProcess=&#34;PowerBoots.psm1&#34;</code> line, and I put the script module into the nested modules list: <code>NestedModules=&#34;PoshWpf&#34;,&#34;PowerBoots.psm1&#34;</code> &#8230; and suddenly it <em>just works</em>.  I uncomment the function generation in PowerBoots.psm1, and it appears we&#8217;re good to go.  I can&#8217;t decide if this is a bug, or just a problem of missing documentation.  In any case, loading bot the psm1 and dll as nested modules works, and causes all of the functions and cmdlets to show up when I run <code>Get-Command -Module PowerBoots</code>, which is really the important part.  Case closed &#8212; I&#8217;ll just have to write up something on connect to see what the Microsoft team says about fixing these error messages and documenting how on earth we&#8217;re supposed to use module metadata.</p>]]></content:encoded>
			<wfw:commentRss>http://joelbennett.net/powershell-modules-metadata-and-mysteries/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>PowerBoots &#8211; Output Graphs to Images from PowerShell</title>
		<link>http://joelbennett.net/powerboots-output-graphs-to-images-from-powershell/</link>
		<comments>http://joelbennett.net/powerboots-output-graphs-to-images-from-powershell/#comments</comments>
		<pubDate>Fri, 16 Jan 2009 06:01:08 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Charting]]></category>
		<category><![CDATA[Charts]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[PowerBoots]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Visifire]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1019</guid>
		<description><![CDATA[Once I started playing with the new ability, introduced in PowerBoots 0.0.4 to easily add support for new graphical controls to PowerBoots, I found all sorts of fun widgets out there in open-source land. I&#8217;ll write about a few of them in the future, but for now I&#8217;m just going to stick with Visifire. After [...]]]></description>
			<content:encoded><![CDATA[	<p>Once I started playing with the new ability, introduced in <a href="http://huddledmasses.org/PowerBoots/">PowerBoots</a> 0.0.4 to easily add support for new graphical controls to PowerBoots, I found all sorts of fun widgets out there in open-source land.  I&#8217;ll write about a few of them in the future, but for now I&#8217;m just going to stick with Visifire.  After playing with a few examples, I finally got around to actually trying to create the graphs I needed at work which were the main source of motivation for trying to do Visifire from PowerShell anyway &#8230;</p>

	<p>The problem was, half of the need we have for graphs is to throw them up on a web-server &#8230; so I needed a way to take the <span class="caps">WPF</span> window, and capture the controls and visuals as an image. Basically, render the <span class="caps">WPF</span> control to a bitmap image, instead of to the screen.  To cut a long story short, I figured it out of course &#8212; why else would I be writing this? (<a href="http://huddledmasses.org/PowerBoots/">get it here</a>).</p>

	<h2>Out-BootsImage</h2>

	<p>So the new version of PowerBoots includes the Out-BootsImage function (aliased as BootsImage and &#8220;obi&#8221;), which basically is designed to take the filename of an image, and a boots visual element, and create a screenshot.  It can generate bmp, gif, jpg, png, tiff, and even Microsoft&#8217;s new <a href="http://www.microsoft.com/windows/windowsmedia/wmphoto" title="HD Photo">wdp</a> format, or it can output to clipboard.</p>

	<p><img src="http://huddledmasses.org/images/PowerBoots/VisiFire-BootsImage.jpg" align="right" alt="" width="200" height="150" /></p>

	<p>It&#8217;s late, so I&#8217;m not going to write any more about this &#8230; the bottom line is, if you have been looking for a (free) way to generate graphs and charts to image files in PowerShell, this will work for you: go grab <a href="http://www.visifire.com/">Visifire</a>, check out their <a href="http://www.visifire.com/silverlight_charts_gallery.php">gallery</a> for inspiration, and start churning out graphs! Just for fun, here are the samples from the help documentation (except, this is the actual script I ran, including the upload code) with screen-shots &#8230;  </p>

	<h3>Important note</h3>

	<p>When producing images of Visifire charts using this method, it is absolutely critical that <strong>you must specify <code>-Animation $false</code></strong> because otherwise you will consistenly produce images of empty charts (since by default Visifire animates all of the bars onto the screen right <em>after</em> the <code>SourceInitialized</code> event that I use to capture the image). Oh, and if you need to remove the Visifire.com watermark, there is a <code>-watermark $false</code> parameter on the <code>Chart</code> element, which turns it off.</p>

	<p><img src="http://huddledmasses.org/images/PowerBoots/VisiFire-OutImage.jpg" alt="" width="200" height="150" /></p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #660033; font-weight: bold;">$credential</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Credential</span></span><br />
<br />
Chart <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">200</span> <span style="color: #000066;">-Height</span> <span style="color: #cc66cc;">150</span> <span style="color: #000066;">-Theme</span> Theme3 <span style="color: #000066;">-Watermark</span> <span style="color: #660033; font-weight: bold;">$false</span> <span style="color: #000066;">-Animation</span> <span style="color: #660033; font-weight: bold;">$false</span> <span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp;DataSeries $<span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; 1..<span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Random</span></span> <span style="color: #000066;">-min</span> <span style="color: #cc66cc;">3</span> <span style="color: #000066;">-max</span> <span style="color: #cc66cc;">6</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">|</span> ForEach<span style="color: #66cc66;">-</span>Object &nbsp;<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DataPoint <span style="color: #000066;">-YValue</span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Random</span></span> <span style="color: #cc66cc;">100</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#41;</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Out-<span style="font-style: normal;">BootsImage</span></span> VisiFire<span style="color: #66cc66;">-</span>BootsImage.<span style="color: #003366;">jpg</span> <span style="color: #66cc66;">|</span>ForEach<span style="color: #66cc66;">-</span>Object <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Send-<span style="font-style: normal;">FTP</span></span> HuddledMasses.<span style="color: #003366;">org</span> <span style="color: #660033; font-weight: bold;">$credential</span> <span style="color: #000066;">-LocalFile</span> <span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #000066;">-Remotefile</span> <span style="color: #009900;">&quot;$imgPath/$($_.Name)&quot;</span> <br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Windows.<span style="color: #003366;">Clipboard</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">SetText</span><span style="color: #333;">&#40;</span> <span style="color: #009900;">&quot;!http://huddledmasses.org/images/PowerBoots/$($_.Name)!&quot;</span> <span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
StackPanel <span style="color: #000066;">-Margin</span> <span style="color: #009900;">&quot;10,5,10,5&quot;</span> $<span style="color: #333;">&#40;</span> <br />
&nbsp; &nbsp;Label <span style="color: #009900;">&quot;Please enter your name:&quot;</span><br />
&nbsp; &nbsp;StackPanel <span style="color: #000066;">-Orientation</span> Horizontal $<span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; TextBox <span style="color: #000066;">-OutVariable</span> global:textbox <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">150</span> <span style="color: #000066;">-On_KeyDown</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &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;">$_</span>.<span style="color: #003366;">Key</span> <span style="color: #000066;">-eq</span> <span style="color: #009900;">&quot;Return&quot;</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;">Write-<span style="font-style: normal;">Output</span></span> <span style="color: #660033; font-weight: bold;">$textbox</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">Text</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$BootsWindow</span>.<span style="color: #003366;">Close</span><span style="color: #333;">&#40;</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; &nbsp; Button <span style="color: #009900;">&quot;Ok&quot;</span> <span style="color: #000066;">-Padding</span> <span style="color: #009900;">&quot;5,0,5,0&quot;</span> <span style="color: #000066;">-Margin</span> <span style="color: #009900;">&quot;2,0,0,0&quot;</span> <span style="color: #000066;">-On_Click</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Output</span></span> <span style="color: #660033; font-weight: bold;">$textbox</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">Text</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$BootsWindow</span>.<span style="color: #003366;">Close</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#41;</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Out-<span style="font-style: normal;">BootsImage</span></span> BootsImage<span style="color: #66cc66;">-</span>Screenshot.<span style="color: #003366;">jpg</span> <span style="color: #66cc66;">|</span> ForEach<span style="color: #66cc66;">-</span>Object <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Send-<span style="font-style: normal;">FTP</span></span> HuddledMasses.<span style="color: #003366;">org</span> <span style="color: #660033; font-weight: bold;">$credential</span> <span style="color: #000066;">-LocalFile</span> <span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #000066;">-Remotefile</span> <span style="color: #009900;">&quot;$imgPath/$($_.Name)&quot;</span> <br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Windows.<span style="color: #003366;">Clipboard</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">SetText</span><span style="color: #333;">&#40;</span> <span style="color: #009900;">&quot;!http://huddledmasses.org/images/PowerBoots/$($_.Name)!&quot;</span> <span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>]]></content:encoded>
			<wfw:commentRss>http://joelbennett.net/powerboots-output-graphs-to-images-from-powershell/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

