<?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; Modules</title>
	<atom:link href="http://huddledmasses.org/tag/modules/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>Working with multiple versions of PowerShell Modules</title>
		<link>http://huddledmasses.org/working-with-multiple-versions-of-powershell-modules/</link>
		<comments>http://huddledmasses.org/working-with-multiple-versions-of-powershell-modules/#comments</comments>
		<pubDate>Wed, 31 Aug 2011 04:14:45 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Modules]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Versioning]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1744</guid>
		<description><![CDATA[What follows is a brief explanation of modules, followed by an explanation of how I handle multiple versions of a module. My way isn&#8217;t the only way &#8212; and I&#8217;m rather annoyed that I have to do it &#8212; but it works, and might help someone, so here it is. Some back-story During development of [...]]]></description>
			<content:encoded><![CDATA[	<p>What follows is a brief explanation of modules, followed by an explanation of how <strong>I</strong> handle multiple versions of a module. My way isn&#8217;t the only way &#8212; and I&#8217;m <em>rather annoyed</em> that I have to do it &#8212; but it works, and might help someone, so here it is.</p>

	<h2>Some back-story</h2>

	<p>During development of a PowerShell module like <a href="http://ShowUI.CodePlex.com">ShowUI</a>, I am usually <strong>not</strong> using a released version of the module, since I&#8217;m obviously working on the next version. In addition, sometimes I need to have &#8220;beta&#8221; and &#8220;stable&#8221; releases &#8230; so I might have two or three versions of the module that I need to be able to load in order to support users and actually do development.</p>

	<p>In case you don&#8217;t know how modules work, here&#8217;s the deal: There&#8217;s an environment variable <code>PSModulePath</code>. It&#8217;s just like the <code>Path</code> environment variable, but for modules. The variable contains a semi-colon delimited list of folders paths, which PowerShell searches for modules.  Some people think of a PowerShell module as basically a .dll (binary module), a .psm1 (script module), or a .psd1 (manifest module)... but it&#8217;s never <em>just</em> a file, it&#8217;s a <em>*Folder* and a file</em>.  In order for PowerShell to find ShowUI when you write <code>Import-Module ShowUI</code>, you have to have a <strong>Folder</strong> in the PSModulePath named &#8220;ShowUI&#8221; and in it, a file (either .dll, .psm1, or .psd1) also named ShowUI.</p>

	<h3>What I would <em>like</em></h3>

	<p>The Import-Module command has a -Version parameter. What I expected What I would <em>like</em> to be able to do is create a folder &#8220;ShowUI&#8221; and in there, create folders &#8220;1.0&#8221; and &#8220;1.1&#8221; and &#8220;1.2&#8221; and then be able to use <code>Import-Module ShowUI -Version 1.1</code> to load a specific version. </p>

	<p>Sadly, this doesn&#8217;t work. First of all, the -Version parameter on Import-Module, is a <strong>minimum</strong> only. Additionally, Import-Module requires the file and folder to match names exactly, so putting the module inside a subfolder doesn&#8217;t work even if there&#8217;s only one.</p>

	<p>However, there are two things we can use to fix our problem:</p>

	<p>1. Just like the <span class="caps">PATH</span> variable, PSModulePath works in order. PowerShell searches the listed paths one at a time and uses the first Module that meets the specified minimimum version.<br />
2. Additionally, PowerShell allows you to specify a relative Path to a module that starts in a PSModulePath &#8230; I&#8217;ll explain more in a moment.</p>

	<h2>My <em>actual</em> working environment</h2>

	<p>All of my development projects are in $Home\Projects, so I put a &#8220;Modules&#8221; folder in there which is where I put modules that I&#8217;m actively working on.  Then I add that folder to my PSModulePath environment variable: @$Env:PSModulePath += &#8220;;$Home\Projects&#8221; ...</p>

	<p>Since I put it on the end, modules in there won&#8217;t be imported if there&#8217;s a module with the same name in my regular Module folder ( $Home\Documents\WindowsPowerShell\Modules ), <strong>unless</strong> I increment the version number in the psd1 metadata file and specify -Version when importing.</p>

	<p>For example, I have two copies of WASP:</p>

	<p>$Home\Documents\WindowsPowerShell\Modules\WASP\Wasp.psd1  (version 1.0)<br />
$Home\Projects\Modules\WASP\Wasp.psd1 (version 2.0.0.2)</p>

	<p>If I want to load the released version, I can just <code>Import-Module WASP</code> &#8212; this ensures that scripts that I have in my scripts folder generally load the released version of <span class="caps">WASP</span>.  When I want to load the development version, I can <code>Import-Module WASP -Version 2.0</code>, and get the version from my Projects folder, because the released version&#8217;s version number is lower than 2.0.</p>

	<h2>More layers onion boy?</h2>

	<p>When I need to have more than one older version, this presents a problem, because I would need another folder in my path, and for the numbering scheme to work despite the fact that -Version is only a minimum value, I would need the released version (say 1.0) to come first in the path, then the beta version (say 2.0.1), then the development version (say 2.1) ... which means I&#8217;d need to add a $Home\Projects\BetaReleaseModules or something like that, and put it in my PSModulePath before the $Home\Projects\Modules folder.  That would work, but you can imagine that it wouldn&#8217;t scale very well.</p>

	<p>Here&#8217;s what I do: past the release and development versions, I start creating folders like this:</p>

	<p>$Home\Documents\WindowsPowerShell\Modules\ShowUI1.0\ShowUI.psd1  (version 1.0)<br />
$Home\Documents\WindowsPowerShell\Modules\ShowUI\ShowUI.psd1  (Release version: 1.1)<br />
$Home\Documents\WindowsPowerShell\Modules\ShowUI1.1\ShowUI.psd1  (Release version: 1.1)<br />
$Home\Documents\WindowsPowerShell\Modules\ShowUI1.2\ShowUI.psd1  (Beta version 1.2)<br />
$Home\Projects\Modules\ShowUI\ShowUI.psd1 (Development version 1.3)</p>

	<p>Now, if I <code>Import-Module ShowUI</code>, I&#8217;ll get 1.1 and if I specify <code>Import-Module ShowUI -Version 1.2</code> I&#8217;ll get &#8230; version 1.3 (<em>whoops</em>).</p>

	<p>To load ShowUI 1.2 with that structure I have to do this: <code>Import-Module ShowUI1.2\ShowUI</code> &#8212; specifying the folder name <strong>and</strong> the module name.  Of course, I could use &#8220;ShowUIBeta&#8221; as the folder name instead, but usually when I get a request for help writing scripts, I can get people to give me the specific version that they&#8217;re working with, so I like the numbered folders instead.  </p>

	<p>A last important note is that I <strong>do not</strong> rename the psd1 or anything else.  </p>

	<p>I <em>could</em> rename the metadata file to $Home\Documents\WindowsPowerShell\Modules\ShowUI1.2\ShowUI1.2.psd1 and then I&#8217;d be able to <code>Import-Module ShowUI1.2</code>, but there&#8217;s a catch.  If I rename it, then I&#8217;ve renamed the module.  If I leave the metadata file as &#8220;ShowUI.psd1&#8221; then <em>once I&#8217;ve imported the module</em> I can call <code>Get-Module ShowUI</code> or <code>Get-Command -Module ShowUI</code> and they still work as they should.</p>

	<p>In any case, this folder-naming stuff is less than ideal and will hopefully be fixed in a future release of PowerShell, but with a &#8220;Projects\Modules&#8221; folder I can have two versions, and usually a development and release version is enough.  Personally, I keep old releases of major modules around in numbered folders just in case, but I rarely actually use them.</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/working-with-multiple-versions-of-powershell-modules/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PowerShell Scripting Best Practices: Prefix A</title>
		<link>http://huddledmasses.org/powershell-scripting-best-practices-prefix-a/</link>
		<comments>http://huddledmasses.org/powershell-scripting-best-practices-prefix-a/#comments</comments>
		<pubDate>Fri, 30 Jul 2010 02:45:52 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Modules]]></category>
		<category><![CDATA[PowerShell]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1516</guid>
		<description><![CDATA[I&#8217;m starting a new series of blog posts about Best Practices for scripting in PowerShell, and I was going to start at the beginning with a requirement that you should use [CmdletBinding()], but the explanation of that will have to wait for the next post, because a bug in PowerShell 2.0 has surfaced which can [...]]]></description>
			<content:encoded><![CDATA[	<p>I&#8217;m starting a new series of blog posts about Best Practices for scripting in PowerShell, and I was going to start at the beginning with a requirement that you should use [CmdletBinding()], but the explanation of that will have to wait for the next post, because a bug in PowerShell 2.0 has surfaced which can only be avoided by carefully following a couple of rules &#8230; and I&#8217;m going to issue those rules as the prefix for the best practices.</p>

	<h3>Rule #1: Never use <code>Get-Module -ListAvailable</code> in a module.</h3>

	<p>You need to run the command outside of your module.  The simplest way to do that is to use <code>Invoke-Command { Get-Module -List Available }</code> &#8230; that should give you the same output, but without the nasty side effects.</p>

	<p>There is <a href="https://connect.microsoft.com/PowerShell/feedback/details/580927">a bug</a> in the <code>ListAvailable</code> parameter, which causes PowerShell to mark other modules <em>which are already loaded</em> as being &#8220;nested&#8221; in your module. </p>

	<p>It does that to all modules which have correct manifests, and the problem is that if your module gets unloaded later (using Remove-Module), it removes these nested modules as well &#8230; even though the user had loaded them separately.</p>

	<h5> <img src='http://huddledmasses.org/wordpress/wp-includes/' alt='[new]' class='wp-smiley' />  <strong>Edit: 2010-08-05</strong></h5>

	<p>I should have mentioned this originally (so I&#8217;m adding it now): one of the reasons developers were using Get-Module with -ListAvailable was to get information <em>about their <strong>own</strong> module</em> during the initial load (such as to check module versions, or load data from the PrivateData). If you were doing that, you can use Test-ModuleManifest instead with the path to your manifest: <code>Test-ModuleManifest $PSScriptRoot\ModuleName.psd1</code></p>

	<h3>Rule #2: Do not use <code>Import-Module</code> in a module.</h3>

	<p>If you have a dependency on another module, you should load it by specifying it as a <code>NestedModules</code> in your module manifest. </p>

	<p>There is a bug in the way that Remove-Module unloads modules which causes modules which are loaded from <em>inside</em> your module (whether by Import-Module or by Get-Module -ListAvailable), to be unloaded <em>completely</em> even if they&#8217;ve been previously loaded in the console (global) scope interactively by the user (or via their profile). </p>

	<p>You could possibly make an exception to this rule, if the module is actually in your module&#8217;s folder (and thus, not easily loadable from outside your module), but you&#8217;re making the assumption that no one will ever use that module outside of your module.  </p>

	<p>Modules loaded via the metadata file&#8217;s <code>NestedModules</code> property don&#8217;t have this problem, so you should always load nested modules that way.</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/powershell-scripting-best-practices-prefix-a/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Import Binary Modules from Network Shares</title>
		<link>http://huddledmasses.org/how-to-import-binary-modules-from-network-shares/</link>
		<comments>http://huddledmasses.org/how-to-import-binary-modules-from-network-shares/#comments</comments>
		<pubDate>Sat, 03 Jul 2010 06:15:04 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[.Net4]]></category>
		<category><![CDATA[CAS]]></category>
		<category><![CDATA[FileShare]]></category>
		<category><![CDATA[Modules]]></category>
		<category><![CDATA[Policy]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[PowerUser]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[UNC]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1506</guid>
		<description><![CDATA[Note: This is from a wiki page I just wrote on Importing Binary Modules from Network Shares which discusses not just the solution below that works for .Net 2.0 but also how to solve the problem on .Net 4.0 (e.g.: in PoshConsole). I will most likely not keep this page up to date, so you [...]]]></description>
			<content:encoded><![CDATA[	<p><strong>Note:</strong> This is from a wiki page I just wrote on <a href="http://wiki.poshcode.org/FAQ/Problems_and_Gotchas/Importing_Binary_Modules_from_Network_Shares">Importing Binary Modules from Network Shares</a> which discusses not just the solution below that works for .Net 2.0 but also how to solve the problem on .Net 4.0 (e.g.: in PoshConsole).  I will most likely <em>not</em> keep this page up to date, so you should refer to that wiki if you need more information.</p>

	<p>Almost every author of a binary module has probably had someone ask about this at some point, because there&#8217;s always someone who has their user profiles stored on a network location, and therefore installed their modules on that network path and can&#8217;t get them to load because they get a warning that .Net &#8220;Failed to grant minimum permission requests.&#8221;</p>

	<p>Before we get into this any further let me just say: <strong>by far</strong> the simplest thing to do is to create a local folder on your local hard drive, add that to your environment PSPathModules variable, and just install your modules there.</p>

	<p>Other than that, the solution depends on the version of .Net that you&#8217;re using (you can tell by checking the $PSVersionTable.CLRVersion</p>

	<h3>The .Net 2.0 framework (and 3.0 and 3.5 and 3.5 SP1)</h3>

	<p>The problem is not a PowerShell problem at all, it&#8217;s a .Net problem. The .Net framework 2.0 (remember that PowerShell targets 2.0, and is actually based on .Net 1.1) didn&#8217;t trust assemblies loaded from network shares. You can fix that for an individual assembly or for a whole share using the <a title="http://msdn.microsoft.com/en-us/library/cb6t8dtz(VS.80).aspx" class=" external" rel="external nofollow" href="http://msdn.microsoft.com/en-us/library/cb6t8dtz%28VS.80%29.aspx" target="_blank">Caspol</a> tool.</p>

	<p>A complete discussion of that tool and it&#8217;s myriad command-line options is beyond me, but for a simple solution, you can run this command specifying the server and share you want to load from (in my example the &#8220;Modules&#8221; share on the &#8220;ProfileServer&#8221; server).</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">Alias</span></span> CasPol <span style="color: #009900;">&quot;$([Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory())CasPol.exe&quot;</span> <br />
CasPol <span style="color: #000066;">-pp</span> off <span style="color: #000066;">-machine</span> <span style="color: #000066;">-addgroup</span> <span style="color: #cc66cc;">1.2</span> <span style="color: #000066;">-url</span> file:<span style="color: #66cc66;">//</span>\ProfileServer\Modules\<span style="color: #66cc66;">*</span> FullTrust</div>

	<p>Hopefully the only thing that needs explaning there is that 1.2 is the default &#8220;Local Intranet&#8221; group, and that CasPol.exe is in your Framework Runtime directory. Once you&#8217;ve run that, you&#8217;ll be able to import any modules that are in subdirectories of that share.</p>

	<p><b>Note:</b> You <em>must</em> run the version of CasPol.exe which is in the lcation defined by the GetRuntimeDirectory() command (it&#8217;s important to use the same version as the runtime you want to be affected).</p>

	<p>You can read more about <a href="http://wiki.poshcode.org/FAQ/Problems_and_Gotchas/Importing_Binary_Modules_from_Network_Shares">importing binary modules from network shares</a>, including how it changed in .Net 3.5 SP1 and why it&#8217;s not automatically fixed in .Net 4 over on the PoshCode wiki.  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=':)' class='wp-smiley' /> </p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/how-to-import-binary-modules-from-network-shares/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>F.A.Q.: How do I Install a PowerShell Module</title>
		<link>http://huddledmasses.org/f-a-q-how-do-i-install-a-powershell-module/</link>
		<comments>http://huddledmasses.org/f-a-q-how-do-i-install-a-powershell-module/#comments</comments>
		<pubDate>Wed, 26 May 2010 20:00:52 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Install]]></category>
		<category><![CDATA[Modules]]></category>
		<category><![CDATA[PoshCode]]></category>
		<category><![CDATA[PowerShell]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1482</guid>
		<description><![CDATA[As a warm up to writing my best-practices posts, I decided to answer this frequently asked question on the PowerShell wiki at PoshCode. I&#8217;m not going to repeat the whole post here, but suffice it to say that there&#8217;s a good explanation on the How Do I Install a PowerShell Module page, along with this [...]]]></description>
			<content:encoded><![CDATA[	<p>As a warm up to writing my best-practices posts, I decided to answer this frequently asked question on the PowerShell wiki at PoshCode. I&#8217;m not going to repeat the whole post here, but suffice it to say that there&#8217;s a good explanation on the <a href="http://wiki.poshcode.org/FAQ/Tips_and_Tricks/How_Do_I..._Install_a_Module">How Do I Install a PowerShell Module</a> page, along with this script:</p>

	<p><script type="text/javascript" src="http://PoshCode.org/embed/1875"></script></p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/f-a-q-how-do-i-install-a-powershell-module/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PowerShell Module Manifests Tip: allowed cmdlets and variables</title>
		<link>http://huddledmasses.org/powershell-module-manifests-tip-allowed-cmdlets-and-variables/</link>
		<comments>http://huddledmasses.org/powershell-module-manifests-tip-allowed-cmdlets-and-variables/#comments</comments>
		<pubDate>Mon, 18 Jan 2010 02:56:58 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Manifest]]></category>
		<category><![CDATA[Metadata]]></category>
		<category><![CDATA[Modules]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Windows 7]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1394</guid>
		<description><![CDATA[I was under the impression that module manifests allow only DATA stuff. In fact, if you try to use cmdlets or variables in them, you get the usual Data-language errors, like this: Import-Module : The module manifest &#8216;C:\Modules\Test\Test.psd1&#8217; could not be processed because it is not a valid PowerShell restricted language file. Please remove the [...]]]></description>
			<content:encoded><![CDATA[	<p>I was under the impression that module manifests allow only <span class="caps">DATA</span> stuff. In fact, if you try to use cmdlets or variables in them, you get the usual Data-language errors, like this:</p>

	<p><code style="color: red; margin-left: 10px;"></code><p>Import-Module : The module manifest &#8216;C:\Modules\Test\Test.psd1&#8217; could not be processed because it is not a valid PowerShell restricted language file. Please remove the elements that are not permitted by the restricted language: The command &#8216;Get-ChildItem&#8217; is not in allowed in restricted language mode or a Data section.</p><br />
<p>Import-Module : The module manifest &#8216;C:\Modules\Test\Test.psd1&#8217; could not be processed because it is not a valid PowerShell restricted language file. Please remove the elements that are not permitted by the restricted language: A variable that cannot be referenced in restricted language mode or a Data section is being referenced. Variables that can be referenced include the following: $PSCulture, $PSUICulture, $true, $false, and $null.</p></p>

	<p><span class="caps">BUT</span> <span class="caps">THEN</span> I came across this in the Windows 7 built-in BitsTransfer module:</p>

	<p>RequiredAssemblies=Join-Path $psScriptRoot &#8220;Microsoft.BackgroundIntelligentTransfer.Management.Interop.dll&#8221;</p>

	<p><span class="caps">AND</span> IT WORKS?!</p>

	<p>Well, that&#8217;s very weird, because <code>Join-Path</code> is certainly not allowed in a normal &#8220;restricted language&#8221; file, and (as you can tell from above) neither is <code>$PsScriptRoot</code> &#8212; in fact, as far as I know, you shouldn&#8217;t have to do that at all, since RequiredAssemblies knows enough to look in your module folder&#8230; but nevermind that, why does it work?</p>

	<h3>The Rest of the Story</h3>

	<p>So I went digging, and it turns out that although they are parsed as Restricted Language (like a &#8220;data section,&#8221; see <a href="http://technet.microsoft.com/en-us/library/dd347678.aspx">about_Data_Sections</a>), module manifests are allowed extra cmdlets:  &#8220;Import-LocalizedData&#8221;, &#8220;ConvertFrom-StringData&#8221;, &#8220;Write-Host&#8221;, &#8220;Out-Host&#8221;, &#8220;Join-Path&#8221; and even special variables that aren&#8217;t normally allowed in data sections: specifically $PsScriptRoot which is the ModuleBase (the parent folder of the psd1) and environment variables ($Env:), plus the usual $PSCulture, $PSUICulture, and of course $true, $false, and $null.</p>

	<p>However, although you can use those variables, you can&#8217;t embed them in strings, so if you wanted to use <code>$Env:Windir</code> or <code>$Env:Temp</code> as part of a path (for instance), you need to take advantage of the availability of <code>Join-Path</code>.</p>

	<p>Now, I can&#8217;t find this documented anywhere (although I did add it to the <a href="http://msdn.microsoft.com/en-us/library/dd878337%28VS.85%29.aspx">module manifest documentation on MSDN</a>), but it&#8217;s true, nonetheless &#8212; you&#8217;ll just have to trust me  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=':)' class='wp-smiley' />  Yeah, PowerShell is starting to drive me crazy again.</p>

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

<div class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/4ca92c55-78cf-49bf-9cd4-a02adb78774d/" title="Reblog this post [with Zemanta]"><img class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=4ca92c55-78cf-49bf-9cd4-a02adb78774d" 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/powershell-module-manifests-tip-allowed-cmdlets-and-variables/feed/</wfw:commentRss>
		<slash:comments>1</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>
		<item>
		<title>A question about PowerShell Module Manifests</title>
		<link>http://huddledmasses.org/a-question-about-powershell-module-manifests/</link>
		<comments>http://huddledmasses.org/a-question-about-powershell-module-manifests/#comments</comments>
		<pubDate>Thu, 07 Jan 2010 05:44:23 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[CPAN]]></category>
		<category><![CDATA[Metadata]]></category>
		<category><![CDATA[Module Manifest]]></category>
		<category><![CDATA[Modules]]></category>
		<category><![CDATA[PoshCode]]></category>
		<category><![CDATA[Windows PowerShell]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1382</guid>
		<description><![CDATA[So, I&#8217;m building the next PoshCode around modules, with CPAN as my model &#8230; Scripts are still there, but they&#8217;re still basically done the way they are now: you just upload a single file. Modules, however, will be treated specially. You&#8217;ll have to have an account to upload, but you&#8217;ll be able to just upload [...]]]></description>
			<content:encoded><![CDATA[	<p>So, I&#8217;m building the next PoshCode around modules, with <span class="caps">CPAN</span> as my model &#8230; </p>

	<p>Scripts are still there, but they&#8217;re still basically done the way they are now: you just upload a single file. </p>

	<p>Modules, however, will be treated specially. You&#8217;ll have to have an account to upload, but you&#8217;ll be able to just upload a .zip file of your module folder using your login and not fill in any other forms. Our system will take care of parsing the metadata out of the <a href="http://msdn.microsoft.com/en-us/library/dd878337%28VS.85%29.aspx">manifest</a> in your module folder.</p>

	<p>The problem is that I need a little data that isn&#8217;t a part of the <a href="http://msdn.microsoft.com/en-us/library/dd878337%28VS.85%29.aspx">standard module manifest</a> format&#8230; and I can&#8217;t add it because PowerShell won&#8217;t load a module with extra fields in the manifest metadata hashtable: https://connect.microsoft.com/PowerShell/feedback/ViewFeedback.aspx?FeedbackID=421837 &#8212; My request was closed &#8220;by design&#8221; early in the beta cycle and I wasn&#8217;t able to convince them to rethink that.</p>

	<p>At a minimum, PoshCode <strong>requires</strong> a <span class="caps">LICENSE</span> field, and a <span class="caps">CATEGORY</span> field (think of the categories on <span class="caps">CPAN</span> or TechNet Script Center).</p>

	<p>So I&#8217;m having an informal poll. (you&#8217;ll have to &#8220;vote&#8221; by commenting or tweeting to me, because I want more than just &#8220;I choose A&#8221;).</p>

	<p>Which option do you prefer, or can you think of a better way:</p>

	<h4>Require the License and Category to be part of the <span class="caps">PRIVATEDATA</span> field in the standard manifest</h4>

	<p>This might require authors to rewrite parts of their modules, because we&#8217;d be forcing PrivateData to be a Hashtable, and to contain keys that they don&#8217;t need in the module.</p>

	<p>Some existing modules use an array in PrivateData, or even a simple string, rather than a Hashtable. However, it&#8217;s not a <strong>huge</strong> difference, and might be the cleanest method: it wouldn&#8217;t require you to manage a second file of data, and the additional data will be easily available to users and scripts via the standard Get-Module output.</p>

	<h4>Add a &#8220;doc comment&#8221; system for Module manifest files.</h4>

	<p>The upside of this is that PoshCode wants to create module-level documentation anyway. If we use documentation comments like those used on functions we would be able to just create our own standard for them, and add any extra fields we need. It would be guaranteed not to conflict with anything you&#8217;re already doing, and in addition to missing metadata, you could have some module-level documentation beyond just the Description field of the metadata.  </p>

	<p>The problem with this is that there&#8217;s no built-in way to parse that, and doing so isn&#8217;t trivial, so you would need to just read it on our website, or read the source of the file, or have our PoshCode cmdlets in order to make any sense of it once you had a module on your system. It doesn&#8217;t integrate with PowerShell in any meaningful way.</p>

	<h4>Embed the extra data right in the manifest hashtable, using a special comment to hide it from Get-Module</h4>

	<p><code lang=&#8220;posh&#8221;<br />
@{<br />
author=&#8220;Joel Bennett&#8221;<br />
description=&#8220;My latest crazy module&#8221;<br />
<#!PoshCode<br />
License=&#8220;Ms-PL&#8221;<br />
Category=&#8220;WPF&#8221;,&#8220;GUI&#8221;,&#8220;Toolkit&#8221;
#><br />
CompanyName=&#8220;Huddled Masses&#8221;<br />
...<br />
}<br />
</code></p>

	<p>I like this because it&#8217;s fairly trivial for us to strip out the comment and just turn that into a plain-old <span class="caps">DATA</span> section.  It also lends itself to reminding the PowerShell team that these fields are missing, and maintains the existing simple syntax of the manifest.  </p>

	<p>The problem here is, again, that the data doesn&#8217;t appear using any of the standard PowerShell tools &#8212; but getting it out is significantly easier than getting out document comments&#8230;</p>

	<h4>Require a separate &#8220;ReadMe.psd1&#8221; (or &#8220;PoshCode.pds1&#8221; or &#8220;Metadata.psd1&#8221;) metadata file</h4>

	<p>You could start with a copy of your module manifest, and then add the extra stuff that PoshCode needs. This would be nice because we would be able to add any extra fields we wanted as mandatory members, and we could include the &#8220;documentation&#8221; stuff I mentioned earlier&#8230;</p>

	<p>But the downside is that it would be a third file (second manifest) that authors would have to maintain and keep current.</p>

	<p>Any thoughts? Suggestions? </p>

	<p>I&#8217;ve put some further thoughts <a href="http://wiki.poshcode.org/PoshCode_Project/CPAN_and_PoshCode_vNext">about CPAN</a> and the <a href="http://wiki.poshcode.org/PoshCode_Project/Module_Upload_System">data problem</a> on the wiki.</p>

<div class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/afd48ed4-ce64-4ce4-a1a3-9d7daffc69ed/" title="Reblog this post [with Zemanta]"><img class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=afd48ed4-ce64-4ce4-a1a3-9d7daffc69ed" 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/a-question-about-powershell-module-manifests/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>PoshCode Updated for CTP3</title>
		<link>http://huddledmasses.org/poshcode-updated-for-ctp3/</link>
		<comments>http://huddledmasses.org/poshcode-updated-for-ctp3/#comments</comments>
		<pubDate>Wed, 24 Dec 2008 20:35:17 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Advanced Functions]]></category>
		<category><![CDATA[CTP3]]></category>
		<category><![CDATA[Modules]]></category>
		<category><![CDATA[PoshCode]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[PowerShell Functions]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=930</guid>
		<description><![CDATA[I&#8217;ve updated the PoshCode script module to support CTP3, and added a -limit parameter to the Get-PoshCode cmdlet so you can specify how many items you want retrieved in the case where there are a lot of matches for your search terms &#8212; by default the limit is 25. Improvements to the underlying web search [...]]]></description>
			<content:encoded><![CDATA[	<p>I&#8217;ve updated the <a href="http://poshcode.org/PoshCode.psm1">PoshCode script module</a> to support CTP3, and added a -limit parameter to the Get-PoshCode cmdlet so you can specify how many items you want retrieved in the case where there are a lot of matches for your search terms &#8212; by default the limit is 25.</p>

	<h3>Improvements to the underlying web search <span class="caps">API</span></h3>

	<p>You’ve always been able to pass a <span class="caps">LIST</span> parameter to the <span class="caps">API</span>, and get more results by specifying a higher number. But it never worked with the &#8220;path&#8221; notation (until now).</p>

	<p>That is, you used to be able to do:</p>

	<ul>
		<li>http://poshcode.org/api1?q=start&#038;list=10</li>
	</ul>
	<ul>
		<li>http://poshcode.org/api1?q=start&#038;list=100</li>
	</ul>

	<p>To make the <span class="caps">API</span> a little easier to use I’ve enhanced it just now:</p>

	<ol>
		<li>You can now page the search results. </li>
		<li>You can use the word &#8220;limit&#8221; instead of &#8220;list&#8221;</li>
		<li>If you specify limit=0 (or list=0) I’ll give you everything I’ve got.  Please use a little precaution about that, as it could be a <span class="caps">LOT</span> of data. I’d much rather you retrieve, say … 25, and then get the second page if you want more.</li>
	</ol>
	<ol>
		<li>You can use path notation.</li>
	</ol>

	<p>So, you can use any of these URLs:</p>

	<ul>
		<li>http://poshcode.org/api1/start/list/25/page/1</li>
		<li>http://poshcode.org/api1/start/limit/25/page/2</li>
		<li>http://poshcode.org/api1?q=start&#038;list=25&#038;page=3</li>
	</ul>
	<ul>
		<li>http://poshcode.org/api1?q=start&#038;limit=25&#038;page=4</li>
	</ul>

	<p>There are a lot of search results for &#8220;start&#8221; ... feel free to play with enhancing the PoshCode module, or incorporating this into your apps, etc.</p>

	<p> <img src='http://huddledmasses.org/wordpress/wp-includes/' alt='[new]' class='wp-smiley' />  I should add that you don&#8217;t <em>have</em> to specify the limit or page number.  By default you&#8217;ll get the first 10 items, which should be enough.  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=':)' class='wp-smiley' /> </p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/poshcode-updated-for-ctp3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting Started with PowerShell 2 &#8211; Part 3</title>
		<link>http://huddledmasses.org/getting-started-with-powershell-2-part-3/</link>
		<comments>http://huddledmasses.org/getting-started-with-powershell-2-part-3/#comments</comments>
		<pubDate>Thu, 24 Jul 2008 04:40:38 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[BestPractices]]></category>
		<category><![CDATA[CodeSigning]]></category>
		<category><![CDATA[GettingStarted]]></category>
		<category><![CDATA[Modules]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Profile]]></category>
		<category><![CDATA[WalkThrough]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/?p=561</guid>
		<description><![CDATA[My &#8220;getting started&#8221; series ran out of steam a bit partly because I didn&#8217;t get much feedback on them &#8212; maybe you&#8217;re not interested, or maybe it wasn&#8217;t easy enough, or was just too confusing. In any case, I want to put up at least this one last post to suggest that you get the [...]]]></description>
			<content:encoded><![CDATA[	<p>My &#8220;getting started&#8221; series ran out of steam a bit partly because I didn&#8217;t get much feedback on them &#8212; maybe you&#8217;re not interested, or maybe it wasn&#8217;t easy enough, or was just too confusing. In any case,  I want to put up at least this one last post to suggest that you get the <a href="http://PoshCode.org/">PowerShell Code Repository</a> set up, and to show you the final version of my profile script and how it loads the various pieces it needs, and then I&#8217;ll send you on your way. </p>

	<p>Once you&#8217;ve got your PowerShell <a href="/getting-started-with-powershell-2-part-1">all installed</a> and have set up <a href="/getting-started-with-powershell-2-part-2">your first profile</a> to auto-load &#8230; you&#8217;re going to want some scripts (well, maybe you&#8217;ll want to learn more about how to use PowerShell, but go with me on this)! </p>

	<p>One of the best places to look for scripts is the <a href="http://PoshCode.org/">PowerShell Code Repository</a>, and although you can browse and search on the website, you can also do it using the PoshCode <a href="http://PoshCode.org/PoshCode.psm1">module</a> (or the <span class="em2">version 1 compatible</span> <a href="http://PoshCode.org/PoshCode.ps1">script</a>).  These scripts include a <code>Get-PoshCode</code> cmdlet which you can use with search terms to get a list of scripts and cmdlets back, or with numeric IDs to download scripts (you&#8217;ll see what I mean later on, for now, go ahead and grab the appropriate version of that script).</p>

	<p>I&#8217;m going to assume you put it into your AutoModules folder. If you grabbed the module, it should be saved to  WindowsPowerShell\AutoModules\PoshCode\PoshCode.psm1 otherwise, to WindowsPowerShell\AutoModules\PoshCode.ps1 &#8230; but you may have run into a minor problem if you load the .ps1 version <span style="padding-left:1em;padding-right:1em;text-align:left;" class="em2">)</span>. Both the module and the script are signed, but they are signed by <a href="/JoelBennett_Code-Signing.crt">my self-issued code signing certificate</a> which your computer almost certainly doesn&#8217;t trust&#8230;  You can use the signature to verify that the file hasn&#8217;t been modified since I signed it, but that&#8217;s about all (and even that&#8217;s a bit of a trick). To actually use the script (module), you&#8217;ll need to sign the script yourself (see the steps and how to get a certificate <a href="/getting-started-with-powershell-2-part-2">in part 2</a>).</p>

	<p><strong>If you&#8217;re on CTP2</strong>, this would be a good time to get my Authenticode script module to help with signing, and to learn a little about those PoshCode cmdlets &#8230; <span id="more-561"></span></p>

	<h3>Using PoshCode to get a module.</h3>

	<p>Create a folder in your AutoModules folder named &#8220;Authenticode&#8221; ... and then inside it, try running <code>Get-PoshCode Authenticode</code> &#8230; You should get a list of 3 or more versions of my script, entitled &#8220;Get/Set Signature (CTP2)&#8221;.  You want the latest version, so pick the one with the biggest number for the ID, and run <code>Get-PoshCode 464</code>.  That should download it and save it. You&#8217;ll be able to load it using Add-Module (since Add-Module in the current <span class="caps">CTP</span> doesn&#8217;t care about code-signing), but <strong>first</strong> you need to create a settings file (it will tell you how, if you try to load it without setting it).  change the <strong>CertificateThumbprint</strong> that&#8217;s set in the first line of code in that script &#8212; set it to the thumbprint of your personal code-signing cert (which you&#8217;ve hopefully imported into your certificate store).</p>

	<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: #660033;">cd</span> <span style="color: #660033; font-weight: bold;">$ProfileDir</span>\AutoModules<br />
<span style="color: #660033;">mkdir</span> Authenticode <span style="color: #66cc66;">|</span> <span style="color: #660033;">cd</span> <span style="color: #666666; font-style: italic;"># Skip this line if you're on v1</span><br />
<span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">PoshCode</span></span> Authenticode <span style="color: #66cc66;">|</span> <span style="color: #660033;">ft</span> Id, Title, Author, Description <span style="color: #000066;">-auto</span><br />
<span style="color: #666666; font-style: italic;"># assuming the first hit is the newest one ... hit the up arrow, and add:</span><br />
<span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">PoshCode</span></span> <span style="color: #009900;">&quot;Authenticode Signature CTP2&quot;</span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">select</span> <span style="color: #000066;">-first</span> <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">PoshCode</span></span><br />
<br />
<span style="color: #666666; font-style: italic;"># Make a subdirectory for your LANGUAGE</span><br />
<span style="color: #660033;">mkdir</span> en <span style="color: #66cc66;">|</span> <span style="color: #660033;">cd</span> <br />
<span style="color: #666666; font-style: italic;"># And create a PowerShell Data file with my cert path ...</span><br />
<span style="color: #666666; font-style: italic;"># I loaded my certificate into my user store, but you can use a pfx file </span><br />
<span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">item</span></span> Authenticode.<span style="color: #003366;">psd1</span> <span style="color: #000066;">-type</span> file <span style="color: #000066;">-value</span> <span style="color: #009900;">'&quot;Cert:\CurrentUser\My\F05F583BB5EA4C90E3B9BF1BDD0B657701245BD5&quot;'</span><br />
<br />
<span style="color: #666666; font-style: italic;"># Load the module...</span><br />
<span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">Module</span></span> Authenticode</div>

	<p>Now you&#8217;ll be able to sign without specifying the certificate each time, and you&#8217;ll be able to pipe files in too, so you could, for instance, sign all the script files in a whole folder tree like: </p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #660033;">ls</span> <span style="color: #660033; font-weight: bold;">$ProfileDir</span>\AutoModules\<span style="color: #66cc66;">*</span> <span style="color: #000066;">-recurse</span> <span style="color: #000066;">-include</span> <span style="color: #66cc66;">*</span>.<span style="color: #003366;">ps1</span>,<span style="color: #66cc66;">*</span>.<span style="color: #003366;">ps1xml</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">AuthenticodeSignature</span></span></div>

	<h3>So, here&#8217;s what I do.</h3>

	<p>First of all, I don&#8217;t <em>actually</em> put .ps1 scripts into the AutoModules folder, I have a separate AutoScripts folder, and I dot-source scripts from there, as well as loading ps1xml format files from it.  I also specifically use a series of scripts there: Aliases.ps1, Variables.ps1, and EyeCandy.ps1.  I assume that the functions of the first two are obvious, the last one is a modified version of the <span class="caps">PSCX</span> prompt and <acronym title="Message of the Day">MOTD</acronym> script to modify my prompt and startup message.</p>

	<p>On top of the PoshCode script module and the Authenticode script module I mentioned earlier, which I use for signing and resigning, and the new CTP2 version of the <a href="http://thepowershellguy.com/blogs/posh/pages/powertab-v2-alpha-1.aspx">PowerTab script module</a>, I also use a source build of the upcoming <span class="caps">PSCX</span> 1.2 release (which needs a bunch of scripts to be useful, so I just renamed their 3-line profile.ps1 to <span class="caps">PSCX</span>.psm1 and stuffed the whole build with all it&#8217;s sub-folders into my WindowsPowerShell\AutoModules\<span class="caps">PSCX</span>).  I also preload my PoshHttp module for downloading files from the web, and I a couple of functions (<a href="http://poshcode.org/477">ellipsis</a> and <a href="http://poshcode.org/424">Get-PerformanceHistory</a>).</p>

	<p>Almost everything else I use I write into scripts (I even use the in-line cmdlet syntax to get full-power parameter parsing in a script file) which I put in directories by category inside my main WindowsPowerShell\Scripts directory (in my profile directory, and add to my path so that I can just run a script named &#8220;Get-<span class="caps">GUID</span>.ps1&#8221; by typing <code>Get-GUID</code> on the command-line.</p>

	<p>Here&#8217;s the profile script off my laptop as an example &#8212; I basically use the same profile on all my PCs, and make changes to the &#8220;Variables&#8221; scripts on each PC to differentiate them.  In fact, I have a sub-directory of my &#8220;WindowsPowerShell\Scripts&#8221; called &#8220;Work&#8221; which I even keep around at home (although I practically never use those scripts at home &#8212; they&#8217;re mostly for resetting servers and doing db queries, and only work from home when my <span class="caps">VPN</span> connection is up).  Let me know if you have any questions.  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';)' class='wp-smiley' /> </p>

	<p>Incidentally, I use &#8220;RemoteSigned for my Execution Policy &#8212; <span class="em1b">I wish there were a way to <strong>require</strong> signing for just my profile</span>, because then this system would basically protect me from tampering with my auto-run scripts.  However, I&#8217;m actually comfortable not worrying about it any more than I worry about viruses replacing the executable for Notepad++ on my thumbdrive or something &#8212; I <strong>do</strong> realize that this profile script could be simplified a lot if I replaced the signature checking with just using the &#8220;AllSigned&#8221; policy &#8230; but there&#8217;s two <strong>major</strong> problems with that:</p>

	<ol>
		<li>I&#8217;m a PowerShell-ed developer &#8212; I spend a lot of time editing scripts and running them, and re-signing scripts that I&#8217;m working on (even though it&#8217;s just a matter of typing &#8220;sign&#8221; before I run a script) becomes an annoyance, such that if I were working in that environment, I&#8217;d inevitably have a &#8220;Sign-and-Run&#8221; script, which would really make me less secure, rather than more &#8212; for the average <em>user</em>, I <strong>do</strong> think that if you&#8217;re not writing scripts yourself on a daily basis, you should consider using <strong>AllSigned</strong>.</li>
	</ol>
	<ol>
		<li><span class="em2b">PowerShell 2 CTP2 doesn&#8217;t check signatures on modules</span>, so even if I have it set to all-signed, it will load script modules and will <span class="em2">let you dot-source <strong>any</strong> script</span> using the add-module command, so I&#8217;d have to avoid modules altogether to really be 100% secure.</li>
	</ol>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666666; font-style: italic;"># Set the profile directory first, so we can refer to it for the rest of the script...</span><br />
<span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">Variable</span></span> ProfileDir &nbsp;<span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Split-<span style="font-style: normal;">Path</span></span> <span style="color: #660033; font-weight: bold;">$MyInvocation</span>.<span style="color: #003366;">MyCommand</span>.<span style="color: #003366;">Path</span> <span style="color: #000066;">-Parent</span><span style="color: #333;">&#41;</span> <span style="color: #000066;">-Scope</span> Global <span style="color: #000066;">-Option</span> AllScope<br />
<br />
<span style="color: #660033;">cd</span> <span style="color: #660033; font-weight: bold;">$ProfileDir</span><br />
<span style="color: #666666; font-style: italic;"># I have a JOIN function I need for massaging path variables ...</span><br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #333399; font-weight: bold; font-style: italic;">join</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">param</span> &nbsp; &nbsp;<span style="color: #333;">&#40;</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$sep</span>, <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$append</span>, <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$prepend</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">begin</span> &nbsp; &nbsp;<span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$ofs</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$sep</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;">$items</span> <span style="color: #66cc66;">=</span> &nbsp;@<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$prepend</span>.<span style="color: #333399; font-weight: bold; font-style: italic;">split</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$sep</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">process</span> &nbsp;<span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$items</span> <span style="color: #66cc66;">+=</span> <span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">end</span> &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$items</span> <span style="color: #66cc66;">+=</span> @<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$append</span>.<span style="color: #333399; font-weight: bold; font-style: italic;">split</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$sep</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span>; <span style="color: #666699; font-weight: bold;">return</span> <span style="color: #009900;">&quot;$($items -ne '')&quot;</span> <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## If we're on version two, we HAVE to set the PsPackagePath (it doesn't matter on v1)</span><br />
<span style="color: #660033; font-weight: bold;">$ENV</span>:PSPACKAGEPATH <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;$ProfileDir\AutoModules;$ProfileDir\Modules&quot;</span> <span style="color: #666666; font-style: italic;">#| join &quot;;&quot; -append $ENV:PSPACKAGEPATH</span><br />
<span style="color: #666666; font-style: italic;">## We also want to add our scripts directory to the path</span><br />
<span style="color: #660033; font-weight: bold;">$ENV</span>:PATH <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">ChildItem</span></span> <span style="color: #660033; font-weight: bold;">$ProfileDir</span>\Script<span style="color: #333;">&#91;</span>s<span style="color: #333;">&#93;</span>,<span style="color: #660033; font-weight: bold;">$ProfileDir</span>\Scripts\<span style="color: #66cc66;">*</span> &nbsp;<span style="color: #66cc66;">|</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: #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;">FullName</span> <span style="color: #333;">&#125;</span> <span style="color: #66cc66;">|</span> <span style="color: #333399; font-weight: bold; font-style: italic;">Join</span> <span style="color: #009900;">&quot;;&quot;</span> <span style="color: #000066;">-append</span> <span style="color: #660033; font-weight: bold;">$ENV</span>:PATH<br />
<br />
<span style="color: #666666; font-style: italic;">## Now, preload any module in AutoModules that is signed ...</span><br />
<span style="color: #666666; font-style: italic;">###################################################################################################</span><br />
<span style="color: #666666; font-style: italic;">##### &nbsp;NOTE: &nbsp; There's a bug which prevents signing or checking psm1 files</span><br />
<span style="color: #666666; font-style: italic;">##### &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;We work around that bug by using this module called &quot;Authenticode&quot; which </span><br />
<span style="color: #666666; font-style: italic;">##### &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Wraps the Get/Set cmdlets in scripts that check psm1 by renaming them to ps1</span><br />
<span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">Module</span></span> Authenticode<br />
<span style="color: #666666; font-style: italic;">##### &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Now we can check signatures on .psm1 files ...</span><br />
<span style="color: #666666; font-style: italic;">##### &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;As long as they're in locations where we can rename them without problems</span><br />
<span style="color: #666666; font-style: italic;">## One last exception case:</span><br />
<span style="color: #666666; font-style: italic;">## If PSCX is available, load that FIRST, because others may depend on it.</span><br />
<span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">Module</span></span> PSCX <span style="color: #000066;">-EA</span> <span style="color: #009900;">&quot;SilentlyContinue&quot;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## Now automatically Add-Module the module subfolders ....</span><br />
<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;AutoLoad Modules: &quot;</span> <span style="color: #000066;">-Fore</span> Cyan <span style="color: #000066;">-NoNewLine</span><br />
<span style="color: #660033; font-weight: bold;">$AutoModuleErrors</span> <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
<span style="color: #666699; font-weight: bold;">ForEach</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$module</span> <span style="color: #666699; font-weight: bold;">in</span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">ChildItem</span></span> <span style="color: #660033; font-weight: bold;">$ProfileDir</span>\AutoModules <span style="color: #66cc66;">|</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: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># Write-Host &quot;Test-Module $($module.Name) &quot; -fore Yellow</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">switch</span> <span style="color: #333;">&#40;</span><span style="color: #660033;">ls</span> <span style="color: #009900;">&quot;$($module.FullName)\*&quot;</span> <span style="color: #000066;">-include</span> <span style="color: #009900;">&quot;$($module.Name).psd1&quot;</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&quot;$($module.Name).ps1&quot;</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&quot;$($module.Name).psm1&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&quot;$($module.Name).dll&quot;</span> <span style="color: #66cc66;">|</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033;">sort</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">switch</span><span style="color: #333;">&#40;</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>IO.<span style="color: #003366;">Path</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">GetExtension</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Name</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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&quot;.psd1&quot;</span> <span style="color: #333;">&#123;</span> <span style="color: #cc66cc;">0</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&quot;.ps1&quot;</span> &nbsp;<span style="color: #333;">&#123;</span> <span style="color: #cc66cc;">1</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&quot;.psm1&quot;</span> <span style="color: #333;">&#123;</span> <span style="color: #cc66cc;">2</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&quot;.dll&quot;</span> &nbsp;<span style="color: #333;">&#123;</span> <span style="color: #cc66cc;">3</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">Select</span> <span style="color: #000066;">-First</span> <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">|</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">AuthenticodeSignature</span></span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">## If they're signed and valid, Add-Module</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#123;</span> &nbsp;<span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">Signature</span></span> <span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #333;">&#125;</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">Module</span></span> <span style="color: #660033; font-weight: bold;">$module</span>.<span style="color: #003366;">Name</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;$($module.Name) &quot;</span> <span style="color: #000066;">-fore</span> Cyan <span style="color: #000066;">-NoNewLine</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">continue</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">## Otherwise, write an error, we don't want this to fail silently</span><br />
&nbsp; &nbsp; &nbsp; default <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Global</span>:AutoModuleErrors <span style="color: #66cc66;">+=</span> @<span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;&quot;</span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">Select</span> @<span style="color: #333;">&#123;</span>n<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;Name&quot;</span>;e<span style="color: #66cc66;">=</span><span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$module</span>.<span style="color: #003366;">Name</span><span style="color: #333;">&#125;</span><span style="color: #333;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@<span style="color: #333;">&#123;</span>n<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;Path&quot;</span>;e<span style="color: #66cc66;">=</span><span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Path</span><span style="color: #333;">&#125;</span><span style="color: #333;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@<span style="color: #333;">&#123;</span>n<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;Error&quot;</span>;e<span style="color: #66cc66;">=</span><span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Status</span><span style="color: #333;">&#125;</span><span style="color: #333;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@<span style="color: #333;">&#123;</span>n<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;StatusMessage&quot;</span>;e<span style="color: #66cc66;">=</span><span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">StatusMessage</span><span style="color: #333;">&#125;</span><span style="color: #333;">&#125;</span><span style="color: #333;">&#41;</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;$($module.Name) &quot;</span> <span style="color: #000066;">-fore</span> Red <span style="color: #000066;">-NoNewLine</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 />
<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span><br />
<br />
<span style="color: #666666; font-style: italic;">## And then, preload any script in AutoScripts that is signed ...</span><br />
<span style="color: #666666; font-style: italic;">###################################################################################################</span><br />
<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;AutoLoad Scripts: &quot;</span> <span style="color: #000066;">-Fore</span> Green <span style="color: #000066;">-NoNewLine</span><br />
<span style="color: #666699; font-weight: bold;">switch</span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">ChildItem</span></span> <span style="color: #660033; font-weight: bold;">$ProfileDir</span>\AutoScripts\<span style="color: #66cc66;">*</span>.<span style="color: #660033;">ps</span><span style="color: #66cc66;">*</span> <span style="color: #000066;">-include</span> <span style="color: #66cc66;">*</span>.<span style="color: #003366;">ps1</span>,<span style="color: #66cc66;">*</span>.<span style="color: #003366;">ps1xml</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">AuthenticodeSignature</span></span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## If they're signed and valid, load them based on type</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#123;</span> <span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">Signature</span></span> <span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #333;">&#125;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Path</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Path</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">switch</span> <span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>IO.<span style="color: #003366;">Path</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">GetExtension</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Path</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: #009900;">&quot;.ps1&quot;</span> &nbsp; &nbsp;<span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">Module</span></span> <span style="color: #660033; font-weight: bold;">$Path</span> <br />
&nbsp; &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;$([IO.Path]::GetFileNameWithoutExtension($Path)) &quot;</span> <span style="color: #000066;">-Fore</span> Green <span style="color: #000066;">-NoNewLine</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&quot;.ps1xml&quot;</span> <span style="color: #333;">&#123;</span> <span style="color: #0066cc; font-style: italic;">Update-<span style="font-style: normal;">TypeData</span></span> <span style="color: #000066;">-PrependPath</span> <span style="color: #660033; font-weight: bold;">$Path</span> <span style="color: #333;">&#125;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">continue</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## Otherwise, write an error, we don't want this to fail silently</span><br />
&nbsp; &nbsp;default <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Name</span> <span style="color: #66cc66;">=</span> $<span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>IO.<span style="color: #003366;">Path</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">GetFileNameWithoutExtension</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Path</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Global</span>:AutoModuleErrors <span style="color: #66cc66;">+=</span> @<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">Select</span> @<span style="color: #333;">&#123;</span>n<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;Name&quot;</span>;e<span style="color: #66cc66;">=</span><span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$Name</span><span style="color: #333;">&#125;</span><span style="color: #333;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @<span style="color: #333;">&#123;</span>n<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;Error&quot;</span>;e<span style="color: #66cc66;">=</span><span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Status</span><span style="color: #333;">&#125;</span><span style="color: #333;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @<span style="color: #333;">&#123;</span>n<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;StatusMessage&quot;</span>;e<span style="color: #66cc66;">=</span><span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">StatusMessage</span><span style="color: #333;">&#125;</span><span style="color: #333;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @<span style="color: #333;">&#123;</span>n<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;Path&quot;</span>;e<span style="color: #66cc66;">=</span><span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Path</span><span style="color: #333;">&#125;</span><span style="color: #333;">&#125;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;$Name &quot;</span> <span style="color: #000066;">-fore</span> Red <span style="color: #000066;">-NoNewLine</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span><br />
<span style="color: #666666; font-style: italic;"># Write out the error messages if we missed loading any modules</span><br />
<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$AutoModuleErrors</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$AutoModuleErrors</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Format-<span style="font-style: normal;">Table</span></span> <span style="color: #66cc66;">*</span> <span style="color: #000066;">-auto</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Out-<span style="font-style: normal;">Host</span></span><br />
<span style="color: #333;">&#125;</span></div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/getting-started-with-powershell-2-part-3/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Getting Started with PowerShell 2 &#8211; Part 2</title>
		<link>http://huddledmasses.org/getting-started-with-powershell-2-part-2/</link>
		<comments>http://huddledmasses.org/getting-started-with-powershell-2-part-2/#comments</comments>
		<pubDate>Mon, 30 Jun 2008 04:50:05 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[CodeSigning]]></category>
		<category><![CDATA[GettingStarted]]></category>
		<category><![CDATA[Modules]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[WalkThrough]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/?p=559</guid>
		<description><![CDATA[This continues a short series of posts about getting started with PowerShell &#8230; with a few tips about things you can do to keep your PowerShell profile safe and organized. Your &#8220;profile&#8221; is the script that is automatically loaded when you start up PowerShell. Really, I should say that your profile is the set of [...]]]></description>
			<content:encoded><![CDATA[	<p>This continues a short series of posts about getting started with PowerShell &#8230; with a few tips about things you can do to keep your <a href="http://www.microsoft.com/technet/scriptcenter/topics/winpsh/manual/profile.mspx">PowerShell profile</a> safe and organized.  Your &#8220;profile&#8221; is the script that is automatically loaded when you start up PowerShell.  Really, I <em>should</em> say that your profile is the <em>set</em> of scripts which are loaded <em>by default</em> when you start up PowerShell.  &#8220;By default&#8221; because you can always skip loading them by passing the -NoProfile switch to PowerShell.exe, and a set because PowerShell does, in fact, attempt to load <em>at least</em> four scripts when you run it:</p>

	<p>PowerShell loads &#8220;machine&#8221; profile scripts (which are located in the PowerShell folder) and &#8220;user&#8221; profile scripts (located in your Documents\WindowsPowerShell folder). But there&#8217;s a little more to it than that: PowerShell is a scripting engine which can be hosted inside any app, PowerShell.exe is a DOS-style console which is the default host.  There are several third-party hosts available such as <a href="http://PowerShell.com/">PowerShell Plus</a> and <a href="http://PowerGUI.org/">PowerGUI</a> and several open source hosts such as <a href="http://CodePlex.com/BgShell/">BgShell</a> and <a href="http://CodePlex.com/PoshConsole/">PoshConsole</a> &#8230; in order to support this ecosystem of hosts, the default PowerShell behavior is to load a host-specific profile script (for both the machine settings and the local-user settings).  Not all hosts will do that, but anyway &#8230; the default host loads <code>Microsoft.PowerShell_profile.ps1</code> and <code>Profile.ps1</code> from both the user and machine locations. </p>

	<p>By default, none of those profiles actually exist. Once you&#8217;ve <a href="/getting-started-with-powershell-2-part-1">installed everything as in Part 1</a>, you should have a Profile.ps1 file provided by <acronym title="PowerShell Community Extensions">PSCX</acronym>. This profile defines a whole bunch of values that are used by various <span class="caps">PSCX</span> cmdlets and scripts, so you may want to change some of it, but you should be careful about just deleting things until you&#8217;re well acquainted with the <span class="caps">PSCX</span> cmdlets.  Over time, I&#8217;ve added settings to my profile for other snapins as well, and there gets to be a lot of noise in there that&#8217;s specific to different snapins, so instead of just leaving all of that in my main profile, I rename the Profile.ps1 file provided by <span class="caps">PSCX</span> and then dot-source it from a new blank profile script.</p>

	<p>In fact, and I found that I started collecting a lot of scripts in my WindowsPowerShell folder so I created a sub-folder for them, and I automatically load everything that&#8217;s in that folder, so I don&#8217;t have to manually dot-source things when I add a new snapin profile. </p>

	<h3>Authenticode Signing your auto-load scripts</h3>

	<p>In order to make sure that automatically loading scripts doesn&#8217;t become a way for people to attack my computer, I made a decision awhile ago that I would only auto-load signed scripts. The how and why of this is a bit much to get into, and I wrote about <a href="http://huddledmasses.org/code-signing-with-openssl-and-powershell/">Generating Windows Authenticode Code-Signing Certificates with OpenSSL</a> a while back, so you can read that if you want more details, I want to review the simplest steps.<span id="more-559"></span></p>

	<p> <img src='http://huddledmasses.org/wordpress/wp-includes/' alt='[new]' class='wp-smiley' />  <strong>Edit</strong>: I should take a moment here to point out, in case you&#8217;re really new to PowerShell, that the first thing you&#8217;ll have to do is fix the error &#8220;File &#8230; cannot be loaded because the execution of scripts is disabled on this system.&#8221;  I originally was just assuming you would have figured that out &#8212; it tells you to read the about_signing help, so you can just execute <code>get-help about_signing</code> and it explains in there what you need to do.  In case you haven&#8217;t gotten that far, before you can execute the New-CodeSigningCert you&#8217;re going to need to run PowerShell as administrator (&#8220;elevated&#8221;) and execute this command: <code>Set-ExecutionPolicy RemoteSigned</code> &#8230; that will let you execute most scripts that you have saved locally without having signed them.  You may want to tighten that up later and set it to &#8220;AllSigned,&#8221; but first you have to be able to sign scripts.</p>

	<h4>Generating a code-signing certificate</h4>

	<p> <img src='http://huddledmasses.org/wordpress/wp-includes/' alt='[new]' class='wp-smiley' />  <strong>Edit</strong>: I&#8217;ve simplified this a bit, and modified the script to import the certificate to the <em>Current User</em> certificate store if you aren&#8217;t running in an elevated console (in Vista) &#8212; which should work fine if you&#8217;re the only user on your machine that needs to run the scripts &#8212; if you need them in the <em>Local Machine</em> store, you need to be running as administrator.  The script will let you know which it does, so you&#8217;ll know, one way or another.</p>

	<p><strong>Step 1.</strong> Download my <a href="http://huddledmasses.org/?attachment_id=554">PoshCerts package</a> and unpack it in your WindowsPowerShell folder &#8212; lets say in, WindowsPowerShell\PoshCerts\bin.</p>

	<p><strong>Step 2.</strong> Run the New-CodeSigningCert script to generate into the Certs folder. So if you&#8217;re in the WindowsPowerShell directory, you can just run it and you&#8217;ll be prompted for a few pieces of information that the certificates require, plus two passwords &#8212; and all of the keys will be generated into the Certs folder as files, and imported to your local store.  Make sure you make a note of those passwords.</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #660033;">cd</span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Split-<span style="font-style: normal;">Path</span></span> <span style="color: #660033; font-weight: bold;">$Profile</span><span style="color: #333;">&#41;</span>\PoshCerts\<br />
.\bin\<span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">CodeSigningCert</span></span>.<span style="color: #003366;">ps1</span> <span style="color: #660033; font-weight: bold;">$pwd</span> <span style="color: #009900;">&quot;[Your Name]&quot;</span> <span style="color: #009900;">&quot;[Your Email]&quot;</span> <span style="color: #000066;">-ImportAll</span></div>

	<p> <img src='http://huddledmasses.org/wordpress/wp-includes/' alt='[new]' class='wp-smiley' />  <strong>Edit</strong>: I came up with a <strong>Step 3.</strong> Under most circumstances (that is, unless you were planning on running a corporate Certificate Authority), you&#8217;ll have no further use for the CA certificate, If you want to make sure that nobody can abuse your root CA &#8230; you can and should just delete the private certificates, like so: <code>Remove-Item *Root-CA.* -exclude *.crt</code></p>

	<h4>Signing Scripts</h4>

	<p>You can now sign scripts at will using the <strong>pfx</strong> file generated in step 2 with the Get-PfxCertificate cmdlet, or the Cert: provider (you have use -ImportAll when you call New-CodeSigningCert, not just -Import &#8212; that causes the import of your code-signing certificate to the &#8220;My&#8221; store &#8230; then you can do <code>Get-ChildItem &#34;Cert:\CurrentUser\My\$thumbprint&#34;</code> where $thumbprint is your certificate&#8217;s thumbprint).  </p>

	<p>Let&#8217;s rename our Profile.ps1 script and then sign it.  We&#8217;ll create create a folder called &#8220;AutoModules&#8221; to stick your <span class="caps">PSCX</span> profile (and any future such scripts) in. If you&#8217;re still on PowerShell 1, you&#8217;ll just put the .ps1 files into that folder, and we&#8217;ll dot-source them. </p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #660033;">cd</span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Split-<span style="font-style: normal;">Path</span></span> <span style="color: #660033; font-weight: bold;">$Profile</span><span style="color: #333;">&#41;</span><br />
<span style="color: #660033;">mkdir</span> AutoModules<br />
<span style="color: #666666; font-style: italic;"># Sign it (you'll reuse this exact code in a moment)</span><br />
<span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">AuthenticodeSignature</span></span> <span style="color: #000066;">-Cert</span> <span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># You'll be prompted for your code-signing cert password here:</span><br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">PfxCertificate</span></span> .\PoshCerts\<span style="color: #66cc66;">*</span>Code<span style="color: #66cc66;">-</span>Signing.<span style="color: #003366;">pfx</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#41;</span> .\Profile.<span style="color: #003366;">ps1</span><br />
<span style="color: #666666; font-style: italic;"># Now move it (the signature doesn't care about the file name).</span><br />
<span style="color: #660033;">mv</span> .\Profile.<span style="color: #003366;">ps1</span> .\AutoModules\PSCX.<span style="color: #003366;">ps1</span><br />
&nbsp;</div>

	<p> <img src='http://huddledmasses.org/wordpress/wp-includes/' alt='[new]' class='wp-smiley' />  <strong>Edit</strong>: The simplest</p>

	<h4>Checking for valid signatures</h4>

	<p>Once you&#8217;ve got that set up, we need to create the new profile script and modify it to load not just the <span class="caps">PSCX</span> module, but any other <strong>signed</strong> scripts we put in there.  Paste this into your new Profile.ps1, and don&#8217;t forget to sign it.</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #660033; font-weight: bold;">$ProfileDir</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Split-<span style="font-style: normal;">Path</span></span> <span style="color: #660033; font-weight: bold;">$MyInvocation</span>.<span style="color: #003366;">MyCommand</span>.<span style="color: #003366;">Path</span><br />
<br />
<span style="color: #666666; font-style: italic;">## If we're on version two, we need to set the PsPackagePath (it doesn't hurt anything anyway)</span><br />
<span style="color: #660033; font-weight: bold;">$ENV</span>:PSPACKAGEPATH <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;$ProfileDir\AutoModules&quot;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## Now, preload any script in AutoModules that is signed ...</span><br />
<span style="color: #666666; font-style: italic;">##### Problem 1: &nbsp;There's a bug which prevents signing or checking psm1 files</span><br />
<span style="color: #666666; font-style: italic;">##### I'll show you how to work around that in Part 3</span><br />
<span style="color: #666666; font-style: italic;">##### For now you don't have any, so this is quite simple:</span><br />
<span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">ChildItem</span></span> <span style="color: #660033; font-weight: bold;">$ProfileDir</span>\AutoModules\<span style="color: #66cc66;">*</span> <span style="color: #000066;">-include</span> <span style="color: #66cc66;">*</span>.<span style="color: #003366;">ps1</span> <span style="color: #000066;">-recurse</span> <span style="color: #66cc66;">|</span> <br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">AuthenticodeSignature</span></span> <span style="color: #66cc66;">|</span> <br />
&nbsp; &nbsp;Where<span style="color: #66cc66;">-</span>Object <span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Status</span> <span style="color: #000066;">-eq</span> <span style="color: #009900;">&quot;Valid&quot;</span><span style="color: #333;">&#125;</span> <span style="color: #66cc66;">|</span> <br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">ChildItem</span></span> <span style="color: #66cc66;">|</span> <span style="color: #66cc66;">&amp;</span><span style="color: #333;">&#123;</span> <span style="color: #666699; font-weight: bold;">PROCESS</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">switch</span> <span style="color: #000066;">-regex</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$_</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&quot;.*\.ps1$&quot;</span> &nbsp; &nbsp;<span style="color: #333;">&#123;</span> . <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">FullName</span>; <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Verbose</span></span> <span style="color: #009900;">&quot;Loaded $_&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&quot;.*\.ps1xml$&quot;</span> <span style="color: #333;">&#123;</span> <span style="color: #0066cc; font-style: italic;">Update-<span style="font-style: normal;">TypeData</span></span> <span style="color: #000066;">-PrependPath</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">FullName</span> &nbsp;<span style="color: #333;">&#125;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;default &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#123;</span> <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Verbose</span></span> <span style="color: #009900;">&quot;Not sure what to do with: $($_.Name)&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span> <span style="color: #333;">&#125;</span></div>

	<p><span class="em2b">If you&#8217;re testing PowerShell 2</span>, you&#8217;ll want to treat these scripts as <a href="/powershell-modules/">Modules</a> &#8230; SnapIns themselves can be loaded as modules in PowerShell 2, but so can any script which needs to be dot-sourced. In this case, the easiest thing is to create a subfolder for each one and then use Add-Module with the folder name. There are some problems with that, which I&#8217;ll go into in <a href="http://huddledmasses.org/getting-started-with-powershell-2-part-3/">Part 3</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/getting-started-with-powershell-2-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

