2016-10-14 49 views
0

我想从命令行向Power Shell传递数组%variable%,然后使用此数组变量在Power shell中执行操作,但我有将变量正确传递给电源外壳时遇到麻烦。当前.BAT脚本调用电源外壳脚本如下...Pass String String从命令行(.BAT文件)变为Power Shell脚本

SET STRING_ARRAY="test1" "test2" "test3" "test4" 
Powershell.exe -executionpolicy remotesigned -File "FILEPATH\Build_DB.ps1" %STRING_ARRAY% 

然后下面电源外壳脚本,以测试阵列varaible的SUCESSFUL切换如下:

[email protected]($args[0]) 

Write-Host $string_array.length 

for ($i=0; $i -lt $string_array.length; $i++) { 
    Write-Host $string_array[$i] 
} 

然而一切返回距离电源壳长度为1。我在这里做错了什么?

回答

0

好的,没关系我最终想出了一个解决方案,在我的情况下工作,所以我张贴在这里,以防其他人受益。如果有人有更好的解决方案,请让我知道。

如下更改开机shell脚本:

#The problem is that each item in the %STRING_ARRAY% variable is passed as 
#an individual argument to power shell. To get around this we can just 
#store all optional arguments passed to power shell as follows. 
[email protected]($args) 

#Now (if desired) we can also remove any optional arguments we don't want 
#in our new array using the following command. 
$string_array = $string_array[2..($string_array.Length)] 

Write-Host $string_array.length 

for ($i=0; $i -lt $string_array.length; $i++) { 
    Write-Host $string_array[$i] 
}