Hello!
\nSo, I’m a beginner in Powershell and this may seem like an idiotic question but, I’m writing my first script and encountered an issue with the do/until loop.<\/p>\n
Here’s the script:<\/p>\n
Clear-Host<\/p>\n
$HowMenu = Read-Host -Prompt \"================ Welcome ================<\/p>\n
$option = Read-Host “Please select an option”<\/p>\n
switch ($ShowMenu){
\n1{Get-Process}
\n2{Get-Service}
\n3{
\n$Connection = Read-Host “Please write an IP adress or a domain name”}
\n4{
\n$num1 = Read-Host “Please write a number”
\nWrite-Host “Please wait …”
\nsleep -Seconds 1
\nWrite-Host “Your chosen number was $num1 and now it’s $([int]$num1 + [int]$num1)”}
\nQ{return}
\n}
\ndo
\n{
\n$Options = Read-Host “Please select an option”<\/p>\n
pause
\n}
\nuntil ($input -eq ‘Q’)q<\/p>\n
The issue lies that the loop focuses on question when initially asked and doesn’t break from it, even when writing “Q”. What am I doing wrong?<\/p>\n
All help is appreciated!<\/p>","upvoteCount":5,"answerCount":7,"datePublished":"2019-06-18T07:50:07.000Z","author":{"@type":"Person","name":"daphrack","url":"https://community.spiceworks.com/u/daphrack"},"acceptedAnswer":{"@type":"Answer","text":"
Hi,<\/p>\n
The first line that starts $HowMenu = Read-Host -Prompt \"… doesn’t need to be a variable taken from a prompt.<\/p>\n
(also, I think you meant $ShowMenu to be the variable name)<\/p>\n
So change that to whole line to Write-Host \"…<\/p>\n
And the line that starts switch ($ShowMenu){ should have been switch ($option){<\/p>\n
Also, you forgot the ping command but no bother.<\/p>\n
To loop the menu option after someone has selected 1,2,3 or 4 you could use a while loop…<\/p>","upvoteCount":2,"datePublished":"2019-06-18T08:21:28.000Z","url":"https://community.spiceworks.com/t/issue-with-do-until-loop-in-powershell/716861/2","author":{"@type":"Person","name":"jonsellors","url":"https://community.spiceworks.com/u/jonsellors"}},"suggestedAnswer":[{"@type":"Answer","text":"
Hello!
\nSo, I’m a beginner in Powershell and this may seem like an idiotic question but, I’m writing my first script and encountered an issue with the do/until loop.<\/p>\n
Here’s the script:<\/p>\n
Clear-Host<\/p>\n
$HowMenu = Read-Host -Prompt \"================ Welcome ================<\/p>\n
$option = Read-Host “Please select an option”<\/p>\n
switch ($ShowMenu){
\n1{Get-Process}
\n2{Get-Service}
\n3{
\n$Connection = Read-Host “Please write an IP adress or a domain name”}
\n4{
\n$num1 = Read-Host “Please write a number”
\nWrite-Host “Please wait …”
\nsleep -Seconds 1
\nWrite-Host “Your chosen number was $num1 and now it’s $([int]$num1 + [int]$num1)”}
\nQ{return}
\n}
\ndo
\n{
\n$Options = Read-Host “Please select an option”<\/p>\n
pause
\n}
\nuntil ($input -eq ‘Q’)q<\/p>\n
The issue lies that the loop focuses on question when initially asked and doesn’t break from it, even when writing “Q”. What am I doing wrong?<\/p>\n
All help is appreciated!<\/p>","upvoteCount":5,"datePublished":"2019-06-18T07:50:07.000Z","url":"https://community.spiceworks.com/t/issue-with-do-until-loop-in-powershell/716861/1","author":{"@type":"Person","name":"daphrack","url":"https://community.spiceworks.com/u/daphrack"}},{"@type":"Answer","text":"
Here’s the code using a “while loop”:<\/p>\n
$Option = \"not q\"\n\nWhile ($Option -ne \"q\"){\nClear-Host\n\nWrite-Host \"================ Welcome ================ \n\n1. List of processes on the system\n2. List of services on the system\n3. Send a ping\n4. Double a number\nQ. Quit\"\n\n$option = Read-Host \"Please select an option\" \n\nswitch ($option){\n 1{Get-Process}\n 2{Get-Service}\n 3{\n $Connection = Read-Host \"Please write an IP adress or a domain name\"\n #Do the ping\n Test-Connection $Connection}\n\n 4{\n $num1 = Read-Host \"Please write a number\"\n Write-Host \"Please wait ...\"\n sleep -Seconds 1\n Write-Host \"Your chosen number was $num1 and now it's $([int]$num1 + [int]$num1)\"}\n Q{return}\n}\n\n}\n\n<\/code><\/pre>","upvoteCount":2,"datePublished":"2019-06-18T08:27:34.000Z","url":"https://community.spiceworks.com/t/issue-with-do-until-loop-in-powershell/716861/3","author":{"@type":"Person","name":"jonsellors","url":"https://community.spiceworks.com/u/jonsellors"}},{"@type":"Answer","text":"Thank you so much! Since I’m new, I still get a little confused with the process… And thanks for the ping command, totally forgot to add it!<\/p>","upvoteCount":1,"datePublished":"2019-06-18T09:03:58.000Z","url":"https://community.spiceworks.com/t/issue-with-do-until-loop-in-powershell/716861/4","author":{"@type":"Person","name":"daphrack","url":"https://community.spiceworks.com/u/daphrack"}},{"@type":"Answer","text":"