Using a re-entrant script block to get user input

$AskForColor = {
  $EnteredColor = Read-Host "Enter a color, or Q to quit" 
   switch ($EnteredColor) {
   'Red' {'You chose red'}
   'Blue' {'You chose blue'}
   'Yellow' {'You chose yellow'}
   'Q' {Return}
  default { 'I do not have that color.  Try again.'
            .$AskForColor}
           }
}
.$AskForColor

Advertisements

One response to “Using a re-entrant script block to get user input

  1. seems like a simple and efficient way to do this. much better to have the code to handle the “invalid input” inside the script block than to do that by wrapping it in a while loop of some kind.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s