<?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; Error message</title>
	<atom:link href="http://huddledmasses.org/tag/error-message/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>Better error messages for PowerShell ValidatePattern</title>
		<link>http://huddledmasses.org/better-error-messages-for-powershell-validatepattern/</link>
		<comments>http://huddledmasses.org/better-error-messages-for-powershell-validatepattern/#comments</comments>
		<pubDate>Wed, 16 Jun 2010 03:08:35 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Attribute]]></category>
		<category><![CDATA[Error message]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[ValidatePattern]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1487</guid>
		<description><![CDATA[If you&#8217;ve been writing advanced PowerShell 2.0 functions, you&#8217;ve probably used some of the Validate* attributes to enforce valid parameter values, and you may have noticed that their error messages leave a lot to be desired. For example, imagine that you have a parameter which takes a 10-digit phone number: function Test-PhoneNumber &#123; param&#40; &#91;ValidatePattern&#40;'^\(?\d{3}\)?[-\s.]?\d{3}[-\s.]\d{4}$'&#41;&#93;$number [...]]]></description>
			<content:encoded><![CDATA[	<p>If you&#8217;ve been writing advanced PowerShell 2.0 functions, you&#8217;ve probably used some of the Validate* attributes to enforce valid parameter values, and you may have noticed that their error messages leave a lot to be desired. For example, imagine that you have a parameter which takes a 10-digit phone number:</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;">Test-<span style="font-style: normal;">PhoneNumber</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #666699; font-weight: bold;">param</span><span style="color: #333;">&#40;</span> <span style="color: #333;">&#91;</span>ValidatePattern<span style="color: #333;">&#40;</span><span style="color: #009900;">'^\(?\d{3}\)?[-\s.]?\d{3}[-\s.]\d{4}$'</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><span style="color: #660033; font-weight: bold;">$number</span> <span style="color: #333;">&#41;</span><br />
<span style="color: #666666; font-style: italic;">&lt;# do stuff #&gt;</span><br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<h3>The problem</h3>

	<p>Mark Schill (Meson) brought this up at our <a href="http://PowerShellGroup.org/virtual">virtual PowerShell User Group</a> on <span class="caps">IRC</span> tonight: the error messages are confusing and not helpful. For instance, when you try that function and pass an invalid phone number (let&#8217;s say you forget to include the area code), you get this error message:</p>

	<p><span style="color:red;font-family:Consolas,'Courier New',monospace">Test-PhoneNumber : Cannot validate argument on parameter 'number'. The argument "555-1212" does not match the "^\(?\d{3}\)?[-\s.]?\d{3}[-\s.]\d{4}$" pattern. Supply an argument that matches "^\(?\d{3}\)?[-\s.]?\d{3}[-\s.]\d{4}$" and try the command again.</span></p>

	<p>There aren&#8217;t very many people who can read regular expressions, but only someone who <em>can</em> would really find the entirety of that error message useful. Everyone else is going to get (at most) this out of it: <span style="color:red;font-family:Consolas,'Courier New',monospace"> Cannot validate argument on parameter &#8216;number&#8217;. The argument &#8220;555-1212&#8221; does not match <strong>... yadda, yadda</strong></span>.  </p>

	<p>That being the case, we&#8217;d like to hide the rest of that message &#8230; particularly the part that says: &#8220;Supply an argument that matches &#8230;&#8221; which is, frankly, just annoying or insulting depending on who your users are. In fact, as Mark suggested, I&#8217;d like to replace it with a custom message &#8230; something like: <span style="color:red;font-family:Consolas,'Courier New',monospace">Cannot validate argument on parameter &#8216;number&#8217;. The supplied value is not a valid phone number. Please supply a full 10-digit number like: (123) 555-1212</span></p>

	<h3>Custom Validation Properties</h3>

	<p>So, this evening I decided to do something about it. It&#8217;s really pretty simple to write your own Validate*Attribute &#8230; you just have to derive from <code>ValidateEnumeratedArgumentsAttribute</code>, and override <code>ValidateElement</code>.  Here&#8217;s an example ValidatePattern that supports a custom error message.  I&#8217;ll paste the C# code separately, but basically you can just use <code>Add-Type -TypeDefinition</code> and pass this code as a here-string &#8230; </p>

	<div class="csharp code csharp" style="font-family:monospace;"><br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.ComponentModel</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Management.Automation</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Text.RegularExpressions</span><span style="color: #008000;">;</span><br />
<span style="color: #000000;">&#91;</span>AttributeUsage<span style="color: #000000;">&#40;</span>AttributeTargets.<span style="color: #0000FF;">Field</span> <span style="color: #008000;">|</span> AttributeTargets.<span style="color: #0000FF;">Property</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> ValidatePatternExAttribute <span style="color: #008000;">:</span> ValidateEnumeratedArgumentsAttribute<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">private</span> RegexOptions _options <span style="color: #008000;">=</span> RegexOptions.<span style="color: #0000FF;">IgnoreCase</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span> _pattern<span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span> _message<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> ValidateElement<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> element<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>element <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">throw</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> ValidationMetadataException<span style="color: #000000;">&#40;</span>_message <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\n</span>ValidatePatternEx Failure: Argument Is Null&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #FF0000;">string</span> input <span style="color: #008000;">=</span> element.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; Regex regex <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; regex <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Regex<span style="color: #000000;">&#40;</span>_pattern, _options<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>regex.<span style="color: #0000FF;">Match</span><span style="color: #000000;">&#40;</span>input<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Success</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">throw</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> ValidationMetadataException<span style="color: #000000;">&#40;</span>_message <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\n</span>ValidatePatternEx failure, the value didn't match the pattern: &quot;</span> <span style="color: #008000;">+</span> _pattern<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #0600FF;">public</span> RegexOptions Options<br />
&nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; get<br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">return</span> _options<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; set<br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_options <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Pattern<br />
&nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; get<br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">return</span> _pattern<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; set<br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>value<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">throw</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> ArgumentException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;RegularExpression Pattern is null or empty&quot;</span>, <span style="color: #666666;">&quot;message&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_pattern <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Message<br />
&nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; get<br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">return</span> _message<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; set<br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>value<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">throw</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> ArgumentException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Error Message is null or empty&quot;</span>, <span style="color: #666666;">&quot;message&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_message <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div>

	<h4>Using it in PowerShell</h4>

	<p>When you use that in PowerShell, assuming that you called <code>Add-Type</code> with that as-is, you only have to change your function slightly, using ValidatePatternEx instead of ValidatePattern, and passing named properties, including the message.</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;">Test-<span style="font-style: normal;">PhoneNumber</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #666699; font-weight: bold;">param</span><span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#91;</span>ValidatePatternEx<span style="color: #333;">&#40;</span>Pattern<span style="color: #66cc66;">=</span><span style="color: #009900;">'^\(?\d{3}\)?[-\s.]?\d{3}[-\s.]\d{4}$'</span>, Message<span style="color: #66cc66;">=</span><span style="color: #009900;">'Please enter a 10-digit phone number like: (123) 555-1212'</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$number</span><br />
<span style="color: #333;">&#41;</span><br />
wrote<span style="color: #66cc66;">-</span>host <span style="color: #660033; font-weight: bold;">$number</span> <span style="color: #000066;">-fore</span> magenta<br />
<span style="color: #333;">&#125;</span></div>

	<p>But once you do, you will get much better error messages:</p>

	<p><span style="color:red;font-family:Consolas,'Courier New',monospace">Test-PhoneNumber : Cannot validate argument on parameter 'number'. Please enter a 10-digit phone number like: (123) 555-1212
ValidatePatternEx failure, the value didn't match the pattern: ^\(?\d{3}\)?[-\s.]?\d{3}[-\s.]\d{4}$</span></p>

	<h3>Other Applications</h3>

	<p>Of course, you may have noticed that this is really a completely custom parameter validator. You aren&#8217;t limited to just adding nice error messages to the validation error &#8212; you can create whatever validator you can dream up. Let me know what you come up with!</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/better-error-messages-for-powershell-validatepattern/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Another Module Manifest Gotcha</title>
		<link>http://huddledmasses.org/another-module-manifest-gotcha/</link>
		<comments>http://huddledmasses.org/another-module-manifest-gotcha/#comments</comments>
		<pubDate>Fri, 15 Jan 2010 03:42:36 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Error message]]></category>
		<category><![CDATA[Globally Unique Identifier]]></category>
		<category><![CDATA[Import-Module]]></category>
		<category><![CDATA[Metadata]]></category>
		<category><![CDATA[Modules]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[RequiredModules]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1386</guid>
		<description><![CDATA[Shay Levy, ScriptFanatic, wrote about the PowerShellHostVersion on his blog this morning, explaining how it is not a field you should use in your module manifests. Of course, there&#8217;s an exception: if your module is dependent on a specific host. For instance, if you&#8217;ve written a module for PoshConsole which exploits the WpfHost display features [...]]]></description>
			<content:encoded><![CDATA[	<p>Shay Levy, ScriptFanatic, wrote <a href="http://blogs.microsoft.co.il/blogs/scriptfanatic/archive/2010/01/14/module-manifest-gotcha.aspx">about the PowerShellHostVersion</a> on his blog this morning, explaining how it is <strong>not</strong> a field you should use in your <a href="http://msdn.com/library/dd878337.aspx">module manifests</a>.</p>

	<p>Of course, there&#8217;s an exception: if your module is dependent on a specific host.  For instance, if you&#8217;ve written a module for <a href="http://poshconsole.codeplex.com">PoshConsole</a> which exploits the WpfHost display features or the BgHost hotkeys feature &#8230; or if you&#8217;ve written <a href="http://code.msdn.microsoft.com/PowerShellPack">ISEPack</a> based on the menu and script-editor features of PowerShell <span class="caps">ISE</span>), then it makes sense to specify the <strong>PowerShellHostName</strong> and the <strong>PowerShellHostVersion</strong> together.  However, if you aren&#8217;t taking a dependency on a specific host &#8230; you should definitely <strong>never</strong> specify PowerShellHostVersion by itself.</p>

	<h3>RequiredModules</h3>

	<p>I&#8217;ve got another one that I think you should not use. PowerShell has several ways of specifying prerequisites for your modules, such as assemblies that should be loaded, modules that must be nested, etc. In every case <strong>except</strong> RequiredModules, PowerShell will actually import those things for you.</p>

	<p>That is, if you specify that you RequiredAssemblies = &#8220;System.Windows.Forms&#8221; it will automatically load it. If you specify NestedModules = &#8220;BitsTransfer&#8221; it will automatically load that. But if you specify RequiredModules = &#8220;BitsTransfer&#8221; you&#8217;re sore out of luck.</p>

	<p>Not only does PowerShell not load them for you, it doesn&#8217;t give you all the information you need to load them when it fails. For example, consider that you have a module called &#8220;ReallyRequired&#8221; and one called &#8220;TestModule&#8221; which requires ReallyRequired.  It&#8217;s metadata file is very simple, it looks like this:</p>

	<div class="posh code posh" style="font-family:monospace;">@<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; Author<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;Joel Bennett&quot;</span><br />
&nbsp; &nbsp; ModuleToProcess<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;TestModule.psm1&quot;</span>; ModuleVersion<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;1.0.0.0&quot;</span><br />
&nbsp; &nbsp; RequiredModules<span style="color: #66cc66;">=</span>@<span style="color: #333;">&#123;</span>ModuleName<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;ReallyRequired&quot;</span>;GUID<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;84b5ab08-3620-4f72-bffd-44d2a6bb506d&quot;</span>; ModuleVersion<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;2.5.0.0&quot;</span><br />
<span style="color: #333;">&#125;</span></div>

	<p>Before we try to import it, we might try to check which modules it requires, and since it doesn&#8217;t appear to require any, we&#8217;ll go ahead and import it:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #333;">&#93;</span>: <span style="color: #0066cc; font-style: italic;">get-<span style="font-style: normal;">module</span></span> <span style="color: #000066;">-list</span> TestModule <span style="color: #66cc66;">|</span> <span style="color: #660033;">Select</span> RequiredModule<br />
<br />
RequiredModules &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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color: #66cc66;">---------------</span> &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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color: #333;">&#123;</span><span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #333;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #333;">&#93;</span>: <span style="color: #0066cc; font-style: italic;">Import-<span style="font-style: normal;">Module</span></span> TestModule<br />
<br />
<span style="color: #0066cc; font-style: italic;">Import-<span style="font-style: normal;">Module</span></span> : The required module <span style="color: #009900;">'ReallyRequired'</span> <span style="color: #333399; font-weight: bold; font-style: italic;">is</span> <span style="color: #333399; font-weight: bold; font-style: italic;">not</span> loaded. <span style="color: #003366;">Load</span> the module <span style="color: #333399; font-weight: bold; font-style: italic;">or</span> remove the module <span style="color: #666699; font-weight: bold;">from</span> <span style="color: #009900;">'RequiredModules'</span> <span style="color: #666699; font-weight: bold;">in</span> the file <span style="color: #009900;">'C:\Users\Joel\Documents\WindowsPowerShell\Modules\TestModule\TestModule.psd1'</span>.</div>

	<p>Apparently, there&#8217;s just no way to tell which module(s) you need to preload (or even whether there are any) until you get an error message. You might think you could just write a script to test for that error and then run <code>Import-Module ReallyRequired</code> &#8230; but what you don&#8217;t know is that after you load your copy of the ReallyRequired module:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #333;">&#93;</span>: ImportModule TestModule<br />
<br />
<span style="color: #0066cc; font-style: italic;">Import-<span style="font-style: normal;">Module</span></span> : The required module <span style="color: #009900;">'ReallyRequired'</span> with version <span style="color: #009900;">'2.5.0.0'</span> <span style="color: #333399; font-weight: bold; font-style: italic;">is</span> <span style="color: #333399; font-weight: bold; font-style: italic;">not</span> loaded. <span style="color: #003366;">Load</span> the module <span style="color: #333399; font-weight: bold; font-style: italic;">or</span> remove the module <span style="color: #666699; font-weight: bold;">from</span> <span style="color: #009900;">'RequiredModules'</span> <span style="color: #666699; font-weight: bold;">in</span> the file <span style="color: #009900;">'C:\Users\Joel\Documents\WindowsPowerShell\Modules\TestModule\TestModule.psd1'</span>.</div>

	<p>If you&#8217;re like me, right now you&#8217;re tearing out your hair wondering why the error message didn&#8217;t tell you that in the first place. Then you have to go off and try to find a newer version of your RequiredModules. And that&#8217;s not even the crazy thing! The crazy thing is, if you find the wrong one, you could still get a message about the <span class="caps">GUID</span> not matching next.  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt='8-O' class='wp-smiley' /> </p>

	<p>So yeah, RequiredModules is really unlikely to be helpful right now. You&#8217;re better off just putting an <code>Import-Module ReallyRequired -version 2.5 -ErrorAction Stop</code> at the top of you script module (although that won&#8217;t help you if you&#8217;re writing a binary cmdlet module).</p>

	<h3>Some good news</h3>

	<p>This is just one of the things that the new PoshCode is being designed to address, so I&#8217;ll be blogging more about this shortly, but for now, you can use this function to list RequiredModules:</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;">Get-<span style="font-style: normal;">RequiredModules</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #666699; font-weight: bold;">Param</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Path</span><span style="color: #333;">&#41;</span><br />
<span style="color: #660033; font-weight: bold;">$dataSource</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Content</span></span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Resolve-<span style="font-style: normal;">Path</span></span> <span style="color: #660033; font-weight: bold;">$Path</span><span style="color: #333;">&#41;</span> <span style="color: #000066;">-delim</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">char</span><span style="color: #333;">&#93;</span></span><span style="color: #cc66cc;">0</span><br />
<br />
<span style="color: #666666; font-style: italic;">## We are ONLY going to &quot;Tokenize&quot; this to make sure there isn't an extra &quot;}&quot; which could lead to an exploit:</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$null</span> <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Management.<span style="color: #003366;">Automation</span>.<span style="color: #003366;">PSParser</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">Tokenize</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;&quot;</span>, <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">ref</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$null</span><span style="color: #333;">&#41;</span> &nbsp;<span style="color: #666666; font-style: italic;"># work around a PowerShell BUG</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$ParseErrors</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> <span style="color: #009900;">&quot;System.Collections.ObjectModel.Collection[System.Management.Automation.PSParseError]&quot;</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$global</span>:tokens <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Management.<span style="color: #003366;">Automation</span>.<span style="color: #003366;">PSParser</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">Tokenize</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$dataSource</span>, <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">ref</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$ParseErrors</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;">$ParseErrors</span>.<span style="color: #003366;">Count</span> <span style="color: #000066;">-gt</span> <span style="color: #cc66cc;">0</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$ParseErrors</span> <span style="color: #66cc66;">|</span> <span style="color: #66cc66;">%</span><span style="color: #333;">&#123;</span> <span style="color: #666699; font-weight: bold;">throw</span> <span style="color: #009900;">&quot;$($_.Message)<span style="color: #000099; font-weight: bold;">`n</span>$File at line:$($_.Token.StartLine) char:$($_.Token.Start)&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
<span style="color: #660033; font-weight: bold;">$dataSource</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$dataSource</span> <span style="color: #000066;">-replace</span> <span style="color: #009900;">&quot;<span style="color: #000099; font-weight: bold;">`n</span># SIG #&quot;</span>,<span style="color: #009900;">&quot;<span style="color: #000099; font-weight: bold;">`n</span> # SIG #&quot;</span><br />
<span style="color: #666699; font-weight: bold;">return</span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Invoke-<span style="font-style: normal;">Expression</span></span> <span style="color: #009900;">&quot;DATA {<span style="color: #000099; font-weight: bold;">`n</span> $dataSource <span style="color: #000099; font-weight: bold;">`n</span>}&quot;</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">RequiredModules</span><br />
<span style="color: #333;">&#125;</span></div>

<h6 class="zemanta-related-title">Related articles by Zemanta</h6><ul class="zemanta-article-ul"><li class="zemanta-article-ul-li"><a href="http://huddledmasses.org/a-question-about-powershell-module-manifests/">A question about PowerShell Module Manifests</a> (huddledmasses.org)</li></ul>

<div class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/bf32d64f-fae9-4f34-ab85-385a6242e5ba/" title="Reblog this post [with Zemanta]"><img class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=bf32d64f-fae9-4f34-ab85-385a6242e5ba" 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://huddledmasses.org/another-module-manifest-gotcha/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

