Module advanced parameters

Maybe I should have titled this as “Advanced modules” (joke lol)…

I need to pass parameters to modules so first line of my module looks like this:

param (
[TypeName] $ParameterName
)
This works just fine, but I'm wondering if modules (*.psm1) files can accept parameters, then what would be the purpose of defining a full param block just like for advanced functions?
[CmdletBinding()]
param (
[Parameter()]
[TypeName] $ParameterName
)
Surely it looks very strange but not enough because I didn't define some parameter set names, some arguments such as Mandatory or HelpURI, and OutputType attribute etc. etc.

Advanced parameters are designed for functions and scripts but not for *.PSM modules AFAIK.

My question is, is there any benefit of module advanced parameters and to which extent? ex.

What would be considered too much and is there any known benefit when using full param block does make benefit?

Or maybe asking another way around makes more sense: Which portion of advanced parameter block features would make absolutely no sense or benefit for modules?

You can have parameters in a script module. This is commonly used to add the “Prefix” parameter to give distinctive noun names to imported commands or the “NoClobber” parameter to prevent overriding existing commands in the session. The Import-Module cmdlet has an -ArgumentList parameter that can be used to pass parameter values to the script module.

 

See: Get-Help Import-Module, Get-Help about_modules

 

 

NoClobber and Prefix make sense.

In other words Parameter and cmdletbinding attributes make no sense? (even though we can define them and it will work)

There might be a use case, but I can’t think of one.

@Mike R: This is a bit of a segway on the original post. I have a large script that accepts 7 parameters. In the main script, I load a module with all my functions. What I have been doing when calling those module functions that require the values from the main script parameters is to pass them as parameters to the functions. Would I be better off passing them as Module parameters? What would be the best practice?

Thanks for any guidance you can provide.

I would keep them in the functions. This would give you the most flexibility. Are you using splatting to pass the parameter values to your functions? If not, this could significantly reduce the syntax complexity by only modifying the hashtable for specific changes in parameter names/values. For example, consider the following script:

[cmdletbinding()]
param (
    $param1, $param2, $param3, $param4
)

function MyFunction ($param1, $param2, $param3, $param4)
{
    "You passed in $param1, $param2, $param3, $param4"
}

MyFunction @PSBoundParameters

Notice I used splatting on the function call and passed the hashtable PSBoundParameters which is an automatic hashtable created for bound parameters. This works because the parameters required for the function are the same as the script. If the were different I could create a different hashtable or copy the PSBoundParameters hashtable to modify it as needed.

Here’s what that script does when I call it.

PS C:\Users\micha> .\args.ps1 -param1 "Hello" -param2 "World" -param3 "foo" -param4 "Bar"
You passed in Hello, World, foo, Bar

 

Recommend review Get-Help about_splatting, Get-Help about_automatic_variables

 

 

Perfect, thank you sir :slight_smile: