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

前端 未结 2 2005
囚心锁ツ
囚心锁ツ 2021-01-07 03:07

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

相关标签:
2条回答
  • 2021-01-07 03:51

    There's nothing you can do!

    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:

    1. A caption ([string])
    2. A message ([string])
    3. A set of descriptions ([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.

    0 讨论(0)
  • 2021-01-07 03:53

    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
    
    0 讨论(0)
提交回复
热议问题