首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >新用户powershell get-service

新用户powershell get-service
EN

Stack Overflow用户
提问于 2019-06-11 05:49:45
回答 1查看 36关注 0票数 0

这段代码有什么问题?

代码语言:javascript
复制
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`

为什么脚本不尊重用户的选择?为什么它会循环?脚本不能pinging通。我想让用户选择一个数字,然后任务完成,光标回到问题,请选择一个数字?因此,我不希望它在完成任务后进入下一个问题

EN

回答 1

Stack Overflow用户

发布于 2019-06-11 06:27:46

你的代码块有太多的错误,所以我只是重写了90%的代码,我相信你正在努力完成。

代码语言:javascript
复制
# 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')

你的问题在我看来。

代码语言:javascript
复制
# 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`
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56533968

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档