Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
新用户powershell获取服务_Powershell_Loops_User Input_Ping - Fatal编程技术网

新用户powershell获取服务

新用户powershell获取服务,powershell,loops,user-input,ping,Powershell,Loops,User Input,Ping,这个代码有什么问题 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 proc

这个代码有什么问题

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`
为什么脚本不尊重用户的选择? 为什么会循环? 脚本没有ping。 我希望用户选择一个数字,然后任务完成,光标回到问题请选择一个数字?等等
我不想让它在完成任务后进入下一个问题

您的代码块有太多错误,所以我只是用我认为您正在尝试完成的内容重新编写了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`

你的代码块有太多错误,所以我只是用我相信你正在努力实现的东西重新编写了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`

这是一个家庭作业问题吗?如果是家庭作业,请记住填写完整的哈佛式引文和参考资料,以获得所提供的帮助。你肯定会得到额外的分数。这是一个家庭作业问题吗?如果是家庭作业,请记住填写完整的哈佛式引用和参考,以获得所提供的帮助。你肯定会得到额外的分数。我正在学习powershell,非常感谢你的讲解。我仍然对cmdlet感到困惑谢谢你们两位Drew我喜欢你们提出问题的方式它帮助我更好地理解了背后的逻辑。我正在学习powershell,我真的很感谢你们的解释。我仍然对cmdlet感到困惑谢谢你们两个我喜欢你们提出问题的方式它帮助我更好地理解了背后的逻辑。