2016-10-14 1 views
0

%%variable%の配列をコマンドシェルからpowerシェルに渡して、この配列変数で操作を実行しようとしていますが、変数をシェルに正しく渡すのに問題があります。パワーシェルスクリプトは以下の通りです呼び出すために、現在の.BATスクリプトは次のように...コマンドライン(.BATファイル)からPower Shellスクリプトに文字列配列を渡す

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

はその後、配列varaibleの成功でハンドオーバーをテストするには、以下のパワーシェルスクリプトは次のとおりです。

[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

申し訳ありませんが、私のケースではうまくいく解決策を思いついたので、他の人にとって恩恵を受ける場合はここに掲載しています。誰かがより良い解決策を持っている場合は、私に知らせてください。

変更パワーシェルスクリプトを次のように

#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] 
} 
関連する問題