2016-08-08 7 views
2

私はうまくいったスクリプトを持っていますが、それはおそらく愚かな兆候でしょうが、私はそれを見つけることができません。 お願いします。 選択バーをポップアップし、選択したプロジェクトに応じて$回答を変更するはずです。パワーシェルスクリプト - もしスイッチと不足している文がある場合

#What project do you work on 
$caption = "Choose Action"; 
$message = "What project are you testing?"; 
$LLL = new-Object System.Management.Automation.Host.ChoiceDescription "&LLL","LLL"; 
$JJJ = new-Object System.Management.Automation.Host.ChoiceDescription "&JJJ","JJJ"; 
$PPP = new-Object System.Management.Automation.Host.ChoiceDescription "&PPP","PPP"; 
$choices = [System.Management.Automation.Host.ChoiceDescription[]]($LLL,$JJJ,$PPP); 
$Project = $host.ui.PromptForChoice($caption,$message,$choices,0) 

switch ($Project) { 
    0 {"LLL"; break} 
    1 {"JJJ"; break} 
    2 {"PPP"; break} 
    } 


if ($Project -eq 0) 
    { 
    $Answer = "LLL" 
    } 


if ($Project -eq 1) 
    { 
    $Answer = "JJJ" 
    } 


if ($Project -eq 2) 
    { 
    $Answer = "PPP" 
    } 


the error is that 

o.ps1:128 char:23 
+  if ($Project -eq 1) 
+      ~ 
Missing statement block after if (condition). 
At ps1:136 char:27 
+   if ($Project -eq 2) 
+       ~ 
Missing statement block after if (condition). 
    + CategoryInfo   : ParserError: (:) [], ParentContainsErrorRecordException 
    + FullyQualifiedErrorId : MissingStatementBlock 
+0

コピー/貼り付けコードを省略することができ、それはで問題なく動作します:あなたが唯一の$Answerを設定したい場合、また、あなたのスイッチブロック内ことを行うことができますISE ?!実行するためにその一部を選択しただけではないと確信していますか? –

+0

特に古いバージョンのPowerShellはありますか?明示的な行末の文字を持たない言語では、行の継続が難しくなります。 PowerShellは、バージョン番号が増えるにつれて、改行を避けて行間をきれいに処理する能力が大幅に向上しました。 –

+0

'switch'の結果を何かに代入するべきですが、現在はvoidで評価しています。たとえば、 '$ answer = switch {.....}'とし、スイッチを複製する際に 'if'を削除します。 – wOxxOm

答えて

4

コードは機能するはずです。

$Answer = switch ($Project) { 
    0 {'LLL'} 
    1 {'JJJ'} 
    2 {'PPP'} 
} 

は今、あなたはすべてのif文...

関連する問題