2016-01-18 98 views
0

我想執行一個類似如下的命令:追加/串聯字符串參數

# $IncludeTraits is a String[] 
$exe = "C:\Foo.exe"; 
$traits; 
foreach ($IncludeTrait in $IncludeTraits) 
{ 
    if ($IncludeTrait -ne $null -and $IncludeTrait -ne "") 
    { 
     $traits = $traits + "-trait `"$IncludeTrait`" " 
    } 
} 

& $exe $traits 

最後的命令應該是:

Foo.exe -trait "One" -trait "Two" -trait "Three" 

如果我手動寫上面的命令它的工作原理,但不使用我的字符串連接的代碼。我怎樣才能得到這個使用字符串連接的工作?

+0

會發生什麼,如果你寫輸出「$ EXE $特徵「你看到命令Foo.exe - 」一個「 - 」兩個「 - 」三個「 –

回答

2

請勿使用字符串連接。收集你的論點中的數組:

$traits = foreach ($IncludeTrait in $IncludeTraits) { 
    if ($IncludeTrait) { '-trait'; $IncludeTrait } 
} 

然後運行與數組命令:

& $exe $traits 

或使用splatting

& $exe @traits