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

mm9b1k5b  于 2023-01-26  发布在  Shell
关注(0)|答案(3)|浏览(152)

我不是一个很好的编剧,我在翻译中迷失了方向......也许我做得完全错了......
我正在构建一个脚本,我希望用户从一个选项列表中进行选择,特别是一年中的月份,然后在脚本的后面使用这个结果。
下面是代码片段:

$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))

但是,我不断得到一个错误:
错误:使用"4"个参数调用"PromptForChoice"时出现异常:"对象引用未设置为对象的示例。" TestingMenu.ps1(31,1):错误:在行:31字符:1错误:+ $结果= $host. ui.提示选择($标题,$消息,$选项,[int []...错误:+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~错误:+类别信息:未指定:(:)[],方法调用异常错误:+完全限定的错误ID:异常错误:
我不确定我错过了什么......而且我可能会使它比需要的更复杂......但是结束变量$result应该是类似于"04-April"的内容。
你知道我错过了什么吗?

px9o7tmv

px9o7tmv1#

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

# 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]:
svmlkihl

svmlkihl2#

查看您的选项变量,在$option4后面有一个句点而不是逗号。
解决这个问题,它应该工作。

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

hi3rlvi23#

正如另一个答案(@user681141)所说,但可能更简洁一点;
你可以把数组通过管道传输到ForEach-Object,别名是%

$choices = @(
  ("&Quit",        "Quit"),
  ("0&1-January",  "01-January OU"),
  ("0&2-February", "02-February OU"),
  ("0&3-March",    "03-March OU"),
  # etc...
) | % { New-Object System.Management.Automation.Host.ChoiceDescription $_ }

$Host.ui.PromptForChoice($title, $message, $choices, $default)

相关问题