【问题标题】:new user powershell get-service新用户 powershell 获取服务
【发布时间】:2019-10-25 07:14:05
【问题描述】:

这段代码有什么问题?

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。 我希望用户选择一个数字然后任务完成并且光标回到问题请选择一个数字?很快 我不希望它完成任务后转到下一个问题

【问题讨论】:

  • 这是一道作业题吗?
  • 如果是家庭作业,请记住完成完整的哈佛风格引文和参考资料以获取所提供的帮助。你一定会得到额外的分数。

标签: powershell loops user-input ping


【解决方案1】:

你的代码块有很多错误,所以我只是用我相信你正在努力完成的内容重写了其中的 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 感到困惑,谢谢
  • 谢谢两位德鲁,我喜欢你提出问题的方式,它帮助我更好地理解了背后的逻辑。
猜你喜欢
  • 1970-01-01
  • 2017-10-23
  • 2020-08-03
  • 2022-10-18
  • 2014-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-21
相关资源
最近更新 更多