January 6, 2014

Loading (import) a module with arguments

You can use the –ArgumentList parameter of the import-module cmdlet to pass arguments when loading a module.

You should use a param bloc in your module to define your parameters :

param(
    [parameter(Position=0,Mandatory=$false)][boolean]$BeQuiet=$true,
    [parameter(Position=1,Mandatory=$false)][string]$URL
)

Then call the import-module cmdlet like this :

import-module .\myModule.psm1 -ArgumentList $True,'http://www.microsoft.com'

As may have already noticed, you can only supply values (no names) to –ArgumentList. So you should define you parameters carefully with the position argument.

2 comments:

Rikard said...

I am trying this:
# ThisModule.psm1
param(
[parameter(Position=0,Mandatory=$false)][string]$ModuleSettingsAsJson
)

PS:\> Import-Module .\ThisModule -ArgumentList "{'computerName' : 'SERVER1'}"

...and then in the module a function...
$script:ModuleSettings = $null
function ParseSettings {
$script:ModuleSettings = $ModuleSettingsAsJson | ConvertFrom-Json
}

ParseSettings

ParadisJ said...

Hi Rikard,
What's your question exactly ?
Is it working ?

Post a Comment