我有一个批处理文件 test.bat 来启动 powershell 脚本:
@pushd "C:\myscripts"
powershell .\test.ps1 arg1 "arg2 with space" arg3
@popd
脚本 test.ps1(位于 C:\myscripts)是一个非常简单的脚本,如下所示:
# just print out the arguments
Write-Output ("args count: {0}" -f $args.length)
$args
然后我尝试启动test.bat。我应该将三个参数传递给 ps1,但我得到以下结果:
参数数量:5
精氨酸1
精氨酸2
和
空间
精氨酸3
我在脚本中所期望的,args[0] 应该是 arg1,args[1] 应该是“arg2 with space”,args3[2] 应该是 arg3。我不明白为什么脚本实际上有 5 个参数。
如何按照我的预期将参数从 cmd 或批处理传递到 powershell?像这样:
args count: 3
arg1
arg2 with space
arg3
powershell .\test.ps1 arg1 'arg2 with space' arg3
or
powershell .\test.ps1 arg1 """arg2 with space""" arg3
我认为你应该尽量避免使用双引号,因为 cmd 也已经使用了它们,因此很难预测 PowerShell 到底会得到什么。请记住,这会通过twoshell,因此有两层转义/引用。
PowerShell 本身并没有对单引号和双引号做出太多区分。至少在这种情况下,差异是无关紧要的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)