代码之家  ›  专栏  ›  技术社区  ›  Mike Shepard

什么Cmdlet使用ihostuiSupportsMultipleChoiceSelection接口提示选择?

  •  4
  • Mike Shepard  · 技术社区  · 14 年前

    我不记得以前在PowerShell中曾被提示进行多次选择,但我看到过一些实现此接口的主机示例。不幸的是,这些是我看到的对接口的唯一引用。我从未见过“以下是如何测试您是否正确实现它的方法”。

    2 回复  |  直到 14 年前
        1
  •  4
  •   Roman Kuzmin    14 年前

    请不要理会我的第一个回答,我现在看这根本不是一个回答。感谢你提出了一个非常有趣的问题。

    我仍然不知道使用该接口的Cmdlet。但是我们可以从脚本中自己使用它。让我们修改上述get-choice.ps1并调用新的get-choice2.ps1:

    <#
    .SYNOPSIS
        Displays PowerShell style menu and gets user choices
    
    .DESCRIPTION
        *) Returns choice indexes.
        *) Choice keys are indicated by '&' in menu items.
        *) Help strings can be empty or nulls (items are used themselves).
    #>
    
    param
    (
        # Menu caption
        [string]$Caption = 'Confirm',
        # Menu message
        [string]$Message = 'Are you sure you want to continue?',
        # Choice info pairs: item1, help1, item2, help2, ...
        [string[]]$Choices = ('&Yes', 'Continue', '&No', 'Stop'),
        # Default choice indexes (i.e. selected on [Enter])
        [int[]]$DefaultChoice = @(0)
    )
    if ($args) { throw "Unknown parameters: $args" }
    if ($Choices.Count % 2) { throw "Choice count must be even." }
    
    $descriptions = @()
    for($i = 0; $i -lt $Choices.Count; $i += 2) {
        $c = [System.Management.Automation.Host.ChoiceDescription]$Choices[$i]
        $c.HelpMessage = $Choices[$i + 1]
        if (!$c.HelpMessage) {
            $c.HelpMessage = $Choices[$i].Replace('&', '')
        }
        $descriptions += $c
    }
    
    $Host.UI.PromptForChoice($Caption, $Message, [System.Management.Automation.Host.ChoiceDescription[]]$descriptions, $DefaultChoice)
    

    现在我们测试它:

    Get-Choice2 'Title' 'Message' -DefaultChoice 0, 1, 2 -Choices @(
        'Choice &1', 'This is choice 1'
        'Choice &2', ''
        'Choice &3', ''
        'Choice &4', ''
        'Choice &5', ''
        'Choice &6', ''
        'Choice &7', ''
        'Choice &8', ''
        'Choice &9', ''
        'Choice &0', ''
    )
    

    它打印10个选项,前3个选项突出显示(在控制台主机中),并提示:

    0> Test-Get-Choice2.ps1
    Title
    Message
    [1] Choice 1
    [2] Choice 2
    [3] Choice 3
    [4] Choice 4
    [5] Choice 5
    [6] Choice 6
    [7] Choice 7
    [8] Choice 8
    [9] Choice 9
    [0] Choice 0
    [?] Help
    (default choices are 1,2,3)
    Choice[0]:
    

    如果我们按 Enter 立即输出为默认的3个索引:0、1、2。例如,如果我们键入: 5 + Enter + 3 + Enter + 1 + Enter + Enter 然后输出是4,2,0。

    它起作用了。PowerShellISE也支持这一点,但在GUI版本中,UI可能更好一些。

        2
  •  2
  •   Roman Kuzmin    14 年前

    例如:命令 Remove-Item C:\TEMP\Test 提示您选择:

    Confirm
    The item at C:\TEMP\Test has children and the Recurse parameter was not specified. If you continue, all children will be removed with the item. Are you sure you want to continue?
    [Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"):
    

    或者,您可以使用此脚本(或其想法)构建自己的调用: Get-Choice.ps1 - Displays PowerShell style menu and gets a user choice