0

I am trying to setup dynamic parameters that vary depending on if you are adding or modifying/removing a drone. Ex: If you are adding a drone you would need its IP/Name/Location.. To remove the drone you would only need its name. I have tried looking online and try various examples I've seen but I am completely stuck here. Any help to steer me in the right direction would be appreciated. I am somewhat new to powershell. Here's what I have.

[CmdletBinding(SupportsShouldProcess=$True)]
Param(  [Parameter(Mandatory=$true,
        HelpMessage = "Add remove or Modify a drone?")]
        [ValidateSet("Add", "Remove", "Modify")]
        [String]$Action)


      DynamicParam{

        if ($action = "Add"){

            Param( [Parameter(Mandatory)]
            [ValidateSet("NorthAmerica", "SouthAmerica", "NorthernEurope","UK", "CEE", "FRMALU", "SouthernEurope", "AsiaPacific")]
            [String]$curRegion,
            [Parameter(Mandatory)]
            [IPAddress]$ip,
            [Parameter(Mandatory)]
            [String]$droneName)

        }
        if ($action = "Remove"){
            Param( 
                [Parameter(Mandatory)]
                [string]$droneRemoveName)
        }

    }

1 Answer 1

1

Consider driving your parameter constraints with named Parameter Sets instead. I'm suggesting this because dynamic parameters don't work quite like you think they do, but named parameter sets are an easier way to solve your problem. In case you're interested, here's a blog post explaining how to use dynamic parameters and it winds up being pretty manual parameter handling.

You can add a parameter to more than one parameter set depending on the contexts in which each parameter is required. Instead of using -Action ACTION as a driver for a dynamic parameter, use a [switch] instead, such as -Add and -Remove, and have each switch part of its own parameter set. For example, when defining your parameters, it may look something like this:

Param(
  [Parameter(ParameterSetName='Remove')]
  [switch]$Remove,
  [Parameter(ParameterSetName='Add')]
  [switch]$Add,
  [Parameter(ParameterSetName='Remove', Mandatory)]
  [Parameter(ParameterSetName='Add', Mandatory)]
  [string]$IPAddress
)

In this example, -IPAddress is valid when you use the -Add or -Remove switch, but won't be relavant outside of this context. Of course, if a parameter should only be valid for a certain parameter set, don't define it under more than one parameter set name.


If you want to make sure at least one "action" switch is defined before executing, you can check that one of those parameters was used when invoking the cmdlet by checking $PSBoundParameters:

('Add' -in $PSBoundParameters.Keys) -Or ('Remove' -in $PSBoundParameters.Keys)
Sign up to request clarification or add additional context in comments.

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.