powershell 和 cmd 数组参数没有填充
powershell and cmd array parameter no populating
周五下午,
我正在尝试从 cmd 调用 powershell 脚本(类似于 Nuke 调用“构建”的方式),但我无法获得数组参数以正确传递和填充。
设置如下:
我有 1 个文本文件“masterswitch.cmd”,它是一个单行文件,仅调用 powershell 脚本“masterswitch。ps1”,都在同一目录中。
powershell -ExecutionPolicy ByPass -NoProfile -File "%~dp0masterswitch.ps1" %*
“masterswitch.ps1”文件的内容如下。
[CmdletBinding()]
Param(
[Alias("n")]
[string]$meal,
[Alias("e")]
[array]$foods,
[Alias("h")]
[switch]$help
)
if ($Help){
powershell -command "get-help $PSCommandPath -Detailed"
exit
}
if ($meal.length -eq 0){
Write-Output "`n No meal to eat"
exit}
if ($foods.length -eq 0){
Write-Output "`n No foods where provided"
exit}
$i = 0
foreach ( $line in $foods) {
write "[$i] $line"
$i++
}
打开 cmd window 并 cd 到这两个文件所在的目录。然后 运行 masterswitch -h
工作得很好。 masterswitch -n lunch
也收到了 -foods 丢失的预期通知。
但是当我 运行 masterswitch -n dinner -e burritos,nachos
我得到 [0] burritos,nachos
.
的输出
我应该得到的,以及当我从 powershell ide 中 运行 得到的是:
[0] burritos
[1] nachos
那么,在我设置的一行“masterswitch.cmd”文件中,是什么阻止了 powershell 正确解析我传递的数组的能力? (是的,我意识到我可以把它变成一个字符串并自己解析)
更新
下面的答案很清楚。所要做的就是将那个班轮从 -File
更改为 -Command
。所以新的班轮是
powershell -ExecutionPolicy ByPass -NoProfile -Command "%~dp0masterswitch.ps1" %*
这段代码是否产生了您想要的结果?
PS C:\src\t> type .\foods.ps1
[CmdletBinding()]
Param(
[Alias("n")]
[string]$meal,
[Alias("e")]
[string[]]$foods,
[Alias("h")]
[switch]$help
)
if ($Help){
powershell -command "get-help $PSCommandPath -Detailed"
exit
}
if ($meal.length -eq 0){
Write-Output "`n No meal to eat"
exit}
if ($foods.length -eq 0){
Write-Output "`n No foods where provided"
exit}
$i = 0
foreach ( $line in $foods) {
write "[$i] $line"
$i++
}
PS C:\src\t> .\foods.ps1 -n lunch -e apple,orange
[0] apple
[1] orange
更新:
16:00:45.10 C:\src\t
C:>powershell -NoLogo -NoProfile -Command ".\foods.ps1 -n lunch -e apple,orange"
[0] apple
[1] orange
更新二:
16:01:17.45 C:\src\t
C:>powershell -NoLogo -NoProfile -Command "C:\src\t\foods.ps1 -n lunch -e apple,orange"
[0] apple
[1] orange
周五下午, 我正在尝试从 cmd 调用 powershell 脚本(类似于 Nuke 调用“构建”的方式),但我无法获得数组参数以正确传递和填充。
设置如下: 我有 1 个文本文件“masterswitch.cmd”,它是一个单行文件,仅调用 powershell 脚本“masterswitch。ps1”,都在同一目录中。
powershell -ExecutionPolicy ByPass -NoProfile -File "%~dp0masterswitch.ps1" %*
“masterswitch.ps1”文件的内容如下。
[CmdletBinding()]
Param(
[Alias("n")]
[string]$meal,
[Alias("e")]
[array]$foods,
[Alias("h")]
[switch]$help
)
if ($Help){
powershell -command "get-help $PSCommandPath -Detailed"
exit
}
if ($meal.length -eq 0){
Write-Output "`n No meal to eat"
exit}
if ($foods.length -eq 0){
Write-Output "`n No foods where provided"
exit}
$i = 0
foreach ( $line in $foods) {
write "[$i] $line"
$i++
}
打开 cmd window 并 cd 到这两个文件所在的目录。然后 运行 masterswitch -h
工作得很好。 masterswitch -n lunch
也收到了 -foods 丢失的预期通知。
但是当我 运行 masterswitch -n dinner -e burritos,nachos
我得到 [0] burritos,nachos
.
我应该得到的,以及当我从 powershell ide 中 运行 得到的是:
[0] burritos
[1] nachos
那么,在我设置的一行“masterswitch.cmd”文件中,是什么阻止了 powershell 正确解析我传递的数组的能力? (是的,我意识到我可以把它变成一个字符串并自己解析)
更新
下面的答案很清楚。所要做的就是将那个班轮从 -File
更改为 -Command
。所以新的班轮是
powershell -ExecutionPolicy ByPass -NoProfile -Command "%~dp0masterswitch.ps1" %*
这段代码是否产生了您想要的结果?
PS C:\src\t> type .\foods.ps1
[CmdletBinding()]
Param(
[Alias("n")]
[string]$meal,
[Alias("e")]
[string[]]$foods,
[Alias("h")]
[switch]$help
)
if ($Help){
powershell -command "get-help $PSCommandPath -Detailed"
exit
}
if ($meal.length -eq 0){
Write-Output "`n No meal to eat"
exit}
if ($foods.length -eq 0){
Write-Output "`n No foods where provided"
exit}
$i = 0
foreach ( $line in $foods) {
write "[$i] $line"
$i++
}
PS C:\src\t> .\foods.ps1 -n lunch -e apple,orange
[0] apple
[1] orange
更新:
16:00:45.10 C:\src\t
C:>powershell -NoLogo -NoProfile -Command ".\foods.ps1 -n lunch -e apple,orange"
[0] apple
[1] orange
更新二:
16:01:17.45 C:\src\t
C:>powershell -NoLogo -NoProfile -Command "C:\src\t\foods.ps1 -n lunch -e apple,orange"
[0] apple
[1] orange