Couldn't use predefined array inside Validateset - Powershell

7

I'm looking for a way to make a cmdlet which receives parameter and while typing, it prompts suggestions for completion from a predefined array of options.

I was trying something like this:

$vf = @('Veg', 'Fruit')
function Test-ArgumentCompleter {
[CmdletBinding()]
 param (
        [Parameter(Mandatory=$true)]
        [ValidateSet($vf)]
        $Arg
)

}

The expected result should be: When writing 'Test-ArgumentCompleter F', after clicking the tub button the F autocompleted to Fruit. Thanks in advance for your help!

Share
Improve this question
3
  • [ValidateSet('Veg','Fruit')] – Theo May 2 at 13:22
  • @Theo Thank you for the reply, but its not the answer for my question. I 'm looking for answer which solves it with an array of values defined earlier in the code, so I can reuse it instead of writing 'Veg','Fruit' in every function I write because it requires much more maintenance in each value addition in the future. – barper May 2 at 13:45
  • 2
    That was to say you can't do what you want with ValidateSet. You can use ValidateScript where you test yourself if the input var is found in an array, but then you lose intellisense. – Theo May 2 at 13:50

Comments

Popular posts from this blog

Meaning of `{}` for return expression

Get current scroll position of ScrollView in React Native

flutter websocket connection issue