Search code examples
powershellchocolatey

Chocolatey Passing array as a parameter


Can someone advice how to pass array as parameter to chocolatey package. I have written below sample code to test the code.

$pp = Get-PackageParameters

# set a default if it not passed
if (!$pp['PowerUser']) { "Provide user to proceed further."}


Foreach ($user in $pp['PowerUser']) {
    "$user"
}

I am using below command to run the package:

choco install -f .\amm.test.install.1.0.nupkg --params "/PowerUser:Test1,Test2"

Solution

  • Presumably - given that Chocolatey doesn't know anything about the purpose of the open-ended pass-through parameters passed to its --params parameter - the string Test1,Test2 is stored as-is in $pp['PowerUser'].

    Therefore, it is up to you to interpret the string as an array, which you can do with the -split operator:

    foreach ($user in $pp['PowerUser'] -split ',') {
      $user
    }