您需要通过调用脚本
powershell.exe
:
Start-Process powershell -ArgumentList "-File .\test.ps1 arg1 arg2 argX"
可以将参数列表指定为字符串或字符串数组。
See example 7 here
了解更多信息。
如@mklement0在问题注释中所述,如果您不通过
父进程
,它将在默认上下文中执行它,就像Windows认为的那样
.ps1
文件应该被执行,在这种情况下,它不会向脚本传递额外的参数。
您可能不需要使用
Start-Process
不过,如果您不需要
启动进程
&
运算符,或指定脚本的路径,就像交互式地那样:
# You can use a variable with the path to the script
# in place of .\test.ps1 here and provide the arguments
# as variables as well, which lets you build a dynamic
# command out without using `Start-Process` or `Invoke-Expression`.
& .\test.ps1 arg1 arg2 argX
或
# You can use variables for the arguments here, but the script name
# must be hardcoded. Good for cases where the entrypoint doesn't change.
.\test.ps1 arg1 arg2 argX
你也可以考虑使用
argument splatting
在构建动态命令时也可以使用。
I wrote an answer here
还有更详细的描述。