Hello!
So, 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.

Here’s the script:

Clear-Host

$HowMenu = Read-Host -Prompt "================ Welcome ================

  1. List of processes on the system
  2. List of services on the system
  3. Send a ping
  4. Double a number
    Q. Quit"

$option = Read-Host “Please select an option”

switch ($ShowMenu){
1{Get-Process}
2{Get-Service}
3{
$Connection = Read-Host “Please write an IP adress or a domain name”}
4{
$num1 = Read-Host “Please write a number”
Write-Host “Please wait …”
sleep -Seconds 1
Write-Host “Your chosen number was $num1 and now it’s $([int]$num1 + [int]$num1)”}
Q{return}
}
do
{
$Options = Read-Host “Please select an option”

pause
}
until ($input -eq ‘Q’)q

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?

All help is appreciated!

5 Spice ups

Hi,

The first line that starts $HowMenu = Read-Host -Prompt "… doesn’t need to be a variable taken from a prompt.

(also, I think you meant $ShowMenu to be the variable name)

So change that to whole line to Write-Host "…

And the line that starts switch ($ShowMenu){ should have been switch ($option){

Also, you forgot the ping command but no bother.

To loop the menu option after someone has selected 1,2,3 or 4 you could use a while loop…

2 Spice ups

Here’s the code using a “while loop”:

$Option = "not q"

While ($Option -ne "q"){
Clear-Host

Write-Host "================ Welcome ================ 

1. List of processes on the system
2. List of services on the system
3. Send a ping
4. Double a number
Q. Quit"

$option = Read-Host "Please select an option" 

switch ($option){
    1{Get-Process}
    2{Get-Service}
    3{
      $Connection = Read-Host "Please write an IP adress or a domain name"
      #Do the ping
      Test-Connection $Connection}

    4{
      $num1 = Read-Host "Please write a number"
      Write-Host "Please wait ..."
      sleep -Seconds 1
      Write-Host "Your chosen number was $num1 and now it's $([int]$num1 + [int]$num1)"}
    Q{return}
}

}

2 Spice ups

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!

1 Spice up

Glad to help, you’re welcome :slight_smile:

2 Spice ups

Both of you…

If you post code, please use the ‘Insert Code’ button. Please and thank you!

codebutton_small.png

@jonsellors @daphrack

1 Spice up

Will do Neally. I always used to forget, now I won’t :slight_smile:

Also, I’ve also edited my original post so that its correctly “inserted as code”.

1 Spice up