2009-11-26 129 views
5

我有一个test.bat批处理文件来启动PowerShell脚本:使用蝙蝠开始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,但我得到了以下结果:

ARGS数:5 ARG1 ARG2 与 空间 ARG3

我预计在脚本什么,ARGS [0]应该ARG1 args [1]应该是“arg2 with space”,args3 [2]是arg3。我不明白为什么脚本实际上有5个参数。

如何将cmd或batch中的参数传递给PowerShell?就像这样:

args count: 3 
arg1 
arg2 with space 
arg3 

回答

7
powershell .\test.ps1 arg1 'arg2 with space' arg3 

powershell .\test.ps1 arg1 """arg2 with space""" arg3 

我想你应该尽量避免使用双引号CMD已经使用了他们太多,因此这是一个有点很难预测究竟PowerShell中会得到。请记住,这是通过两个炮弹,因此两层逃逸/报价。

PowerShell本身对单引号和双引号没有太大的区别。至少在这种情况下,差异是无关紧要的。

0

好的。我想我明白了:

@pushd “C:\ myscripts”。 的PowerShell \ test.ps1 ARG1 'ARG2与空间' ARG3 @popd

单引号字符,而不是双之一。也许他们在PS中意味着不同的东西。

+0

他们没有。但是它们对于'cmd'确实意味着不同的东西。 – Joey 2009-11-26 17:16:43