输入结果后返回菜单



我正在编写Show Menu PS脚本。在用户输入并得到结果之后。他应该再看看菜单,而不是";输入用户ID:";

我知道这可以通过删除"显示菜单"来实现,但我的命令没有选择我的用户输入。

提前感谢!

function Show-Menu {
param (
[string]$Title = 'UserInfo V3.1'
)
Write-Host "Enter the user ID: " -ForegroundColor Cyan -NoNewline
Read-Host 
Write-Host ""   
Write-Host "================ $Title ================"   
Write-Host "Press 'U' for general user info."
Write-Host "Press 'P' for account and password information."
Write-Host "Press 'C' for computer info."
Write-Host "Press 'G' for Virtual Applications (AVC)."
write-Host "Press 'S' for SNOW info details"
Write-Host "Press 'Q' to  Quit."
write-Host "=============================================="
}
do {
$userName = Show-Menu 
$Selection = Read-Host "Please make a selection"
switch ($Selection) {
'U' { Get-ADUser -Identity $Username; pause }
}
} until ($Selection -eq 'q')

如果Show-Menu只应该显示菜单,那么提示用户使用Read-Host进行输入显然是不合适的。将该部分移出Show-Menu功能:

function Show-Menu {
param (
[string]$Title = 'UserInfo V3.1'
)
Write-Host ""   
Write-Host "================ $Title ================"   
Write-Host "Press 'U' for general user info."
Write-Host "Press 'P' for account and password information."
Write-Host "Press 'C' for computer info."
Write-Host "Press 'G' for Virtual Applications (AVC)."
write-Host "Press 'S' for SNOW info details"
Write-Host "Press 'Q' to  Quit."
write-Host "=============================================="
}
Write-Host "Enter the user ID: " -ForegroundColor Cyan -NoNewline
$userName = Read-Host 
do {
Show-Menu 
$Selection = Read-Host "Please make a selection"
switch ($Selection) {
'U' { Get-ADUser -Identity $Username; pause }
}
} until ($Selection -eq 'q')

[您可能会发现只使用Out-Gridviewcmdlet更简单。如果您可以安装这样的控制台,则可以使用纯文本控制台版本。]


以下方法略有不同。它接受可能的菜单选择的列表并且仅接受那些项目或CCD_ 5作为有效选择。

我认为代码相当明显,所以我只展示它。。。以及屏幕输出。[grin]

function Get-MenuChoice
{
[CmdletBinding ()]
Param
(
[Parameter (
Mandatory,
Position = 0
)]
[string[]]
$MenuList,
[Parameter (
Position = 1
)]
[string]
$Title,
[Parameter (
Position = 2
)]
[string]
$Prompt = 'Please enter a number from the above list or "x" to exit '

)
$ValidChoices = 0..$MenuList.GetUpperBound(0) + 'x'
$Choice = ''
while ([string]::IsNullOrEmpty($Choice))
{
Write-Host $Title
foreach ($Index in 0..$MenuList.GetUpperBound(0))
{
Write-Host ('{0} - {1}' -f $Index, $MenuList[$Index])
}
$Choice = Read-Host -Prompt $Prompt
Write-Host ''
if ($Choice -notin $ValidChoices)
{
[System.Console]::Beep(1000, 300)
Write-Warning ''
Write-Warning ('    [ {0} ] is not a valid selection.' -f $Choice)
Write-Warning '    Please try again.'
Write-Warning ''
$Choice = ''
pause
}
}
# send it out to the caller
if ($Choice -eq 'x')
{
'Exit'
}
else
{
$Choice
}
} # end >>> function Get-MenuChoice

'***** demo usage below *****'
$MenuList = @(
'An Item'
'Some Other Item'
'Middle Menu Item'
'Yet Another Item'
'The Last Choice'
)
$Choice = Get-MenuChoice -MenuList $MenuList
'You chose [ {0} ] giving you [ {1} ].' -f $Choice, $MenuList[$Choice]

一个无效&一个有效输入。。。

0 - An Item
1 - Some Other Item
2 - Middle Menu Item
3 - Yet Another Item
4 - The Last Choice
Please enter a number from the above list or "x" to exit : 66
WARNING: 
WARNING:     [ 66 ] is not a valid selection.
WARNING:     Please try again.
WARNING: 
Press Enter to continue...: 
0 - An Item
1 - Some Other Item
2 - Middle Menu Item
3 - Yet Another Item
4 - The Last Choice
Please enter a number from the above list or "x" to exit : 1
You chose [ 1 ] giving you [ Some Other Item ].

最新更新