Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there some way to define a parameterless powershell parameter set?

I've got some fairly complex functions that I'm writing for a library module, with lots of different ways it can be called. However, it is in fact possible to default all of them, but when I try to call my function with no parameters the call fails because the parameter set cannot be determined.

I would like to define a parameter set that contains no parameters whatsoever, such that calls with no parameters succeed. This is difficult to do since ParameterSetName is a property of the Parameter attribute though, and it's not possible to attribute nothing.

I experimented with the DefaultParameterSet property of the CmdletBinding attribute that is placed on the param block, however nothing seemed to work. It seems that the parameter set name defined there must actually exist in order for Powershell to default to it.

Currently my best approximation of this use case is to define one of the parameter sets to have no Mandatory parameters, however these fail when empty strings or nulls are piped in, and I would like for this not to be the case.

Is this possible?

like image 614
bwerks Avatar asked Dec 21 '17 21:12

bwerks


People also ask

How do you specify a parameter in PowerShell?

One way to use PowerShell function parameters in a script is via parameter name -- this method is called named parameters. When calling a script or function via named parameters, use the entire name of the parameter. For example, perhaps the example param block above is stored in a PowerShell script called foo. ps1.

How do I set a default parameter in PowerShell?

You can use a script block to specify different default values for a parameter under different conditions. PowerShell evaluates the script block and uses the result as the default parameter value. The Format-Table:AutoSize key sets that switch parameter to a default value of True.

How do I create a switch parameter in PowerShell?

To create a switch parameter in a function, specify the switch type in the parameter definition. Switch parameters are easy to use and are preferred over Boolean parameters, which have a less natural syntax for PowerShell. For example, to use a switch parameter, the user types the parameter in the command.

How do you pass parameters to a PowerShell script?

You can pass the parameters in the PowerShell function and to catch those parameters, you need to use the arguments. Generally, when you use variables outside the function, you really don't need to pass the argument because the variable is itself a Public and can be accessible inside the function.

What does “argument parametersetname” mean in PowerShell?

Argument ParameterSetName with Argument value “FileName” which means that this parameter belongs to the “ FileName ” Parameter Set. If you want to read the article dedicated to PowerShell Parameter Sets please click here.

How to create PowerShell parameter (s)?

Follow these steps in order to create the PowerShell Parameter (s): Use param () statement right after function definition to define all the input parameters within it. This step is done only once! Give a name to the parameter starting with $.

How to pass argument values when running PowerShell scripts?

Ones parameters are defined, we can pass argument values when running the PowerShell scripts by entering parameter name prefixed with an hyphen “-” and it’s value. For example, to pass a value to the Name parameter enter the value like -Name "Code Steps". Here is the example of the Script with a parameter defined.

What is the difference between one parameter and multiple parameters?

Only one parameter in a set can declare the ValueFromPipeline keyword with a value of true. Multiple parameters can define the ValueFromPipelineByPropertyName keyword with a value of true. If no parameter set is specified for a parameter, the parameter belongs to all parameter sets.


1 Answers

Sure is. Just specify a default parameter set name that isn't used otherwise:

function Foo {
    [CmdletBinding(DefaultParameterSetName='x')]
    Param(
        [Parameter(Mandatory=$true, ParameterSetName='y')]$a,
        [Parameter(Mandatory=$false, ParameterSetName='y')]$b,
        [Parameter(Mandatory=$true, ParameterSetName='z')]$c,
        [Parameter(Mandatory=$false)]$d
    )

    "`$a: $a"
    "`$b: $b"
    "`$c: $c"
    "`$d: $d"
}
like image 160
Ansgar Wiechers Avatar answered Nov 02 '22 14:11

Ansgar Wiechers