将数组作为参数传递给Chocolatey

huangapple go评论88阅读模式
英文:

Chocolatey Passing array as a parameter

问题

以下是翻译好的部分:

  1. 有人能指导如何将数组作为参数传递给Chocolatey包。我编写了下面的示例代码来测试这段代码。
  2. $pp = Get-PackageParameters
  3. # 如果未传递默认值
  4. if (!$pp['PowerUser']) { "请提供用户以继续操作。" }
  5. Foreach ($user in $pp['PowerUser']) {
  6. "$user"
  7. }

我正在使用以下命令运行该包:

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

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

  1. $pp = Get-PackageParameters
  2. # set a default if it not passed
  3. if (!$pp['PowerUser']) { "Provide user to proceed further."}
  4. Foreach ($user in $pp['PowerUser']) {
  5. "$user"
  6. }

I am using below command to run the package:

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

答案1

得分: 1

由于Chocolatey不知道传递给其--params参数的开放性传递参数的目的,所以可以假定字符串Test1,Test2会按原样存储在$pp['PowerUser']中。

因此,由您来解释该字符串为一个数组,您可以使用-split运算符来完成:

  1. foreach ($user in $pp['PowerUser'] -split ',') {
  2. $user
  3. }
英文:

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:

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

huangapple
  • 本文由 发表于 2023年5月28日 17:55:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76350892.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定