I use dynamic validate sets for my functions for mandatory parameters.
When not provided Powershell prompts and forces user to input.
However in this case, T
The reason is that the prompt presented to you by the host application does not have the necessary information available to cycle through valid values.
To understand why, it's essential to understand exactly what happens when you invoke a cmdlet without passing an argument to a Mandatory
parameter. Let's explore!
First, let's define a simple sample function:
function Test-ValidateSet {
param(
[Parameter(Mandatory)]
[ValidateSet('a','b','c')]
[string[]]$Character
)
Write-Host "You entered: $Character!"
}
If you want to see what PowerShell does to a statement when you 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 for missing mandatory parameters using the host
cmdlet Test-ValidateSet at command pipeline position 1
Supply values for the following parameters:
Character[0]:
These DEBUG
messages must have been emitted from where the prompt is being called. Searching through the PowerShell
GitHub repo for "PROMPTING for missing mandatory parameters using the host" will lead you to this call site.
As you may be able to decipher, the Prompt() method takes just 3 things:
[string]
)[string]
)[System.Management.Automation.Host.FieldDescription[]]
)Looking at the properties of a FieldDescription object, you'll find that they roughly correspond to the [Parameter]
attribute - no other kind of attribute - and that's basically why you can't tab complete the values based on validation attributes.
You can only use tab on ValidateSet You can see same behavior if you use Show-Command The only pre run validation is ValidateSet
try this:
Function Test-Function
{
Param
(
[String]$NormalParameter,
[ValidateSet('T1','T2','T3')]
[String]$ValidateSetParameter,
[ValidatePattern("[T4]|[T5]|[T6]")]
[String]$ValidatePatternParameter,
[ValidateScript({$_ -In ('T7','T8','T9')})]
[String]$ValidateScriptParameter,
[ValidateRange('A','C')]
[String]$ValidateRangeParameter
)
}
Show-Command Test-Function