英文:
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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论