PoshCode Archive  Artifact [50669abd50]

Artifact 50669abd5050427c6b68f47c47a642e14459ee07e017344b15c5941cc8c5950d:

  • File Read-Choice.ps1 — part of check-in [df205d9266] at 2018-06-10 13:02:18 on branch trunk — Just a little wrapper for PromptForChoice (user: Joel Bennett size: 1478)

# encoding: ascii
# api: powershell
# title: Read-Choice
# description: Just a little wrapper for PromptForChoice
# version: 0.1
# type: function
# author: Joel Bennett
# license: CC0
# function: Read-Choice
# x-poshcode-id: 1871
# x-archived: 2015-11-28T15:17:32
# x-published: 2010-05-25T08:59:00
#
#
# function Read-Choice {
[CmdletBinding()]
param(
   [Parameter(Mandatory=$true, ValueFromRemainingArguments=$true)]
   [hashtable[]]$Choices
,
   [Parameter(Mandatory=$False)]
   [string]$Caption = "Please choose!"
,  
   [Parameter(Mandatory=$False)]
   [string]$Message = "Choose one of the following options:"
,  
   [Parameter(Mandatory=$False)]
   [int[]]$Default  = 0
,  
   [Switch]$MultipleChoice
,
   [Switch]$Passthru
)
begin {
   [System.Collections.DictionaryEntry[]]$choices = $choices | % { $_.GetEnumerator() }
}
process {
   $Descriptions = [System.Management.Automation.Host.ChoiceDescription[]]( $(
                     foreach($choice in $choices) {
                        New-Object System.Management.Automation.Host.ChoiceDescription $choice.Key,$choice.Value
                     } 
                   ) )

   if(!$MultipleChoice) { [int]$Default = $Default[0] }

   [int[]]$Answer = $Host.UI.PromptForChoice($Caption,$Message,$Descriptions,$Default)

   if($Passthru) {
      Write-Verbose "$Answer"
      Write-Output  $Descriptions[$Answer]
   } else {
      Write-Output $Answer
   }
}

# }