<?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; Splatting</title>
	<atom:link href="http://huddledmasses.org/tag/splatting/feed/" rel="self" type="application/rss+xml" />
	<link>http://huddledmasses.org</link>
	<description>You can do more than breathe for free...</description>
	<lastBuildDate>Fri, 27 Apr 2012 05:42:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<cloud domain='huddledmasses.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>PowerShell and Hashtable oddities</title>
		<link>http://huddledmasses.org/powershell-and-hashtable-oddities/</link>
		<comments>http://huddledmasses.org/powershell-and-hashtable-oddities/#comments</comments>
		<pubDate>Wed, 19 Nov 2008 05:29:25 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Arrays]]></category>
		<category><![CDATA[Bugs]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Splatting]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/powershell-and-hashtable-oddities/</guid>
		<description><![CDATA[Hashtables are IEnumerable, but they don't behave that way in PowerShell ... this seems to cause all sorts of odd behavior ...]]></description>
			<content:encoded><![CDATA[	<p>Hashtables are IEnumerable, but they don&#8217;t behave that way in PowerShell &#8230; this seems to cause all sorts of odd behavior and such, so I thought I&#8217;d write up all the examples I can think of in one place.  That means this post is going to be a little bit rambling, so please bear with me.</p>

	<h3>PowerShell enumerates IEnumerable</h3>

	<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: #009900;">&quot;<br />
&lt;code lang=&quot;</span>csharp<span style="color: #009900;">&quot;&gt;<br />
using System.Collections; <br />
public class enumer : IEnumerable {<br />
&nbsp; public IEnumerator GetEnumerator() {<br />
&nbsp; &nbsp; for(int i = 0; i &lt; 10; i++) { <br />
&nbsp; &nbsp; &nbsp;yield return i; <br />
&nbsp; &nbsp;}<br />
&nbsp; }<br />
}<br />
&quot;</span>@<br />
<br />
<span style="color: #660033; font-weight: bold;">$e</span> <span style="color: #000066;">-is</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Collections.<span style="color: #003366;">IEnumerable</span><span style="color: #333;">&#93;</span></span> &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># is true </span><br />
<span style="color: #660033; font-weight: bold;">$e</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> enumer<br />
<span style="color: #660033; font-weight: bold;">$e</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># will output 0..9 to the console</span><br />
<span style="color: #660033; font-weight: bold;">$e</span>.<span style="color: #003366;">GetEnumerator</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># will output the same</span><br />
<span style="color: #660033; font-weight: bold;">$e</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">measure-<span style="font-style: normal;">object</span></span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># will show a count of 10.</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$table</span> <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#123;</span>test<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;This is a test&quot;</span>;exam<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;this is an exam&quot;</span>;defense<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;defend your thesis&quot;</span><span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$table</span> <span style="color: #000066;">-is</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Collections.<span style="color: #003366;">IEnumerable</span><span style="color: #333;">&#93;</span></span> &nbsp;<span style="color: #666666; font-style: italic;"># is true too!</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$table</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># will output three DictionaryEntry items</span><br />
<span style="color: #660033; font-weight: bold;">$table</span>.<span style="color: #003366;">GetEnumerator</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># will output the same thing</span><br />
<span style="color: #660033; font-weight: bold;">$table</span>.<span style="color: #003366;">Count</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># will output 3</span><br />
<span style="color: #660033; font-weight: bold;">$table</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">measure-<span style="font-style: normal;">object</span></span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># will output 1! WHAT!?</span><br />
&nbsp;</div>

	<p>It turns out that in the case of Hashtables, PowerShell does <span class="caps">NOT</span> enumerate them into the pipeline. Instead, it passes the entire Hashtable object.  Of course, nobody realizes this &#8230;  because the ouput cmdlets unwrap them (what?!).</p>

	<h3>But there are bugs caused by special treatment</h3>

	<p>The <a href="https://connect.microsoft.com/feedback/ViewFeedback.aspx?FeedbackID=382818&#38;SiteID=99">first bug is in Add-Member</a>, which doesn&#8217;t work on Hashtables until you&#8217;ve already <strong>used</strong> the Hashtable.</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #660033; font-weight: bold;">$table</span> <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#123;</span>test<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;This is a test&quot;</span>;exam<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;this is an exam&quot;</span>;defense<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;defend your thesis&quot;</span><span style="color: #333;">&#125;</span><br />
<span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">Member</span></span> <span style="color: #000066;">-in</span> <span style="color: #660033; font-weight: bold;">$table</span> NoteProperty Quiz <span style="color: #009900;">&quot;Surprise, hope you're ready!&quot;</span><br />
<span style="color: #660033; font-weight: bold;">$table</span>.<span style="color: #003366;">Quiz</span> &nbsp;<span style="color: #666666; font-style: italic;"># It's not there! There is NO OUTPUT</span><br />
<span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">Member</span></span> <span style="color: #000066;">-in</span> <span style="color: #660033; font-weight: bold;">$table</span> NoteProperty Quiz <span style="color: #009900;">&quot;Surprise, hope you're ready!&quot;</span><br />
<span style="color: #660033; font-weight: bold;">$table</span>.<span style="color: #003366;">Quiz</span> &nbsp;<span style="color: #666666; font-style: italic;"># This time it works ...</span><br />
&nbsp;</div>

	<p><span class="caps">NOT</span> <span class="caps">ONLY</span> does Add-Member not work the first time, it&#8217;s not just a matter of calling it twice: you just have to try to access something in the hashtable before you can use Add-Member on it:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #660033; font-weight: bold;">$table</span> <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#123;</span>test<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;This is a test&quot;</span>;exam<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;this is an exam&quot;</span>;defense<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;defend your thesis&quot;</span><span style="color: #333;">&#125;</span><br />
<span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">Member</span></span> <span style="color: #000066;">-in</span> <span style="color: #660033; font-weight: bold;">$table</span> NoteProperty Puzzle <span style="color: #009900;">&quot;How on earth does this work?&quot;</span><br />
<span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">Member</span></span> <span style="color: #000066;">-in</span> <span style="color: #660033; font-weight: bold;">$table</span> NoteProperty Quiz &nbsp; <span style="color: #009900;">&quot;Surprise, hope you're ready!&quot;</span><br />
<span style="color: #660033; font-weight: bold;">$table</span>.<span style="color: #003366;">Quiz</span> &nbsp;<span style="color: #666666; font-style: italic;"># It's not there! There is NO OUTPUT</span><br />
<span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">Member</span></span> <span style="color: #000066;">-in</span> <span style="color: #660033; font-weight: bold;">$table</span> NoteProperty Quiz &nbsp; <span style="color: #009900;">&quot;Surprise, hope you're ready!&quot;</span><br />
<span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">Member</span></span> <span style="color: #000066;">-in</span> <span style="color: #660033; font-weight: bold;">$table</span> NoteProperty Puzzle <span style="color: #009900;">&quot;How on earth does this work?&quot;</span><br />
<span style="color: #660033; font-weight: bold;">$table</span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">gm</span> <span style="color: #000066;">-type</span> NoteProperty <span style="color: #66cc66;">|</span> <span style="color: #66cc66;">%</span><span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Name</span> <span style="color: #333;">&#125;</span> &nbsp;<span style="color: #666666; font-style: italic;"># Output: Puzzle, Quiz</span><br />
&nbsp;</div>

	<p>Here&#8217;s another <a href="https://connect.microsoft.com/feedback/ViewFeedback.aspx?FeedbackID=382822&#38;SiteID=99">buggy manifestation, in the Formatting cmdlets</a>. This time, the Format-* cmdlets unroll the hashtable &#8230; to make it look like it&#8217;s being enumerated the way it should be.</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #660033; font-weight: bold;">$table</span> <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#123;</span>test<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;This is a test&quot;</span>;exam<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;this is an exam&quot;</span>;defense<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;defend your thesis&quot;</span><span style="color: #333;">&#125;</span><br />
<span style="color: #666666; font-style: italic;">## Prime it, and then Add-Member won't work</span><br />
<span style="color: #660033; font-weight: bold;">$table</span>.<span style="color: #003366;">PrimeTheHashtableSoWeCanAddMember</span><br />
<span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">Member</span></span> <span style="color: #000066;">-in</span> <span style="color: #660033; font-weight: bold;">$table</span> NoteProperty Quiz &nbsp; <span style="color: #009900;">&quot;Surprise, hope you're ready!&quot;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## Note how Measure-Object and Get-Member operate on the HASHTABLE</span><br />
<span style="color: #666666; font-style: italic;">## There's only a single item, of course...</span><br />
<span style="color: #660033; font-weight: bold;">$table</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">measure-<span style="font-style: normal;">object</span></span> <span style="color: #66cc66;">|</span> <span style="color: #66cc66;">%</span><span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Count</span><span style="color: #333;">&#125;</span> &nbsp; <br />
<span style="color: #666666; font-style: italic;">## And we have 7 Properties, plus the Quiz NoteProperty</span><br />
<span style="color: #660033; font-weight: bold;">$table</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">get-<span style="font-style: normal;">member</span></span> <span style="color: #000066;">-type</span> Properties <span style="color: #66cc66;">|</span> <span style="color: #66cc66;">%</span><span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Name</span> <span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## But Format-List shows the properties of the ITEMS</span><br />
<span style="color: #666666; font-style: italic;">## So we think we can list those properties like:</span><br />
<span style="color: #660033; font-weight: bold;">$table</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">format-<span style="font-style: normal;">list</span></span> <span style="color: #66cc66;">*</span><br />
&nbsp;</div>

	<p>Clearly, the Format-* cmdlets have magic code that unwraps hashtables. Which just leads to even more confusion:  $table looks the same (in the console output) as $table.GetEnumerator() ... but it doesn&#8217;t behave the same way, <span class="caps">EXCEPT</span> to the format cmdlets.</p>

	<h3>PowerShell 2.0 uses Hashtables more</h3>

	<p>In PowerShell 2.0, the PowerShell team is adding another special feature based on hashtables (which <strong>appears</strong> at first to be based on IEnumerables):</p>

	<p>Jeffrey Snover gave an example of <strong>splatting</strong> in his <span class="caps">PDC</span> presentation.  Splatting is where a collection is unwrapped so that you can take an array of values and pass one to each parameter of a cmdlet or function. But in Jeffrey Snover&#8217;s demo, he splatted a hashtable.  Basically, the hashtable keys are matched up to parameter names as though they had been specified by name. That made me wonder why splatting can&#8217;t work with custom objects, but after investigating a bit, I&#8217;m actually frustrated with the inconsistency of how hashtables are treated in Posh.</p>

	<ul>
		<li>In the splatting scenario they are unrolled as a collection of named parameters &#8230;</li>
		<li>If you pipe them, they&#8217;re treated as a single object instead of being unrolled &#8230; </li>
	</ul>
	<ul>
		<li>Even though you can access hashtable items using dotted property syntax, you can&#8217;t use them to set ValueFromPipelineByPropertyName values, because they aren&#8217;t really properties.</li>
	</ul>

	<p>The new splatting feature seems to only work with simple arrays and hashtables &#8230; adding yet another scenario where the hashtable is being treated specially (even though it doesn&#8217;t need to be: if they just <a href="https://connect.microsoft.com/feedback/ViewFeedback.aspx?FeedbackID=382827&#38;SiteID=99">splatted IEnumerable</a>, we could work with List<String>, and any IEnumerable of DictionaryEntry objects, or KeyValuePair<String,T> could be matched by name &#8230; that would make hashtables work, but it would also let you use the more powerful generic collections, etc.</p>

	<p><strong>You know what would be cool?</strong> If I could splat <em>any</em> object (like a custom PSObject that I have added members to), and have it&#8217;s property names matched to parameter names as though all the parameters had ValueFromPipelineByPropertyName set.</p>

	<p><strong>You know what would be <em>really</em> cool?</strong> If I could specify that <a href="https://connect.microsoft.com/feedback/ViewFeedback.aspx?FeedbackID=382825&#38;SiteID=99">I want pipeline objects splatted</a>, forcing <span class="caps">ALL</span> parameters to be treated as ValueFromPipelineByPropertyName, without needing to use: ForEach-Object { Test-Splatting @_ } &#8230; maybe a syntax like: Get-HashTablesToSplat | Test-Splatting @@ &#8230;</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/powershell-and-hashtable-oddities/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

