Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 从命令行运行脚本并传递字符串数组_Powershell - Fatal编程技术网

Powershell 从命令行运行脚本并传递字符串数组

Powershell 从命令行运行脚本并传递字符串数组,powershell,Powershell,所以基本上我需要从命令行运行我的ps1脚本,并将自定义参数传递到脚本中。我的脚本需要一个字符串数组,但当我运行该命令时,出现一个错误,即找不到接受参数“X1”的位置参数 这是我的命令行: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy RemoteSigned -NoProfile -NonInteractive -File "C:\Program Files\MSBuild\MyScript.

所以基本上我需要从命令行运行我的ps1脚本,并将自定义参数传递到脚本中。我的脚本需要一个字符串数组,但当我运行该命令时,出现一个错误,即找不到接受参数“X1”的位置参数

这是我的命令行:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy RemoteSigned -NoProfile -NonInteractive -File "C:\Program Files\MSBuild\MyScript.ps1" -builds "X1” “X2" "X3" "X4"

我的理解是它知道如何处理第一个参数'X1',但不知道第二个参数,所以它崩溃了?有什么想法吗?

我不能很好地解释为什么
-file
不起作用,但该参数还有其他已知问题。使用它时,您无法从PowerShell获得正确的退出代码。使用
-command
可以:

Powershell.exe -ExecutionPolicy RemoteSigned -NoProfile -NonInteractive -Command "& {& 'C:\Program Files\MSBuild\myscript.ps1' -builds x1,x2,x3}"

您需要使用
-Command
参数,而不是
-File
参数。注意下面的屏幕截图和示例脚本中的行为变化

[CmdletBinding()]
param (
    [int[]] $MyInts
)

foreach ($MyInt in $MyInts) {
    $MyInt + 1;
}

我不熟悉powershell,我不知道您的脚本-您应该在这里发布-不能使用冒号分隔的元素,如“X1,X2,X3,…”?与逗号没有区别,或者我不应该知道。我必须弄清楚,因为TFS 2013中的运行脚本使用了-File和not-Command。我需要修改ps1脚本以识别逗号并在其中创建字符串数组。