powershell - How can I make Validate-set also to work for mandatory parameter input prompt? -


i use dynamic validate sets functions mandatory parameters.

when not provided powershell prompts , forces user input.

however in case, tab not respond , have type value.

is there way make dynamic validate set available on prompt?

there's nothing you can do!

the reason prompt presented host application not have necessary information available cycle through valid values.

to understand why, it's essential understand happens when invoke cmdlet without passing argument mandatory parameter. let's explore!

first, let's define simple sample function:

function test-validateset {   param(     [parameter(mandatory)]     [validateset('a','b','c')]     [string[]]$character   )    write-host "you entered: $character!" } 

if want see powershell does statement when hit enter, use trace-command.

ps c:\> trace-command parameterbinding {test-validateset} -pshost debug: parameterbinding information: 0 : bind named cmd line args [test-validateset] debug: parameterbinding information: 0 : bind positional cmd line args [test-validateset] debug: parameterbinding information: 0 : mandatory parameter check on cmdlet [test-validateset] debug: parameterbinding information: 0 :     prompting missing mandatory parameters using host cmdlet test-validateset @ command pipeline position 1 supply values following parameters: character[0]: 

these debug messages must have been emitted prompt being called. searching through powershell github repo "prompting missing mandatory parameters using host" lead to call site.

as may able decipher, prompt() method takes 3 things:

  1. a caption ([string])
  2. a message ([string])
  3. a set of descriptions ([system.management.automation.host.fielddescription[]])

looking @ properties of fielddescription object, you'll find correspond [parameter] attribute - no other kind of attribute - , that's why can't tab complete values based on validation attributes.


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -