Multiple separate Validate sets in separate parameter set

I think that what I want to do might not be possible, but let’s see :slight_smile:
 
I would like to have two separate validate sets for one, depending on which parameter set is being used.
Example:
 

Param
	(
		[Parameter(Mandatory=$True,ParameterSetName='DC1')]
		[Parameter(Mandatory=$True,ParameterSetName='DC2')]
		[string]$VMName,
		
		[Parameter(Mandatory=$True,ParameterSetName='DC1')]
		[Parameter(Mandatory=$True,ParameterSetName='DC2')]
		[ValidateSet("DC1","DC2")]
		[string]$DataCentre,
		
		[Parameter(Mandatory=$True,ParameterSetName='DC1')]
		[ValidateSet("Any-DC1","DC1-Dev")]
		[Parameter(Mandatory=$True,ParameterSetName='DC2')]
		[ValidateSet("Any-DC2","DC2-Prd")]
		[string]$ServerCloud
	)

What happens here is that the correct validate set is tabbed through, but both validate sets are used, so both set options will be blocked by the other, for example “Any-DC1” will be blocked by the Validate Set in Parameter Set DC2, and vice versa…

Is what I am trying to do even possible? I.e. having two separate validate sets depending on the parameter set?

You can’t include an if or switch statement in a normal parameter set, but you can put one in a dynamic parameter set. Check out the last section on this page, it should let you use one validate set or the other depending on the value of the previous parameters.

The only issue with this is that PowerShell parameters can normally be entered in arbitrary order, and this will break that flexibility.

Groovy, thanks. I will try to play around with Dynamic Parameters.
Currently it errors out if I put anything after it and before the closing } in the example function and in the one I was writing. It looks promising though.

Another option for you to consider, is leveraging the …

Register-ArgumentCompleter

… cmdlet

As described in these resources…

Register-ArgumentCompleter

Description

The Register-ArgumentCompleter cmdlet registers a custom argument completer. An argument completer allows you to provide dynamic tab completion, at run time for any command that you specify.

Completing Parameter Values With Other Parameter Values

Creating your own custom “Argument Completer

That looks really interesting as well, thanks; I didn’t know about that cmdlet…

You could combine that with a dynamic ValidateSet or just a ValidateScript also; a few more options here: https://vexx32.github.io/2018/11/29/Dynamic-ValidateSet/