# Wednesday, October 21, 2009

You might not know it, but when you import a PowerShell module you can pass it one or more arguments by way of Import-Module's -ArgumentList (aliased to -Args) parameter. While it looks like passing parameters to a standard ps1 file, there are some limitations. Take this module for an example:

# -- begin foo.psm1 --
param()
 
. {
  param(
     [validateset("a","b","c")]
     $letter
 
     function Get-Letter {
        "You initialized the module with $letter"
     }
} @args
# -- end foo.psm1

ps> import-module foo -args b
ps> get-letter
"You initialized the module with b"

You might notice that I am dotting (dot-sourcing or dot-executing) the scriptblock. By doing this, you are ensuring that anything declared in the scriptblock is imported into the calling scope. You can also apply advanced-function style validation to the module initializer by splatting (@) the module arguments ($args). If the scriptblock was called instead (via & { ... } @args) then the nested scope created from the call (&) operator prevents the function Get-Letter from being exported from the module because it goes out of scope when the called scriptblock completes. So, why not put the param block at the top of the module where param() is now you might ask? Because that mechanism is broken and/or partially implemented in v2, and completely ignores [cmdletbinding()] directives and validators. Note also that there is no way to use switches in the traditional sense with module arguments; instead you can pass boolean literals like $true or $false which will be mapped positionally to any declared switches.

So where might you use this technique of module initializers? You could create a generalized custom module that works with your various development environments, or SQL clusters/servers. You pass the name of the environment (or server) to the module on import, and all of the functions exported then are "bound" to that cluster (or server) so you don't have to continually pass each function the cluster (or server) name as an argument.

Have fun!

posted on Wednesday, October 21, 2009 11:05:08 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] Trackback
Related posts:
PowerShell Script Provider
PowerShell ISE Hacking: Change default save encoding to ASCII
PowerShell 2.0 – PSCX Labs: Invoke-Reflector
PowerShell 2.0 – Developer Essentials #1 – Initializing a Runspace with a Module
PowerShell 2.0 – Partial Application of Functions and Cmdlets
PowerShell – The Patchwork of Paths, PSPaths and ProviderPaths

Referred by:
http://favit.com/ [Referral]
http://twitturls.com/ [Referral]
http://www.google.com/ig [Referral]
http://brizzly.com/ [Referral]
http://powertwitter.me/ [Referral]
http://www.google.com/reader/view/ [Referral]
http://twitter.com/ [Referral]
http://twitter.com/oising [Referral]
PowerShell (www.google.co.jp) [Referral]
app:/index.html [Referral]
http://www.google.com/ [Referral]
http://www.google.nl/ig?hl=nl&t=1 [Referral]
powershell 2.0 imoprt module ad (www.google.fi) [Referral]
powershell (www.google.com) [Referral]
powershell v2.0 module initialization (www.google.com.eg) [Referral]
powershell call function on module (www.google.com) [Referral]
powershell-2.0 modules (www.google.com) [Referral]
how to import powershell modules (www.google.co.nz) [Referral]
powershell import module (search.yahoo.com) [Referral]
powershell 2.0 (www.google.com) [Referral]
powershell module initialization (www.google.be) [Referral]
powershell params out of scope (www.google.dk) [Referral]
powershell 2.0 modules (www.google.com) [Referral]
powershel 2.0 import-module (www.google.ch) [Referral]
.net module initializer (www.google.com) [Referral]
powershell call module (www.google.com) [Referral]
"Enter the code shown (prevents robots)" (www.google.com) [Referral]
function powershell 2.0 (www.google.ca) [Referral]
powershell 2.0 callbacks with arguments param (www.google.de) [Referral]
powershell 2.0 import module cluster (www.google.it) [Referral]
powershell 2.0 import module (www.google.com) [Referral]
powershell 2.0 module (www.google.be) [Referral]
powershell 2.0 Modules -CTP (www.google.ch) [Referral]
http://by112w.bay112.mail.live.com/mail/TodayLight.aspx?layo... [Referral]
powershell modules initialization (www.google.cz) [Referral]
powershell function module (www.bing.com) [Referral]
http://ig.gmodules.com/gadgets/ifr?view=home&url=http://1o4.... [Referral]
import module powershell (www.bing.com) [Referral]
powershell 2.0 scriptblock (www.bing.com) [Referral]
powershell 2.0 sharepoint (search.yahoo.com) [Referral]
powershell 2.0 call operator (www.google.com) [Referral]
http://www.bloglines.com/myblogs_display?sub=24951782&site=1... [Referral]
cluster powershell module (www.google.nl) [Referral]
snapins for powershell 2.0 (www.ask.com) [Referral]
powershell modules (www.bing.com) [Referral]
powershell import-module argumentlist (www.google.com) [Referral]
powershell Import-Module @Args (www.google.com.au) [Referral]
powershell module initialization (www.google.com) [Referral]
powershell module initialization (www.bing.com) [Referral]
powershell 2.0 cluster import-module (www.google.com) [Referral]
powershell 2.0 passing parameters to modules (www.google.com) [Referral]
powershell import-module arguments (www.google.com) [Referral]
"powershell 2.0" develop module (www.bing.com) [Referral]
powershell module initialization (www.google.ru) [Referral]
http://www.ask.com/web?o=10588&l=dis&gct=dns&gc=1&q=ig.gmodu... [Referral]
powershell import cluster modules (www.bing.com) [Referral]
"import-module" +cluster (www.altavista.com) [Referral]
Powershell + Import-Module + ArgumentList (www.google.be) [Referral]
import powershell cluster module (www.google.com) [Referral]
powershell dns module (www.bing.com) [Referral]
powershell import-module argumentlist (www.bing.com) [Referral]
develop powershell 2.0 module (www.google.com) [Referral]
http://delicious.com/tag/powershell?page=2 [Referral]
powershell 2.0 modules (cn.bing.com) [Referral]
powershell import cluster module (www.google.de) [Referral]
import-module cluster (www.google.co.jp) [Referral]
powershell module scope (www.bing.com) [Referral]
module initializers (www.bing.com) [Referral]
DNS module for powershell (www.google.com) [Referral]
http://delicious.com/stejcz/powershell+tipy-triky?page=1 [Referral]
powershell modules ArgumentList (www.bing.com) [Referral]
scriptblock literals begin block (www.bing.com) [Referral]
how to create a powershell module (www.google.co.uk) [Referral]
+powershell +module +param (www.google.de) [Referral]
module initializer .net (www.google.com) [Referral]
powershell 2.0 DNS (www.google.hu) [Referral]
cluster module powershell (www.google.com) [Referral]
import cluster powershell (www.google.de) [Referral]
install cluster module powershell (www.google.se) [Referral]
powershell 2.0 dns (www.google.com) [Referral]
"@{}" powershell initializers (www.google.nl) [Referral]
pass parameters to modules in powershell (www.google.es) [Referral]
powershell pass parameters to modules (www.google.com) [Referral]
powershell import-module scope (www.google.com.au) [Referral]
powershell modules cluster (www.google.co.il) [Referral]
powershell pass parameters to modules (www.google.es) [Referral]
powershell cluster module (www.google.com) [Referral]
function module call powershell (www.google.com) [Referral]
"windows server 2008" cluster powershell module install (www.bing.com) [Referral]
powershell modules scope providers (www.google.co.uk) [Referral]
powershell module scope providers (www.google.co.uk) [Referral]
SQL site:nivot.org (www.google.com) [Referral]
.net module initializer (www.google.com.au) [Referral]
powershell validateset example (www.google.com) [Referral]
powershell modules initialization (www.google.ch) [Referral]
ig.ig.gmodu (www.google.it) [Referral]
powershell dot sourcing operator (search.yahoo.com) [Referral]
powershell cluster module (www.google.se) [Referral]
powershell params (www.bing.com) [Referral]
www.google.nl (search.bearshare.com) [Referral]
http://longurl.org/ [Referral]
.NET module initializer (www.bing.com) [Referral]
ig.gmodu (tw.search.yahoo.com) [Referral]
powershell validateset (www.google.com) [Referral]
Comments are closed.