r/PowerShell • • Apr 15 '22

Question PowerShell parameter validate set and removing already used options

I have a function with several parameters defined. One of the parameters is a validate set consisting of 3 items. This works as expected and the validation only allows the entry of those 3 items.

However where im getting tripped up on is that it seems like validate set will allow me to specify the same item multiple times. for example with the validate set below.

[ValidateSet("Apple","Orange","Banana")]

When entering data into the function the user can input an entry from the validate set multiple times.

 order-breakfast -fruit apple, apple, apple, banana

Is there a way to limit the validate set so that you can only enter apple once. Basically if the entry from validate set is used, remove it from the list of available options.

30 Upvotes

27 comments sorted by

View all comments

2

u/Th3Sh4d0wKn0ws Apr 15 '22 edited Apr 15 '22

My first thought was to cast your parameter variable specifically as a single string, and therefore to not allow an array.

Function Order-Breakfast {
	Param (
		[ValidateSet("apple","orange","banana")]
		[String]$Fruit
	)

	$Fruit
}  

By using '[String]' you're staying that the variable $Fruit can only ever be a single thing. It will throw this error text if you try to pass it more than one value from that set:

Test : Cannot validate argument on parameter 'Fruit'. The argument "apple orange" does not belong to the set "apple,orange,banana" specified by the ValidateSet attribute. Supply an argument that is in the set and then try the command again.    
At line:1 char:13
+ test -fruit apple,orange
+             ~~~~~~~~~~~~
	+ CategoryInfo          : InvalidData: (:) [Test], ParameterBindingValidationException  

which admittedly isn't super clear. But if you change the object cast to '[String[]]' you're stating that it will accept and array of input, and it will then accept multiple values.

EDIT
another way to do this would be to use the '[ValidateCount()]' parameter attribute.

Function Order-Breakfast {
	Param (
		[ValidateSet("apple","orange","banana")]
		[ValidateCount(1,1)]
		[String[]]$Fruit
	)

	$Fruit
}  

Even though I'm saying that the $Fruit variable will accept and array of values i've specified the ValidateCount attribute to say that the minimum number of values is 1, and the max is 1. If I provide two values the error message is:

Order-Breakfast : Cannot validate argument on parameter 'Fruit'. The number of provided arguments, (2), exceeds the maximum number of allowed arguments (1). Provide fewer than 1 arguments, and then try the command again.
At line:1 char:24
+ order-breakfast -fruit apple,apple
+                        ~~~~~~~~~~~
	+ CategoryInfo          : InvalidData: (:) [Order-Breakfast], ParameterBindingValidationException
	+ FullyQualifiedErrorId : ParameterArgumentValidationError,Order-Breakfast  

This seems to accomplish the goal and the error message is a little more clear about what it is unhappy about.

2

u/xxdcmast Apr 15 '22

Thanks for the help. It seems there isnt really a clean way of doing this natively. Your idea comes close but you the request can be an array of multiple options, apple and banana is fine.

From the suggestions is thing the .tolower and select unique might be the best way forward. Thanks

2

u/Th3Sh4d0wKn0ws Apr 15 '22

oh man i completely skipped over the fact that we just don't want repeats. There is also the ValidateScript option. I'm on mobile or I'd whip something up.

1

u/neztach Apr 16 '22

When you aren’t on mobile, I’d be interested in the mock up. I’ve recently been researching how to about adding a validateset for a switch which apparently “isn’t possible” as a parameter, but surely I’m not inventing this wheel. I should probably also find a decent write up on how to make a parameter set where if you choose to use a given switch then it requires a string to follow it

1

u/Th3Sh4d0wKn0ws Apr 17 '22

i'm actually having a hard time making it work. ValidateScript utilizes the automatic variable $_ to represent the current object being fed to that parameter. When you feed multiple things to the parameter they still seem to get passed to the ValidateScript one at a time, so I'm not sure how to count them.