Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell module - pass a parameter while importing module

In the below sample module file, is there a way to pass the myvar value while importing the module.

For example,

import-module -name .\test.psm1 -?? pass a parameter? e.g value of myvar

#test.psm1
$script:myvar = "hi"
function Show-MyVar {Write-Host $script:myvar}
function Set-MyVar ($Value) {$script:myvar = $Value}
#end test.psm1

(This snippet was copied from another question.)

like image 647
arvind pr Avatar asked Apr 27 '16 18:04

arvind pr


People also ask

What is param () in PowerShell?

Parameters can be created for scripts and functions and are always enclosed in a param block defined with the param keyword, followed by opening and closing parentheses. param() Inside of that param block contains one or more parameters defined by -- at their most basic -- a single variable as shown below.

How do I add a parameter in PowerShell?

To Add Parameters. Open the cmdlet Help file and locate the Command node for the cmdlet you are documenting. If you are adding a new cmdlet you will need to create a new Command node. Your Help file will contain a Command node for each cmdlet that you are providing Help content for.

How do I import a PowerShell module?

In PowerShell 2.0, you can import a newly-installed PowerShell module with a call to Import-Module cmdlet. In PowerShell 3.0, PowerShell is able to implicitly import a module when one of the functions or cmdlets in the module is called by a user.

What is psd1 and psm1?

. psm1 files contain main source code for a powershell module and . psd1 manifest data. You have to install them.


2 Answers

This worked for me:

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

You should use a param block 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.

Reference

like image 53
Kyle Stoflet Avatar answered Oct 01 '22 18:10

Kyle Stoflet


The -ArgumentList parameter of Import-Module unfortunately does not accept a [hashtable] or [psobject] or something. A list with fixed postitions is way too static for my liking so I prefer to use a single [hashtable]-argument which has to be "manually dispatched" like this:

param( [parameter(Mandatory=$false)][hashtable]$passedVariables )
# this module uses the following variables that need to be set and passed as [hashtable]:
#  BeQuiet, URL, LotsaMore...
$passedVariables.GetEnumerator() |
  ForEach-Object { Set-Variable -Name $_.Key -Value $_.Value }
...

The importing module or script does something like this:

...
# variables have been defined at this point
$variablesToPass = @{}
'BeQuiet,URL,LotsaMore' -split ',' |
    ForEach-Object { $variablesToPass[$_] = Get-Variable $_ -ValueOnly }
Import-Module TheModule -ArgumentList $variablesToPass

The above code uses the same names in both modules but you could of course easily map the variable names of the importing script arbitrarily to the names that are used in the imported module.

like image 40
TNT Avatar answered Oct 01 '22 18:10

TNT