powershell和CMD数组参数未填充



周五下午,我正在尝试从cmd调用powershell脚本(类似于Nuke如何调用"build"),但我无法获得数组参数来正确传递和填充。

设置如下:我有一个文本文件名为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窗口并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:srct> 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:srct> .foods.ps1 -n lunch -e apple,orange
[0] apple
[1] orange

更新:

16:00:45.10  C:srct
C:>powershell -NoLogo -NoProfile -Command ".foods.ps1 -n lunch -e apple,orange"
[0] apple
[1] orange

更新2:

16:01:17.45  C:srct
C:>powershell -NoLogo -NoProfile -Command "C:srctfoods.ps1 -n lunch -e apple,orange"
[0] apple
[1] orange