Power Shell脚本 - 如果和切换缺少语句

时间:2016-08-08 08:51:13

标签: powershell if-statement

我有脚本,它有效,而且可能有一些愚蠢的标志丢失,但我无法找到它。 请帮帮我? 应该弹出一个选择栏并根据所选项目更改$答案。

#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

1 个答案:

答案 0 :(得分:4)

您的代码应该有效。如果您只想设置$Answer,也可以在切换块中执行此操作:

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

现在你可以省略所有if语句......