Return 输入结果后到菜单
Return to menu after the input result
我正在制作 Show-Menu PS 脚本。用户输入并获得结果后。他应该会再次看到菜单,而不是“输入用户 ID:”
我知道这可以通过删除 Show-Menu 来实现,但是我的命令没有选择我的用户输入。
提前致谢!
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-Gridview
cmdlet 会更简单。如果您可以安装的话,可以使用 text-only 控制台版本。]
以下是一种略有不同的方法。它接受可能的菜单选项列表,并且只接受这些项目或 x
作为有效选项。
我认为代码相当明显,所以我将展示它……以及屏幕输出。 [咧嘴一笑]
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 ].
我正在制作 Show-Menu PS 脚本。用户输入并获得结果后。他应该会再次看到菜单,而不是“输入用户 ID:”
我知道这可以通过删除 Show-Menu 来实现,但是我的命令没有选择我的用户输入。
提前致谢!
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-Gridview
cmdlet 会更简单。如果您可以安装的话,可以使用 text-only 控制台版本。]
以下是一种略有不同的方法。它接受可能的菜单选项列表,并且只接受这些项目或 x
作为有效选项。
我认为代码相当明显,所以我将展示它……以及屏幕输出。 [咧嘴一笑]
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 ].