Powershell - 让用户从 1 月到 12 月的列表中进行选择

Powershell - let user choice from list of January through December

我不是一个伟大的脚本编写者,我在翻译中迷失了......也许我这样做完全错了......

我正在构建一个脚本,我希望用户从选项列表中进行选择,特别是一年中的月份。此结果随后将在脚本中使用。

这是片段:

$optionQuit = New-Object System.Management.Automation.Host.ChoiceDescription "Quit", "Quit"
$option1 = New-Object System.Management.Automation.Host.ChoiceDescription "01-January", "01-January OU"
$option2 = New-Object System.Management.Automation.Host.ChoiceDescription "02-February", "02-February OU"
$option3 = New-Object System.Management.Automation.Host.ChoiceDescription "03-March", "03-March OU"
$option4 = New-Object System.Management.Automation.Host.ChoiceDescription "04-April", "04-April OU"
$option5 = New-Object System.Management.Automation.Host.ChoiceDescription "05-May", "05-May OU"
$option6 = New-Object System.Management.Automation.Host.ChoiceDescription "06-June", "06-June OU"
$option7 = New-Object System.Management.Automation.Host.ChoiceDescription "07-July", "07-July OU"
$option8 = New-Object System.Management.Automation.Host.ChoiceDescription "08-August", "08-August OU"
$option9 = New-Object System.Management.Automation.Host.ChoiceDescription "09-September", "09-September OU"
$option10 = New-Object System.Management.Automation.Host.ChoiceDescription "10-October", "10-October OU"
$option11 = New-Object System.Management.Automation.Host.ChoiceDescription "11-November", "11-November OU"
$option12 = New-Object System.Management.Automation.Host.ChoiceDescription "12-December", "12-December OU"
$options = [System.Management.Automation.Host.ChoiceDescription[]]($optionQuit, $option1, $option2, $option3, $option4. $option5, $option6, $option7, $option8, $option9, $option10, $option11, $option12)
$result = $host.ui.PromptForChoice($title, $message, $options, [int[]](0)) 

但是,我一直收到错误消息:

ERROR: Exception calling "PromptForChoice" with "4" argument(s): "Object reference not set to an instance of an object." TestingMenu.ps1 (31, 1): ERROR: At Line: 31 char: 1 ERROR: + $result = $host.ui.PromptForChoice($title, $message, $options, [int[] ... ERROR: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ERROR: + CategoryInfo : NotSpecified: (:) [], MethodInvocationException ERROR: + FullyQualifiedErrorId : NullReferenceException ERROR:

我不确定我错过了什么......我可能会把它变得比需要的更复杂......但是结束变量 $result 应该是类似于“04-April”的东西.

知道我在这里遗漏了什么吗?

看看你的 options 变量,$option4 后面有一个句号而不是逗号。

解决这个问题,它应该可以工作。

$options = [System.Management.Automation.Host.ChoiceDescription[]]($optionQuit, $option1, $option2, $option3, $option4, $option5, $option6, $option7, $option8, $option9, $option10, $option11, $option12)

这个变体有效而且不是那么多余。不标记要用 & 符号选择的字母是没有意义的

# C:\Test\Choose-CUI.ps1
$choices=@(
  ("&Quit","Quit"),
  ("0&1-January","01-January OU"),
  ("0&2-February","02-February OU"),
  ("0&3-March","03-March OU"),
  ("0&4-April","04-April OU"),
  ("0&5-May","05-May OU"),
  ("0&6-June","06-June OU"),
  ("0&7-July","07-July OU"),
  ("0&8-August","08-August OU"),
  ("0&9-September","09-September OU"),
  ("1&0-October","10-October OU"),
  ("11-&November","11-November OU"),
  ("12-&December","12-December OU")
)
$choicedesc = New-Object System.Collections.ObjectModel.Collection[System.Management.Automation.Host.ChoiceDescription]
for($i=0; $i -lt $choices.length; $i++){
  $choicedesc.Add((New-Object System.Management.Automation.Host.ChoiceDescription $choices[$i] ) ) }
$Host.ui.PromptForChoice($caption, $message, $choicedesc, $default)

有这个输出

PS C:\Test> .\Choose-CUI.ps1
[Q] Quit
[1] 01-January
[2] 02-February
[3] 03-March
[4] 04-April
[5] 05-May
[6] 06-June
[7] 07-July

[8] 08-August
[9] 09-September
[0] 10-October
[N] 11-November
[D] 12-December
[?] Help
Choice[0]: