将数组作为参数传递给Chocolatey

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

Chocolatey Passing array as a parameter

问题

以下是翻译好的部分:

有人能指导如何将数组作为参数传递给Chocolatey包。我编写了下面的示例代码来测试这段代码。

$pp = Get-PackageParameters

# 如果未传递默认值
if (!$pp['PowerUser']) { "请提供用户以继续操作。" }

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

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

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.

$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"

答案1

得分: 1

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

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

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

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
}

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:

确定