4

以前に PowerShell で複数の選択を求められたことは覚えていませんが、このインターフェイスを実装しているホストの例をいくつか見てきました。残念ながら、これらは私が見た唯一のインターフェイスのリファレンスです。「正しく実装していることをテストする方法は次のとおりです」を見たことがありません。

4

2 に答える 2

4

私の最初の答えは無視してください。私が今見ることができるように、それはまったく答えではありません。そして、とても興味深い質問をありがとうございます。

そのインターフェイスを使用するコマンドレットはまだわかりません。ただし、スクリプトから独自に使用できます。上記の 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 になります。

できます。PowerShell ISE もこれをサポートしていますが、UI は GUI バージョンの方が優れているかもしれません。

于 2010-09-11T05:49:39.440 に答える
2

例: コマンドは、次の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 - PowerShell スタイル メニューを表示し、ユーザーの選択肢を取得します。

于 2010-09-08T06:45:30.203 に答える