Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Dynamically populate powershell switch statement

$
0
0

Hi,

I need help dynamically creating switch statement selections.

I can do this OK.

$ar = "RED","GREEN"

$ANS = (Read-Host "Choose from these $ar")
Choose from options RED GREEN: RED
$ANS
RED
switch ($ANS){

GREEN {write-host "GREEN"}
RED {write-host "RED"}
default {write-host "NO GOOD"}
}

RED

But What if I want to dynamically populate the choices of Red and Green from the $ar array? It does not work. For example if I replace this..

"GREEN {write-host "GREEN"}
RED {write-host "RED"}"

with this..

"foreach($y in $ar){
$y
{write-host "$y"}
}"

Purpose to dynamically create the menu contents based on the array and provide actions based on those. My contents are never static.

I assume I can dynamically populate these somehow but I get an error..

Missing statement block in switch statement clause.

Any ideas?



Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>