[ACCEPTED]-How to expand a PowerShell array when passing it to a function-powershell

Accepted answer
Score: 16

There isn't a good solution to this problem 8 in PowerSHell V1. In V2 we added splatting 7 (though for various reasons, we use @ instead 6 of * for this purpose). Here's what it looks 5 like:

PS (STA-ISS) (2) > function foo ($x,$y,$z) { "x:$x 4 y:$y z:$z" }

PS (STA-ISS) (3) > $a = 1,2,3

PS 3 (STA-ISS) (4) > foo $a # passed as single 2 arg

x:1 2 3 y: z:

PS (STA-ISS) (5) > foo @a 1 # splatted

x:1 y:2 z:3

Score: 0

Well, there may be a better way, but see 1 if this works:

inner --flag [string]::Join(" ", $args)
Score: 0

Building on @EBGreen's idea, and a related 4 question I noticed in the sidebar, a possible 3 solution is this:

function outer
{
    invoke-expression "inner --flag $($args -join ' ')"
}

Note: This example makes use of the Powershell 2.0 CTP's new -join operator.

However, I'd still like 2 to find a better method, since this feels 1 like a hack and is horrible security-wise.

Score: 0

If you want a quick ready-made solution, you 1 can copy paste mine:

<#
    .SYNOPSIS
    Asks a question and waits for user's answer

    .EXAMPLE
    Usage with shortcuts and without ReturnValue
    Invoke-Question -Question "What would you like" -Answers "&Eggs", "&Toasts", "&Steak"

    Shows the quesiton and waits for input. Let's assume user input is 'S', the return value would be 2 (index of "&Steak")

    .EXAMPLE
    Usage without shortcuts and with ReturnValue
    Invoke-Question -Question "What would you like" -Answers "Eggs", "Toasts", "Steak" -ReturnValue

    Shows the quesiton and waits for input. The answers are prefixed with numbers 1, 2 and 3 as shortcuts.
    Let's assume user input is 2, the return value would be "Toasts" (prefixed numbers are "index + 1")

    .EXAMPLE
    Usage from pipeline with default value
    @("Eggs", "Toasts", "Steak") | Invoke-Question -Question "What would you like" -ReturnValue -Default 2

    Shows the quesiton and waits for input. The answers are taken from pipeline and prefixed with numbers 1, 2 and 3 as shortcuts.
    Steak is marked as default. If user simply continues without a choice, Steak is chosen for her.
    However, let's assume user input is 1, the return value would be "Eggs" (prefixed numbers are "index + 1")
#>
function Invoke-Question {
    [CmdletBinding()]
    param(
        # Main question text
        [Parameter(Mandatory = $true)]
        [string] $Question,

        # Question description, e.g. explanation or more information
        [Parameter(Mandatory = $false)]
        [string] $Description = "",

        # Default answer as index in the array, no answer is selected by default (value -1)
        [Parameter(Mandatory = $false)]
        [int] $Default = -1,

        # Set of answers, if the label is given with & sign, the prefixed letter is used as shortcut, e.g. "&Yes" -> Y,
        # otherwise the answer is prefixed with "index + 1" number as a shortcut
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [string[]] $Answers,

        # If set, returns a value of selected answer, otherwise returns its index in the Answer array
        [switch] $ReturnValue
    )

    begin {
        # init choices
        $choices = New-Object Collections.ObjectModel.Collection[Management.Automation.Host.ChoiceDescription]
        $answerNumber = 1
        $rememberAnswers = @()
    }

    process {
        #init answers
        foreach ($answer in $answers) {
            $rememberAnswers += $answer
            if ($answer -notmatch "&") {
                # add number if shortcut not specified
                $answer = "&$answerNumber $answer"
            }

            $choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList $answer))
            $answerNumber++
        }
    }

    end {
        # ask question and return either value or index
        $index = $Host.UI.PromptForChoice($Question, $Description, $choices, $Default)
        if ($ReturnValue) {
            $rememberAnswers[$index]
        } else {
            $index
        }
    }
}

More Related questions