I would like to be able to exit the script after using option 1 and Q.
R for repeat works and any other key would bring me back to the
Menu before. Can i just use another while ($response -eq "Q") ? or is the do while loop just wrong for that? i already tried a if else version but im doing it kinda wrong. Any help please?
function Show-Menu
{
param (
[string]$Title = 'Who? '
)
cls
Write-Host "================ $Title ================"
Write-Host "1: 1"
Write-Host "2: 2"
Write-Host "3: 3"
Write-Host "4: 4"
Write-Host "5: 5"
Write-Host "6: 6"
Write-Host "7: 7"
Write-Host "8: 8"
Write-Host "Q: Q to Quit."
}
do
{
Show-Menu
$input = Read-Host "Please choose."
switch ($input)
{
'1' {
cls
'You chose option #1'
do {
$response = Read-Host "R to repeat, Q to Quit or anything else to go back" }
while ($response -eq "R")
} '2' {
cls
'You chose option #2'
} '3' {
cls
'You chose option #3'
} 'q' {
return
} '3' {
cls
'You chose option #1'
} '4' {
cls
'You chose option #2'
} '' {
cls
'You chose option #3'
} 'q' {
return
}
}
pause
}
until ($input -eq 'q')
how about:
'1'
{
cls
'You chose option #1'
do
{
$response = Read-Host "R to repeat, Q to Quit or anything else to go back"
}
while ($response -eq "R")
if ($response -eq "q")
{
exit
}
}
Just make an if statement. If response contains the Char Q then return.
function Show-Menu
{
param (
[string]$Title = 'Who? '
)
cls
Write-Host "================ $Title ================"
Write-Host "1: 1"
Write-Host "2: 2"
Write-Host "3: 3"
Write-Host "4: 4"
Write-Host "5: 5"
Write-Host "6: 6"
Write-Host "7: 7"
Write-Host "8: 8"
Write-Host "Q: Q to Quit."
}
do
{
Show-Menu
$input = Read-Host "Please choose."
switch ($input)
{
'1' {
cls
'You chose option #1'
do {
$response = Read-Host "R to repeat, Q to Quit or anything else to go back" }
while ($response -eq "R")
if($response.Contains("Q"))
{return}
} '2' {
cls
'You chose option #2'
} '3' {
cls
'You chose option #3'
} 'q' {
return
} '3' {
cls
'You chose option #1'
} '4' {
cls
'You chose option #2'
} '' {
cls
'You chose option #3'
} 'q' {
return
}
}
pause
}
until ($input -eq 'q')
One way to do it
Related
I'm working on a Show-Menu PS script. After the user gives in his input and he gets the result. He should see again the Menu and not "Enter the user ID: "
I know this is possible by removing the Show-Menu but than my user input is not selected for my commands.
Thanks in advance!
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')
If Show-Menu is only supposed to show a menu, then prompting a user for input with Read-Host is clearly not appropriate. Move that part out of the Show-Menu function:
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')
[you may find it simpler to just use the Out-Gridview cmdlet. there is a text-only console version available if you can install such.]
the following is a slightly different approach. it accepts a list of possible menu choices and ONLY accepts those items OR x as valid choices.
i think the code is fairly obvious, so i will just show it ... and the onscreen output. [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]
output for one invalid & one valid input ...
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 ].
trying to write a PowerShell script which uses a menu and sub menus. When I first run the code and press Q to quit the 'Main Menu', it quits back to console. However, if I go into 'Menu 1' then quit back to 'Main Menu' and then quit once more to the console, it runs this code again $input = Read-Host "Please choose an option"
I've tried looking for different methods of quitting the menu, but haven't found much luck as of yet. Any advice would be appreciated
See example code below:
function showMainMenu {
function showMenu1 {
do {
Write-Host "================ Menu 1 ================"
Write-Host "1: Option 1"
Write-Host "Q: Back to the main menu"
$input = Read-Host "Please choose an option"
switch ($input) {
'1' {
Write-Host "Some text"
} 'q' {
showMainMenu
}
}
}
until ($input -eq 'q')
}
function showMenu2 {
Write-Host "================ Menu 2 ================"
Write-Host "1: Option 1"
Write-Host "Q: Press Q to quit"
do {
$input = Read-Host "Please choose an option"
switch ($input) {
'1' {
Write-Host "Some text"
} 'q' {
showMainMenu
}
}
}
until ($input -eq 'q')
}
Write-Host "================ Main Menu ================"
Write-Host "1: Menu 1"
Write-Host "2: Menu 2"
Write-Host "3: Press Q to quit"
do {
$input = Read-Host "Please choose an option"
switch ($input) {
'1' {
showMenu1
} '2' {
showMenu2
} 'q' {
return
}
}
pause
}
until ($input -eq 'q')
}
showMainMenu
Its stuck in a loop due to using do{}until().
function showMainMenu {
function showMenu1 {
do {
Write-Host "================ Menu 1 ================"
Write-Host "1: Option 1"
Write-Host "Q: Back to the main menu"
$input = Read-Host "Please choose an option"
switch ($input) {
'1' {
Write-Host "Some text"
} 'q' {
showMainMenu
}
}
}
until ($input -eq 'q')
}
function showMenu2 {
Write-Host "================ Menu 2 ================"
Write-Host "1: Option 1"
Write-Host "Q: Press Q to quit"
do {
$input = Read-Host "Please choose an option"
switch ($input) {
'1' {
Write-Host "Some text"
} 'q' {
showMainMenu
}
}
}
until ($input -eq 'q')
}
Write-Host "================ Main Menu ================"
Write-Host "1: Menu 1"
Write-Host "2: Menu 2"
Write-Host "3: Press Q to quit"
$input = Read-Host "Please choose an option"
switch ($input) {
'1' {
showMenu1
} '2' {
showMenu2
} 'q' {
break
}
}
}
showMainMenu
Just making a little menu for zoom usage.
The user insert it's selection, and the script automatically opens zoom to the specific session.
The problem is at if (($hashTable.$selection).Count -gt 1) where $selection does have the value 1 (if I pressed 1) but the expression always gets false - so it proceeds to the else statement.
The goal is to have in the hash table either only a session number or both session number and a password.
After checking if there's more than one value to the key, then I chose my action.
$selection does holds the number the user enters - so why $hashTable.$selection is empty?
$hashTable = #{
1 = '6108514938', 'f'
2 = 'Val2'
3 = 'Val3'
}
function Show-Menu
{
param (
[string]$Title = 'My Menu'
)
Clear-Host
Write-Host "================ $Title ================"
Write-Host "1: Press '1' for Algorithms."
Write-Host "2: Press '2' for this option."
Write-Host "3: Press '3' for this option."
Write-Host "Q: Press 'Q' to quit."
}
do
{
Show-Menu –Title 'ZOOM Menu'
$zoomBaseLink = 'zoommtg://zoom.us/join?confno='
$selectedSession = $null
$selection = Read-Host "Please select session"
switch ($selection)
{
'1' { $selectedSession = ($hashTable.1) }
'2' { $selectedSession = ($hashTable.2) }
'3' { 'You chose option #3' }
}
if($selection -ne 'q') {
if (([string]::IsNullOrEmpty($selectedSession))) {
Write-Host 'session is null or empty'
}
else {
Write-Host $selection
Write-Host $selectedSession
pause
<#($hashTable.$selection) always gets false.#>
if (($hashTable.$selection).Count -gt 1) { Write-Host 'here 1'; Write-Host $hashTable.$selection[1] }
else{ Write-Host 'here 2'; Write-Host ($zoomBaseLink + $selectedSession) }
}
$selectedSession = $null
}
pause
}
until ($selection -eq 'q')
The problem ist, that you didn't convert the variable $selection to an integer.
[int]$selection = [convert]::ToInt32($selection, 10)
If you do that first, it will work (tested):
if($selection -ne 'q') {
if (([string]::IsNullOrEmpty($selectedSession))) {
Write-Host 'session is null or empty'
}
else {
Write-Host $selection
Write-Host $selectedSession
pause
[int]$selection = [convert]::ToInt32($selection, 10)
<#($hashTable.$selection) always gets false.#>
if (($hashTable.$selection).Count -gt 1) { Write-Host 'here 1'; Write-Host $hashTable.$selection[1] }
else{ Write-Host 'here 2'; Write-Host ($zoomBaseLink + $selectedSession) }
}
$selectedSession = $null
[string]$selection = $null
}
pause
Please let me know if it worked and if it did, please mark my post as the answer :)
I have a few powershell Scripts (simple ones) I want to execute within Visual Studio Code.
When I run it with F5 the VSC turns orange (Debug Mode) for a few seconds and then turns back to normal without showing me anything.
This is the console output:
PS C:\Users\Huberdo\Documents\WindowsPowerShell> c:\Users\Huberdo\Documents\Meine Projekte\HD-Powershell-Scripts\Mailbox\HD-Mailbox.ps1
PS C:\Users\Huberdo\Documents\Meine Projekte\HD-Powershell-Scripts\Mailbox>
The Script should ask me to make a choice with Write-Host but it doesn't.
Here is the main script:
function Show-Menu() {
Write-Host "================ $Title ================"
Write-Host "1: Press '1' for this option."
Write-Host "2: Press '2' for this option."
Write-Host "3: Press '3' for this option."
Write-Host "Q: Press 'Q' to quit."
do {
Show-Menu
$input = Read-Host "Please make a selection"
switch ($input) {
'1' {
cls
'You chose option #1'
} '2' {
cls
'You chose option #2'
} '3' {
cls
'You chose option #3'
} 'q' {
return
}
}
pause
}
until ($input -eq 'q')
}
I have installed and activated the Powershell Extension.
I googled and searched here on Stackoverflow for possible solutions or similiar questions, but couldn't find any.
Am I doing something wrong? I tried it with x86 and x64 PS.
You are missing a closing bracket in the Show-Menu function.
function Show-Menu() {
Write-Host "================ $Title ================"
Write-Host "1: Press '1' for this option."
Write-Host "2: Press '2' for this option."
Write-Host "3: Press '3' for this option."
Write-Host "Q: Press 'Q' to quit."
}
do {
Show-Menu
$input = Read-Host "Please make a selection"
switch ($input) {
'1' {
cls
'You chose option #1'
} '2' {
cls
'You chose option #2'
} '3' {
cls
'You chose option #3'
} 'q' {
return
}
}
pause
}
until ($input -eq 'q')
If that does not solve your issue - then I would look into verifying that you installed the powershell extension properly and when you create the powershell script in VS you are choosing:
File -> New Project -> Powershell -> Powershell Script Project
I want to add a another (small) script for the main script who has choices. How can I do that ?
The script I want to add:
*$shell = New-Object -ComObject WScript.Shell
$desktop = [System.Environment]::GetFolderPath('Desktop')
$shortcut = $shell.CreateShortcut("$desktop\Jauns Notepad.lnk")
$shortcut.TargetPath = "notepad.exe"
$shortcut.IconLocation = "%windir%\system32\imageres.dll 98"
$shortcut.Save()*
And the main script:
do {
do {
write-host ""
write-host "1 - Selection 1"
write-host "2 - Selection 2"
write-host ""
write-host "0 - Exit"
write-host ""
write-host -nonewline "Type your choice and press Enter: "
$choice = read-host
write-host ""
$ok = $choice -match '^[abcdx]+$'
if ( -not $ok) { write-host "Invalid selection" }
} until ( $ok )
switch -Regex ( $choice ) {
"1"
{
write-host "You entered '1'"
}
"2"
{
write-host "You entered '2'"
}
}
} until ( $choice -match "0" )
Please somebody help out!