我不能将Switch参数作为数组。例如

时间:2016-04-07 06:54:29

标签: powershell-v4.0

$ b = 1,4,6 SWITCH($ B)     {

  1 {"Reviewers are Waheed"}
  2 {"Reviewers are amjad"}
  3 {"Reviewers are basil"}
  4 {"Reviewers are ehsan"}
  5 {"Reviewers are Ali"}
  6 {"Reviewers are chupachoops"}
  7 {"Reviewers are poopapoops"}
  8 {"Reviewers are leelo leer"}
  9 {"Reviewers are chuwara"} 
}

此代码将回显我想要的正确输出。

评论员是Waheed 评论家是ehsan 评论家是chupachoops

但如果我喜欢,

SWITCH($ b = read-host"输入No:")     {

  1 {"Reviewers are Waheed"}
  2 {"Reviewers are amjad"}
  3 {"Reviewers are basil"}
  4 {"Reviewers are ehsan"}
  5 {"Reviewers are Ali"}
  6 {"Reviewers are chupachoops"}
  7 {"Reviewers are poopapoops"}
  8 {"Reviewers are leelo leer"}
  9 {"Reviewers are chuwara"} 
}

现在,如果用户进入,则< 1,2,7>我应该得到正确的输出,输出对应于相应的开关盒,但是我怎么没有得到任何东西作为输出,可以任何一个帮助?

1 个答案:

答案 0 :(得分:0)

您可以使用get-member检查变量包含的内容,这对解决此类问题非常有用。默认情况下,读取主机会在这种情况下将您的输入转换为int32,并且您不希望这样。您可以强制输入为字符串,然后将其拆分为读取交换机中的每个值,如下所示:

[string]$b = read-host "Enter No: "
SWITCH($b.Split(",")) {

1 {"Reviewers are Waheed"}
2 {"Reviewers are amjad"}
3 {"Reviewers are basil"}
4 {"Reviewers are ehsan"}
5 {"Reviewers are Ali"}
6 {"Reviewers are chupachoops"}
7 {"Reviewers are poopapoops"}
8 {"Reviewers are leelo leer"}
9 {"Reviewers are chuwara"} 
}