PoshCode Archive  Artifact [d2090e138e]

Artifact d2090e138e3430b46fe6e243e08696512cac00877135a683f2340ae92c4339ff:

  • File New-Choice.ps1 — part of check-in [8e36ee1f15] at 2018-06-10 13:13:52 on branch trunk — New-Choice Update (user: Andy Schneider size: 1586)

# encoding: ascii
# api: powershell
# title: New-Choice
# description: New-Choice Update
# version: 0.1
# type: function
# author: Andy Schneider
# license: CC0
# function: New-Choice
# x-poshcode-id: 2660
# x-archived: 2017-02-12T19:38:49
# x-published: 2012-05-06T11:21:00
#
#

function New-Choice {
<#
	.SYNOPSIS
		The New-Choice function is used to provide extended control to a script author who writing code 
        that will prompt a user for information.

	.PARAMETER  Choices
		An Array of Choices, ie Yes, No and Maybe

	.PARAMETER  Caption
		Caption to present to end user

	.EXAMPLE
		PS C:\> New-Choice -Choices 'Yes','No' -Caption "PowerShell makes choices easy"
		
	.NOTES
		Author: Andy Schneider
		Date: 5/6/2011
#>

[CmdletBinding()]
param(
		
	[Parameter(Position=0, Mandatory=$True, ValueFromPipeline=$True)]
	$Choices,
		
	[Parameter(Position=1)]
	$Caption,
    
	[Parameter(Position=2)]
	$Message    
	
)
	
process {
        
        $resulthash += @{}
        for ($i = 0; $i -lt $choices.count; $i++) 
            {
        	   
               $ChoiceDescriptions += @(New-Object System.Management.Automation.Host.ChoiceDescription ("&" + $choices[$i]))
               $resulthash.$i = $choices[$i]
            }
        $AllChoices = [System.Management.Automation.Host.ChoiceDescription[]]($ChoiceDescriptions)
        $result = $Host.UI.PromptForChoice($Caption,$Message, $AllChoices, 1)
        $resulthash.$result -replace "&", ""
        }         
}

new-choice -Choices "yes","no","maybe"