Module advanced parameters

Welcome Forums General PowerShell Q&A Module advanced parameters

Viewing 6 reply threads
  • Author
    Posts
    • #284332
      Participant
      Topics: 6
      Replies: 14
      Points: 126
      Rank: Participant

      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?

      • This topic was modified 2 weeks, 1 day ago by metablaster.
    • #284341
      Participant
      Topics: 5
      Replies: 253
      Points: 1,007
      Helping Hand
      Rank: Community Hero

      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

       

       

    • #284347
      Participant
      Topics: 6
      Replies: 14
      Points: 126
      Rank: Participant

      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)

    • #284362
      Participant
      Topics: 5
      Replies: 253
      Points: 1,007
      Helping Hand
      Rank: Community Hero

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

    • #284371
      Participant
      Topics: 10
      Replies: 203
      Points: 996
      Helping Hand
      Rank: Major Contributor

      @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.

    • #284389
      Participant
      Topics: 5
      Replies: 253
      Points: 1,007
      Helping Hand
      Rank: Community Hero

      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:

      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.

       

      Recommend review Get-Help about_splatting, Get-Help about_automatic_variables

       

       

    • #284398
      Participant
      Topics: 10
      Replies: 203
      Points: 996
      Helping Hand
      Rank: Major Contributor

      Perfect, thank you sir 🙂

Viewing 6 reply threads
  • You must be logged in to reply to this topic.