<?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; CTP</title>
	<atom:link href="http://huddledmasses.org/tag/ctp/feed/" rel="self" type="application/rss+xml" />
	<link>http://huddledmasses.org</link>
	<description>You can do more than breathe for free...</description>
	<lastBuildDate>Sat, 28 Jan 2012 21:37:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<cloud domain='huddledmasses.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>PowerShell 3 &#8211; Finally on the DLR!</title>
		<link>http://huddledmasses.org/powershell-3-finally-on-the-dlr/</link>
		<comments>http://huddledmasses.org/powershell-3-finally-on-the-dlr/#comments</comments>
		<pubDate>Thu, 05 Jan 2012 05:38:21 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[CTP]]></category>
		<category><![CDATA[DLR]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[PS3]]></category>
		<category><![CDATA[Windows 8]]></category>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	<p>Anyway, I&#8217;m sure I&#8217;ll have more to write about the <span class="caps">DLR</span> and the changes it&#8217;s bringing to PowerShell, but for now, I hope that&#8217;s enough to get you thinking  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';-)' class='wp-smiley' /> </p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/powershell-3-finally-on-the-dlr/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>WPF From PowerShell &#8211; The Missing Walkthrough</title>
		<link>http://huddledmasses.org/wpf-from-powershell-the-missing-walkthrough/</link>
		<comments>http://huddledmasses.org/wpf-from-powershell-the-missing-walkthrough/#comments</comments>
		<pubDate>Tue, 27 May 2008 13:53:19 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[CTP]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/?p=545</guid>
		<description><![CDATA[If you&#8217;ve been reading my series of articles about using WPF from PowerShell and wishing I had started with the basics about what WPF is &#8230; you&#8217;re in luck. Following my series of posts, the PowerShell team has written their own series &#8212; with a much more methodical approach. Starting with an introduction to WPF [...]]]></description>
			<content:encoded><![CDATA[	<p>If you&#8217;ve been reading my series of articles about using <span class="caps">WPF</span> from PowerShell and wishing I had started with the basics about what <span class="caps">WPF</span> is &#8230; you&#8217;re in luck.  Following my series of posts, the PowerShell team has written their own series &#8212; with a much more methodical approach.  Starting with <a href="http://blogs.msdn.com/powershell/archive/2008/05/22/wpf-powershell-part-1-hello-world-welcome-to-the-week-of-wpf.aspx">an introduction to <span class="caps">WPF</span> and PowerShell</a> followed by a great overview of how to use some of the <a href="http://blogs.msdn.com/powershell/archive/2008/05/23/wpf-powershell-part-2-exploring-wpf-and-the-rest-of-net-with-scripts.aspx"><span class="caps">WPF</span> controls from PowerShell</a> and how to <a href="http://blogs.msdn.com/powershell/archive/2008/05/24/wpf-powershell-part-3-handling-events.aspx">handle <span class="caps">WPF</span> events</a> , they have now moved beyond the basics of <a href="http://blogs.msdn.com/powershell/archive/2008/05/25/wpf-powershell-part-4-xaml-show-control.aspx"><span class="caps">XAML</span> and PowerShell</a> to show you how you can <a href="http://blogs.msdn.com/powershell/archive/2008/05/26/wpf-powershell-part-5-using-wpf-powershell-modules.aspx">make <span class="caps">GUI</span> a part of your PowerShell pipeline</a> like I did with my <a href="/wpf-from-powershell-select-grid/">Select-Grid</a> post.  With their most recent post, they explain <a href="http://blogs.msdn.com/powershell/archive/2008/05/27/wpf-powershell-part-6-running-functions-in-the-background.aspx">how to run <span class="caps">WPF</span> in a background runspace</a>  and they promise tomorrow they&#8217;ll expand on that to &#8220;make controls in the background stream data and talk to the main runspace&#8221; &#8212; these two topics are what I was going get to next, so this frees me up to play with something more interesting  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=':-D' class='wp-smiley' />  &#8230;.</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/wpf-from-powershell-the-missing-walkthrough/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PowerShell 2 CTP2 &#8212; Wow!</title>
		<link>http://huddledmasses.org/powershell-2-ctp2-wow/</link>
		<comments>http://huddledmasses.org/powershell-2-ctp2-wow/#comments</comments>
		<pubDate>Sat, 03 May 2008 04:39:10 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[CTP]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/powershell-2-ctp2-wow/</guid>
		<description><![CDATA[The PowerShell team has released the second CTP of PowerShell 2 and it&#8217;s got so much new stuff in it, that it&#8217;s honestly hard to know where to start, so this first post is just going to be a list of the things I find the most exciting. STA Threading You can run PowerShell in [...]]]></description>
			<content:encoded><![CDATA[	<p>The PowerShell team has released the <a href="http://go.microsoft.com/fwlink/?LinkID=107395">second <span class="caps">CTP</span> of PowerShell 2</a> and it&#8217;s got so much new stuff in it, that it&#8217;s honestly hard to know where to start, so this first post is just going to be a list of the things I find the <em>most exciting</em>.</p>

	<h3><span class="caps">STA</span> Threading</h3>

	<p>You can run PowerShell in Single Threaded Apartment mode, which will let us create WPF/<span class="caps">XAML</span> and some neat UI tricks that we couldn&#8217;t do before.</p>

	<h3>Modules</h3>

	<p>Modules are &#8230; like classes.  They export public functions, and can have private functions and variables&#8230; you basically import the functions into your runspace, so you can call them, and they can share data via variables and even other functions in the same module.  This is <em>really</em> awesome, and will almost certainly improve a dozen or more of my scripts which I&#8217;ve been using as containers for sets of functions.</p>

	<h3>Events</h3>

	<p>PowerShell has support for events, including a whole set of cmdlets: Register-ObjectEvent, Register-PSEvent, Wait-PSEvent, Remove-PSEvent, Unregister-PSEvent, Get-PSEvent, New-PSEvent, Get-PSEventSubscriber which will, in many cases, seem extremely familiar to users of <a href="http://www.codeplex.com/PSEventing">x0n&#8217;s PSEventing</a> snapin.</p>

	<h3>Constrained Runspaces</h3>

	<p>I haven&#8217;t had a chance to test this out yet, but the word is that we can create constrained runspaces that limit access to commands, scripts, and language elements.  I&#8217;m not sure if this lets you prevent access to part of the .Net framework, but it looks interesting in terms of running other peoples scripts.</p>

	<h3>Splatting Operator</h3>

	<p>When you&#8217;ve got an array, you can split it up and pass it to a function so each element in the array shows up as a separate argument to the function, so if you have a function <code>foo</code> can call a function <code>bar</code> and pass it all the arguments that get passed to foo, without needing to know what they are or how many there are. (in v1, you have to do things like this: <code>iex &#34;&#38;amp;amp;&#39;bar&#39; $args&#34;</code> which just gets ugly).</p>

	<h3>Verbose, Warning and Debug streams</h3>

	<p>I haven&#8217;t figured out how to really do anything with this yet, but the release notes say that these streams now contain string messages, invocation information and even pipeline metadata like counts of how many times each command in a pipeline has been invoked &#8212; someone was just asking for this on the usenet newsgroup the other day &#8230;</p>

	<h3> <img src='http://huddledmasses.org/wordpress/wp-includes/' alt='[new]' class='wp-smiley' />  Add-Type</h3>

	<p>There&#8217;s no help for this cmdlet, but it&#8217;s primarily a wrapper around the .net compilers (CSharp, CSharpVersion3, VisualBasic, and JScript, by default).  However, it can also replace all the <code>[Reflection.Assembly]::Load*</code> methods so you can save some typing. I&#8217;ve figured out a couple of uses so far, and both are really useful:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666666; font-style: italic;"># This is equivalent to [Reflection.Assembly]::LoadWithPartialName(&quot;System.Web&quot;)</span><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;">Web</span><br />
<span style="color: #666666; font-style: italic;"># And this is equivalent to [Reflection.Assembly]::LoadFrom( (ls PsXmppClient.dll) )</span><br />
<span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">Type</span></span> <span style="color: #000066;">-Path</span> PsXmppClient.<span style="color: #003366;">dll</span><br />
<br />
<span style="color: #666666; font-style: italic;"># And this syntax lets you compile your own C# code on the fly to create custom types</span><br />
<span style="color: #666666; font-style: italic;"># So next time you needed to complete Scripting Games 2008 event 8, you could use this:</span><br />
<span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">Type</span></span> @<span style="color: #009900;">&quot;<br />
using System; <br />
public struct Song { <br />
&nbsp; &nbsp;public String Artist;<br />
&nbsp; &nbsp;public String Name;<br />
&nbsp; &nbsp;public TimeSpan Length;<br />
<br />
&nbsp; &nbsp;public Song(string artist, string name, TimeSpan length) <br />
&nbsp; &nbsp;{ <br />
&nbsp; &nbsp; &nbsp; Artist = artist; <br />
&nbsp; &nbsp; &nbsp; Name = name; <br />
&nbsp; &nbsp; &nbsp; Length = length; <br />
&nbsp; &nbsp;}<br />
}<br />
&quot;</span>@<br />
&nbsp;</div>

	<p>Incidentally, there&#8217;s no help for Add-Type for some reason, but you can get basic information about how to use a cmdlet by running a command like this: <code>(Get-Command Add-Type).Definition -replace &#34;`n&#34;,&#34;`n`n&#34;</code> &#8230;</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/powershell-2-ctp2-wow/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PowerShell 2.0 CTP &#8211; Script Cmdlets</title>
		<link>http://huddledmasses.org/powershell-20-ctp-script-cmdlets/</link>
		<comments>http://huddledmasses.org/powershell-20-ctp-script-cmdlets/#comments</comments>
		<pubDate>Tue, 06 Nov 2007 22:09:57 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Alpha]]></category>
		<category><![CDATA[Beta]]></category>
		<category><![CDATA[Cmdlet]]></category>
		<category><![CDATA[CTP]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Pipeline]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/powershell-20-ctp-script-cmdlets/</guid>
		<description><![CDATA[Well, the first alpha CTP release of PowerShell 2.0 is out, and there&#8217;s a lot of new stuff in it &#8230; but I won&#8217;t repeat the list from the PowerShell blog, because I&#8217;m sure you&#8217;ve seen it five or six times already. Instead, lets just skip straight to talking about one of the features we&#8217;ve [...]]]></description>
			<content:encoded><![CDATA[	<p>Well, the first <a href="http://blogs.msdn.com/powershell/archive/2007/11/06/what-s-new-in-ctp-of-powershell-2-0.aspx"><del>alpha</del> <span class="caps">CTP</span> release of PowerShell 2.0</a> is out, and there&#8217;s a lot of new stuff in it &#8230; but I won&#8217;t repeat the list from the PowerShell blog, because I&#8217;m sure you&#8217;ve seen it five or six times already. Instead, lets just skip straight to talking about one of the features we&#8217;ve been hearing about the longest: in PowerShell 2, you can create Cmdlets in script &#8230; bringing nearly full parity between  whats possible in a C# cmdlet and what&#8217;s possible in script.</p>

	<p>There are a few caveats still (Parameter Sets aren&#8217;t working yet, and neither is help, really), and a few surprises &#8230; there&#8217;s a few downsides to PowerShell script vs C# ... but in this particular context one thing that stands out is that in C# the <code>BeginProcessing</code>, <code>ProcessRecord</code>, and <code>EndProcessing</code> blocks are actually methods which can call each other, and as demonstrated in my <a href="/writing-cmdlets-for-the-powershell-pipeline/">tutorial for writing cmdlets that work in the pipeline</a>, they can be recursive &#8212; without getting duplicate variables.  </p>

	<h4>A sample Script Cmdlet</h4>

	<p>In the interests of being the first to publish an interesting script cmdlet  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';)' class='wp-smiley' />  and to continue my recent trend of talking about writing for the PowerShell pipeline, I&#8217;ve merged the logic of my <a href="/writing-better-script-functions-for-the-powershell-pipeline/">script function</a> and my <a href="/writing-cmdlets-for-the-powershell-pipeline/">pipeline cmdlet</a> into a <a href="/wordpress/wp-content/uploads/2007/11/test-pipelinev2.ps1">single sample script cmdlet for PowerShell 2.0</a> and it works great!</p>

	<p>A few observations from the process, in no particular order:</p>

	<ul>
		<li>If you recursively call your cmdlet from within itself, you have to test for parameters using the new <code>$CommandLineParameters.ContainsKey</code> because parameter variables keep their values through recursion if you don&#8217;t explicitly pass a value.</li>
		<li><code>$CommandLineParameters.ContainsKey</code> works differently in the <code>Begin</code> block where it will return <code>$false</code> for arguments which will get their values from the pipeline, than in the <code>Process</code> block where it will treat values which were passed as <code>CommandLineParameters</code> the same as those which were passed via the pipeline.</li>
		<li>If you want to see how your function behaves in a pipeline, you should make sure to test it at different points in the pipeline: at the front, in the middle, and at the end.</li>
		<li>Cmdlets are functions: they show up in the Function provider.</li>
		<li>Cmdlets are functions: they have to be dot-sourced before you can call them.</li>
		<li>Cmdlets are not functions: they are a single command <code>Cmdlet</code> which takes a name (which <strong>must</strong> have a &#8211; in it) and a couple of other parameters followed by a function script block.</li>
	</ul>
	<ul>
		<li>When you recurse by executing &#038;($MyInvocation.InvocationName), that second invocation has an InvocationName of &#8220;&#038;&#8221; ... so you can&#8217;t go any further (this might be a good thing, if you want to stop recursion at one level no matter what. If you want to go further, you need to put your commands into a string, and use <code>Invoke-Expression</code>.</li>
	</ul>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666666; font-style: italic;">#requires -version 2.0</span><br />
<span style="color: #666666; font-style: italic;">###################################################################################################</span><br />
<span style="color: #666666; font-style: italic;">## A Template for Script Cmdlets which can _also_ be executed in the pipeline ....</span><br />
<span style="color: #666666; font-style: italic;">## &nbsp; by Joel Bennett, in hopes it will help...</span><br />
<span style="color: #666666; font-style: italic;">## Version History</span><br />
<span style="color: #666666; font-style: italic;">## v1.0 - First public release (after over 9 different versions in my various other functions)</span><br />
<span style="color: #666666; font-style: italic;">## v1.2 - Show the use of Write-Output, and change &quot;return&quot; in the BEGIN to &quot;Write-Output&quot; to avoid</span><br />
<span style="color: #666666; font-style: italic;">## &nbsp; &nbsp; &nbsp;the pooling of the output from the process block when it's invoked as a function.</span><br />
<span style="color: #666666; font-style: italic;">## v1.3 - Switched back to &quot;break&quot; instead of &quot;return&quot; so that if you pass via the pipeline AND via</span><br />
<span style="color: #666666; font-style: italic;">## &nbsp; &nbsp; &nbsp;the inputObject, only the inputObject gets process (this is how cmdlets behave).</span><br />
<span style="color: #666666; font-style: italic;">## &nbsp; &nbsp; &nbsp;- Cleaned up the comments, and removed the confusing alternate method and $args handling</span><br />
<span style="color: #666666; font-style: italic;">##</span><br />
<span style="color: #666666; font-style: italic;">## v2.0 - First Version as a Script Cmdlet. </span><br />
<span style="color: #666666; font-style: italic;">## &nbsp; &nbsp; &nbsp;This is much easier with support for [ValueFromPipeline] and [ValueFromPipelineByName] </span><br />
<span style="color: #666666; font-style: italic;">##</span><br />
<span style="color: #666666; font-style: italic;">###################################################################################################</span><br />
Cmdlet <span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">PipelineV2</span></span> <span style="color: #000066;">-ConfirmImpact</span> low <span style="color: #000066;">-snapin</span> Huddled.<span style="color: #003366;">Tests</span><br />
<span style="color: #333;">&#123;</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>Position<span style="color: #333;">&#40;</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>ConsoleColor<span style="color: #333;">&#93;</span></span> <span style="color: #660033; font-weight: bold;">$Color</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#91;</span>Position<span style="color: #333;">&#40;</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>Mandatory<span style="color: #333;">&#93;</span></span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>ValueFromPipeline<span style="color: #333;">&#93;</span></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;">&#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><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;">$CommandLineParameters</span>.<span style="color: #003366;">ContainsKey</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;InputObject&quot;</span><span style="color: #333;">&#41;</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;"># Don't do anything here, because we're about to get re-invoked...</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$FromArgs</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$true</span><br />
&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;<span style="color: #666666; font-style: italic;"># Normal &quot;run-once&quot; BEGIN processing</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$FromArgs</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$false</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Verbose</span></span> <span style="color: #009900;">&quot;Begin $Color&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<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: #666666; font-style: italic;"># We no longer have to test for $_ or even to see if the [ValueFromPipeline] param is set</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># It *HAS* to be set, because it's a [Mandatory] parameter :)</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;">$FromArgs</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;"># Don't do anything here except re-invoke ourselves.</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;">$InputObject</span> <span style="color: #66cc66;">|</span> <span style="color: #66cc66;">&amp;</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$MyInvocation</span>.<span style="color: #003366;">InvocationName</span><span style="color: #333;">&#41;</span> <span style="color: #660033; font-weight: bold;">$Color</span><br />
&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;<span style="color: #666666; font-style: italic;"># Normal Pipeline-friendly per-item processing</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;Process: $InputObject&quot;</span> <span style="color: #000066;">-Fore</span> <span style="color: #660033; font-weight: bold;">$Color</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## You should make a practice of explicitly calling Write-Output on things</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## That's how you emit them into the pipeline instead of just printing them</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;">$InputObject</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">END</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;">$FromArgs</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># Don't do anything here ... it just confuses things</span><br />
&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;<span style="color: #666666; font-style: italic;"># Normal &quot;run-once&quot; END processing</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Verbose</span></span> <span style="color: #009900;">&quot;End $Color&quot;</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>

	<h4>A test case</h4>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666666; font-style: italic;">## Test Script:</span><br />
<span style="color: #666666; font-style: italic;">##</span><br />
<span style="color: #666666; font-style: italic;">## &quot;a&quot;,&quot;b&quot;,&quot;c&quot; | Test-PipelineV2 &quot;Cyan&quot; -verbose</span><br />
<span style="color: #666666; font-style: italic;">## @(&quot;a&quot;,&quot;b&quot;,&quot;c&quot;) | Test-PipelineV2 &quot;Cyan&quot; -verbose</span><br />
<span style="color: #666666; font-style: italic;">## Test-PipelineV2 &quot;Cyan&quot; @(&quot;a&quot;,&quot;b&quot;,&quot;c&quot;) -verbose </span><br />
<span style="color: #666666; font-style: italic;">## </span><br />
<span style="color: #666666; font-style: italic;">## &quot;a&quot;,&quot;b&quot;,&quot;c&quot; | Test-PipelineV2 &quot;Cyan&quot; -verbose | Test-PipelineV2 &quot;Green&quot; -verbose</span><br />
<span style="color: #666666; font-style: italic;">## @(&quot;a&quot;,&quot;b&quot;,&quot;c&quot;) | Test-PipelineV2 &quot;Cyan&quot; -verbose | Test-PipelineV2 &quot;Green&quot; -verbose</span><br />
<span style="color: #666666; font-style: italic;">## Test-PipelineV2 &quot;Cyan&quot; @(&quot;a&quot;,&quot;b&quot;,&quot;c&quot;) -verbose &nbsp;| Test-PipelineV2 &quot;Green&quot; -verbose</span><br />
<span style="color: #666666; font-style: italic;">###################################################################################################</span><br />
<span style="color: #666666; font-style: italic;">## Expected Output (sorry, no color here...)</span><br />
<br />
VERBOSE: <span style="color: #666699; font-weight: bold;">Begin</span> Cyan<br />
<span style="color: #666699; font-weight: bold;">Process</span>: a<br />
a<br />
<span style="color: #666699; font-weight: bold;">Process</span>: b<br />
b<br />
<span style="color: #666699; font-weight: bold;">Process</span>: c<br />
c<br />
VERBOSE: <span style="color: #666699; font-weight: bold;">End</span> Cyan<br />
VERBOSE: <span style="color: #666699; font-weight: bold;">Begin</span> Cyan<br />
<span style="color: #666699; font-weight: bold;">Process</span>: a<br />
a<br />
<span style="color: #666699; font-weight: bold;">Process</span>: b<br />
b<br />
<span style="color: #666699; font-weight: bold;">Process</span>: c<br />
c<br />
VERBOSE: <span style="color: #666699; font-weight: bold;">End</span> Cyan<br />
VERBOSE: <span style="color: #666699; font-weight: bold;">Begin</span> Cyan<br />
<span style="color: #666699; font-weight: bold;">Process</span>: a<br />
a<br />
<span style="color: #666699; font-weight: bold;">Process</span>: b<br />
b<br />
<span style="color: #666699; font-weight: bold;">Process</span>: c<br />
c<br />
VERBOSE: <span style="color: #666699; font-weight: bold;">End</span> Cyan<br />
VERBOSE: <span style="color: #666699; font-weight: bold;">Begin</span> Cyan<br />
VERBOSE: <span style="color: #666699; font-weight: bold;">Begin</span> Green<br />
<span style="color: #666699; font-weight: bold;">Process</span>: a<br />
<span style="color: #666699; font-weight: bold;">Process</span>: a<br />
a<br />
<span style="color: #666699; font-weight: bold;">Process</span>: b<br />
<span style="color: #666699; font-weight: bold;">Process</span>: b<br />
b<br />
<span style="color: #666699; font-weight: bold;">Process</span>: c<br />
<span style="color: #666699; font-weight: bold;">Process</span>: c<br />
c<br />
VERBOSE: <span style="color: #666699; font-weight: bold;">End</span> Cyan<br />
VERBOSE: <span style="color: #666699; font-weight: bold;">End</span> Green<br />
VERBOSE: <span style="color: #666699; font-weight: bold;">Begin</span> Cyan<br />
VERBOSE: <span style="color: #666699; font-weight: bold;">Begin</span> Green<br />
<span style="color: #666699; font-weight: bold;">Process</span>: a<br />
<span style="color: #666699; font-weight: bold;">Process</span>: a<br />
a<br />
<span style="color: #666699; font-weight: bold;">Process</span>: b<br />
<span style="color: #666699; font-weight: bold;">Process</span>: b<br />
b<br />
<span style="color: #666699; font-weight: bold;">Process</span>: c<br />
<span style="color: #666699; font-weight: bold;">Process</span>: c<br />
c<br />
VERBOSE: <span style="color: #666699; font-weight: bold;">End</span> Cyan<br />
VERBOSE: <span style="color: #666699; font-weight: bold;">End</span> Green<br />
VERBOSE: <span style="color: #666699; font-weight: bold;">Begin</span> Green<br />
VERBOSE: <span style="color: #666699; font-weight: bold;">Begin</span> Cyan<br />
<span style="color: #666699; font-weight: bold;">Process</span>: a<br />
<span style="color: #666699; font-weight: bold;">Process</span>: a<br />
a<br />
<span style="color: #666699; font-weight: bold;">Process</span>: b<br />
<span style="color: #666699; font-weight: bold;">Process</span>: b<br />
b<br />
<span style="color: #666699; font-weight: bold;">Process</span>: c<br />
<span style="color: #666699; font-weight: bold;">Process</span>: c<br />
c<br />
VERBOSE: <span style="color: #666699; font-weight: bold;">End</span> Cyan<br />
VERBOSE: <span style="color: #666699; font-weight: bold;">End</span> Green</div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/powershell-20-ctp-script-cmdlets/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

