新用户Powershell获取服务

时间:2019-06-10 21:49:44

标签: 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 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通。  我希望用户选择一个数字,然后任务完成,光标又回到问题所在,请选择一个数字?依此类推 我不希望它在完成任务后转到下一个问题

1 个答案:

答案 0 :(得分:2)

您的代码块有很多错误,因此我只将90%的代码重写为我认为您要完成的工作。

WITH CTE AS (
 SELECT *, 
 row_number() over (partition by VehicleID, ActivityDateTime order by (select null)) as rownum
 from #ActivityLog
)
UPDATE CTE SET ActivityDateTime = DATEADD(millisecond,(rownum-1)*3, ActivityDateTime)
WHERE rownum>1;

您看到的问题。

# 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')