0

So I'm trying to write a script that uses switch parameters and I also need arguments to go along with them. So for instance,

Function Foo ($X, $Z, [switch]$Y, $Yarg, [switch]$K, $Karg){
  if($Y){
    $Yup = $Yarg
  }

  if($K){
    $Y = $Karg
  }Else{$Y = 42}

  $X + $Z / $Yup

}

Essentialy I want to ask if the switch is there, then if it's there I want it to use the $Yarg variable, currently when I do that I get an error saying the switch can only be a boolean value. Then the rest of the code fails. Any ideas?

2 Answers 2

2

My assumption is that Y and K are mutually exclusive parameter sets, and X and Z are common to both, and mandatory. If neither -Y nor -K are specified, the default will be -Y. I'm using the alternate parameter declaration syntax here for clarity:

function Foo(
    [cmdletbinding(defaultparametersetname="setY")]
    param(
        [parameter(mandatory=$true)]
        $X,

        [parameter(mandatory=$true)]
        $Y

        [parameter(parametersetname="setY")]
        [switch]$Y,

        [parameter(parametersetname="setY", mandatory=$true)]
        $YArg,

        [parameter(parametersetname="setK")]
        [switch]$K,

        [parameter(parametersetname="setK", mandatory=$true)]
        $KArg,
    )
    # do work
)

I'm hoping the syntax should be obvious.

Sign up to request clarification or add additional context in comments.

3 Comments

This pushed me much further along, but now I'm stuck again.
Ok, so this worked pretty well, it just ended up being a matter of the parameter types I was using. I didn't really need to use Switches for the most part. It was more so using unmandatory variables. so instead of the above code I used this: function Foo( [cmdletbinding(defaultparametersetname="setY")] param( [parameter(mandatory=$true, position=0)] $X, [parameter(mandatory=$true, position=1)] $Y [parameter(mandatory=$false)] $Y, [parameter(mandatory=$false)] $K, ) # do work ) Thanks for the help!
ah, so you were confusing -parameterName:parameterValue with switches. These ain't switches; they're "named parameters." Switches don't have arguments (unless you explicitly provide a Boolean like -myswitch:$true or -myswitch:$false).
0

So what I am intending on doing is making a function where you enter variables like so

foo x z -y yarg -k karg

Primarily wanting the -y yarg type variable entry.

For me to be able to do this I thought I needed to use a switch, but really I just needed unmandatory parameters.

   function Foo(
    [cmdletbinding(defaultparametersetname="setY")]
    param(
        [parameter(mandatory=$true, position=0)]
        $X,

        [parameter(mandatory=$true, position=1)]
        $Z

        [parameter(mandatory=$false)]
        $Y,

        [parameter(mandatory=$false)]
        $K
    )
    # do work
)

That seemed easier, and it works!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.