новый пользователь powershell get-service - PullRequest
0 голосов
/ 11 июня 2019

Что не так с этим кодом?

Clear-Host

$num1 = Read-Host "Please choose"

1 = service
2 = process
3 = pinging
4 = multiplying a number

$num1 = Read-Host " Please enter a number for service "
Snumber = 1
Get-Service

$num2 = Read-Host " Please enter a number for process"
$Number = 2
Get-Process

$num3 = Read-Host " Please enter a number to ping"
$Number = 3
$ComputerName = Read-Host "enter the FQDN of the target computer"
Test-Connection $ComputerName FQDN

$num4 = Read-Host " Please enter a number for double the number"
$Number = 4
$num4 = Read-Host "Pleas enter number 5" 
Write_Host "Your original number was 5, now it's 10"`enter code here`

почему скрипт не учитывает выбор пользователя?почему это зацикливание?Скрипт не пингуется.Я хочу, чтобы пользователь выбрал один номер, после чего задача будет выполнена, и курсор вернется к вопросу. Выберите номер?и так далее, я не хочу переходить к следующему вопросу после завершения задания

1 Ответ

2 голосов
/ 11 июня 2019

В вашем кодовом блоке так много неправильного, поэтому я просто переписал 90% того, что, как я полагаю, вы пытаетесь выполнить.

# Sets up the visual choices for the user
Function Choose-Selection {
    Clear-Host

    Write-Host "1: Service"
    Write-Host "2: Process"
    Write-Host "3: Pinging"
    Write-Host "4: Multiplying a number"
    Write-Host "Q: Quit" -ForegroundColor Red 
}

# Displays those choices
Choose-Selection
# Enters loop
Do{
    # Checks for a selection from the user
    $selection = Read-Host "Please make a selection"
    # Switches take the input from the user and runs code based on the switch
    Switch($selection) {
        '1' {
            $num1 = Read-Host " Please enter a number for service"
            (Get-Service)[$num1]
            Sleep -Seconds 5
            Choose-Selection
        } '2' {
            $num2 = Read-Host " Please enter a number for process"
            (Get-Process)[$num2]
            Choose-Selection
        } '3' {
            $ComputerName = Read-Host "enter the FQDN of the target computer"
            Test-Connection -ComputerName $ComputerName
            Sleep -Seconds 5
            Choose-Selection
        } '4' {
            $num4 = Read-Host " Please enter a number for double the number"
            # Checks to see if input is an int. If not an int, terminates script
            If($num4 -match '^[0-9]+$') {
                "Your original number was $num4, now it's $([int]$num4*2)"
            } Else {
                Throw "You have not entered a valid number. Menu terminated."
            }
            Sleep -Seconds 5
            Choose-Selection
        } 'q'  { 

            'Leaving Menu...'
            Return
        }
    }
 }

Until($response -eq 'Q')

Твои проблемы, как я их вижу.

# This section does nothing except cause errors
1 = service
2 = process
3 = pinging
4 = multiplying a number

# What is SNumber? Variables need a $ in front of them
$num1 = Read-Host " Please enter a number for service "
Snumber = 1
# What are you doing with the number they give you? You are retrieving all services
Get-Service

$num2 = Read-Host " Please enter a number for process"
$Number = 2
# What are you doing with the number they give you? You are retrieving all Processed
Get-Process

$num3 = Read-Host " Please enter a number to ping"
# What is this variable used for?
$Number = 3
$ComputerName = Read-Host "enter the FQDN of the target computer"
Test-Connection $ComputerName FQDN

$num4 = Read-Host " Please enter a number for double the number"
# What is this variable used for?
$Number = 4
$num4 = Read-Host "Pleas enter number 5" 
# What if I chose the number 200, it will still say I chose 5
# Well it would if it didn't error out. The command is Write-Host
Write_Host "Your original number was 5, now it's 10"`enter code here`
...