Julia
Julia

Reputation: 145

Power Shell script - if and switch missing statement

I have the script, which worked, and it's probably some stupid sign missing, but i just cant find it. Help me please? Is supposed to pop up a choice bar and change the $answer according to the chosen project.

#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

Upvotes: 2

Views: 665

Answers (2)

user22037299
user22037299

Reputation: 1

   #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"; $Answer = "LLL"}
       1 {"JJJ"; $Answer = "JJJ"}
       2 {"PPP"; $Answer = "PPP"}
    }

Upvotes: 0

Martin Brandl
Martin Brandl

Reputation: 58991

Your code should work. If you only want to set $Answer, you can also do that within your switch block:

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

Now you can omit all if statements...

Upvotes: 4

Related Questions