我想添加一个中止菜单在 power shell 控制台菜单中。如果控制台菜单中的列表超过 7 个选项,则新的菜单列表页面应该再次列出 7 个选项。我遇到了在中提到的解决方案https://stackoverflow.com/questions/59318105/powershell-make-a-menu-out-of-text-file这为动态菜单生成提供了一种非常好的方法。但是如果菜单选项超出,我就无法添加新页面。也无法添加中止菜单。有人能帮助我吗?
我尝试了下面的代码:
function Show-Menu {
Param(
[Parameter(Position=0, Mandatory=$True)]
[string[]]$MenuItems,
[string] $Title
)
$header = $null
$header = '{0}{1}{2}' -f $Title, [Environment]::NewLine, ('-' * $len)
}
# possible choices: digits 1 to 9, characters A to Z
$choices = (49..57) + (65..90) | ForEach-Object { [char]$_ }
if (![string]::IsNullOrWhiteSpace($Title)) {
$len = [math]::Max(($MenuItems | Measure-Object -Maximum -Property Length).Maximum, $Title.Length)
$i = 0
$items = ($MenuItems | ForEach-Object { '{0} {1}' -f $choices[$i++], $_ }) -join [Environment]::NewLine
# display the menu and return the chosen option
while ($true) {
cls
if ($header) { Write-Host $header -ForegroundColor Yellow }
Write-Host $items
Write-Host
$answer = (Read-Host -Prompt 'Please make your choice').ToUpper()
$index = $choices.IndexOf($answer[0])
if ($index -ge 0 -and $index -lt $MenuItems.Count) {
return $MenuItems[$index]
}
else {
Write-Warning "Invalid choice.. Please try again."
}
}
}
# get a list of file names
$Files = Get-ChildItem -Path "c:\Temp" -Filter 'abc*.txt' -File | Select-Object Name, FullName
$selected = Show-Menu -MenuItems $Files.Name -Title 'Please select the file to use'
# get the full path name for the chosen file from the $Files array
$ToUse = ($Files | Where-Object { $_.Name -eq $selected }).FullName
Write-Host "`r`nYou have selected file '$ToUse'"
我使用了上面的代码片段。在上面的代码片段中,菜单的选项列表超过 7 个,但字母数字字符不应该出现。如果菜单列表超过 7 个,则应提示用户进入下一页,下一页应有新的 7 个菜单列表。此外,在最后应添加“中止”菜单选项,而我无法添加。有人能帮忙吗?