Jonas Andersen
Jonas Andersen

Reputation: 489

Is it possible to combine a parameter list across functions?

I have a Powershell script with 2 functions that are very similar in parameters. Is it possible to combine the parameters (Computername, Port, OtherVariable) into some sort of set?

In this example, it's only 3 parameters but in reality it's 11 parameters. I'm thinking of doing this because the parameterlist is starting to become a bit long.

function Function1 
{
  Param(
    [Parameter()]
    [String]$Filename,
    [Parameter()]
    [String]$Computername,
    [Parameter()]
    [String]$Port,
    [Parameter()]
    [String]$OtherVariable
    )
  Process
  {

  }
}

function Function2
{
  Param(
    [Parameter()]
    [String]$Url,
    [Parameter()]
    [String]$Computername,
    [Parameter()]
    [String]$Port,
    [Parameter()]
    [String]$OtherVariable
    )
  Process
  {

  }
}

Upvotes: 0

Views: 78

Answers (1)

TechSpud
TechSpud

Reputation: 3518

You have a few options:

  1. Not declare your parameters. But you then lose any auto-help and this would make it difficult to maintain (unless you added comments, detailing the expected/accepted params - but this would still require maintenance).
  2. Move the code common to both functions into a 3rd function (not sure if this is possible in your use-case?)
  3. Create a super*-function, as woxxom mentioned in the comment to your post, with all the params plus Compile and Export [switch] params. Then either move the compile and export code into separate functions, or include in the super-function.

*super being superset

Upvotes: 1

Related Questions