批处理文件***.bat来启动Powershell脚本:
1
2
3
@pushd"C:myscripts"
powershell .***.ps1 arg1"arg2 with space" arg3
@popd
脚本***.ps1(位于C: my烧束再镇烈scripts)是一个非常简单的脚本,例如:
1
2
3
# just print out the arguments
Write-Output ("args count: {0}" -f $args.length)
$args
然后,我尝试启动***.bat。 我应该将三个参数传360问答递给ps1,但是得到以下结果:
args计数:5
arg1
arg2
与
空间
arg3
我在脚本中期望的是,args [0]应该为arg1,而args [1]应该为"带有空格的arg2",而args3 [2]介云细深垂报引例我为arg3。 我不明期括移免死贵白为什么脚本实际毛兴苗双步范上得到5个参数。
如何按预期将参数从cmd或批处理传递到powers上误城短扬亲早hell? 像这样:
1
2
3
4
args cou分权探销约民间责至nt: 3
arg1
arg2 with space
a英他孔转道rg3
1
powershell .***.ps1 arg1 'arg2 with space' arg3
要么
1
powershell .***.ps1 arg1"""arg2 with space""" arg3
我认为您应该避免使用双引号,因为cmd也已经使用了双引号,因此很难预测PowerShell会得到细将矛味候沉什么。 请记住,这是通过两个外壳传递的,因此要经过两层转义/引用。
PowerShell本身在单引号和双垂送密意良审跳当铁引号之间没有太多区别。 至少占尼练矛技特在这种情况下,差异是无关紧要的。
好。 我想我明白了:
1
2
3
@作含害女后pushd"C:my族坚额scripts"
p松毫错owershell .***.ps1 arg1 'arg2 with spac顺州e' arg3
@popd
单引号字符而不是双引号。 也许它们在PS中的含义不同。
给你参考一下
希望能帮到你