如何从switch语句中退出While循环

时间:2019-08-19 12:17:35

标签: powershell scripting

在PowerShell中,如何退出嵌套在while语句中的switch循环,而不在while块之后立即执行代码?我似乎无法弄清楚。到目前为止,我尝试过的所有事情都会导致该代码块被执行。

这就是我要完成的工作:

  1. 检查文件是否存在,并通知用户文件是否为 检测到。
  2. 每10秒钟再次检查一次,并通知用户
  3. 如果未检测到文件,则退出循环并切换,然后继续 使用步骤#2
  4. 如果30秒后仍检测到文件,则超时并退出 完全是脚本。

代码如下:

try {
    #Step 1
    $Prompt = <Some Notification Dialog with two buttons>
    switch ($Prompt){
        'YES' {
            # Display the Windows Control Panel

            #Wait for user to manually uninstall an application - which removes a file from the path we will check later.
            $Timeout = New-Timespan -Seconds 30
            $Stopwatch = [Dispatch.Stopwatch]::StartNew()

            while ($Stopwatch.elapsed -lt $Timeout) {
                if (Test-Path -Path "C:\SomeFile.exe" -PathType Leaf) {
                    Write-Host "The file is still there, remove it!"
                    return
                }
                Start-Sleep 10
            }

            #After timeout is reached, notify user and exit the script
            Write-Host "Timeout reached, exiting script"
            Exit-Script -ExitCode $mainExitCode #Variable is declared earlier in the script
        }
        'NO' {
            # Do something and exit script
        }
    }

    # Step 2
    # Code that does something here

    # Step 3
    # Code that does something here
} catch {
    # Error Handling Code Here
}

2 个答案:

答案 0 :(得分:3)

您可以使用带有标签的break来退出特定循环(将switch语句视为循环),请参见about_break

$a = 0
$test = 1
:test switch ($test) {
    1 {
        Write-Output 'Start'
        while ($a -lt 100)
        {
            Write-Output $a
            $a++
            if ($a -eq 5) {
                break test
            }
        }
        Write-Output 'End'
    }
}
Write-Output "EoS"

答案 1 :(得分:1)

在没有try / catch的情况下您看到的是吗?我有一个例外:Unable to find type [Dispatch.Stopwatch]。否则,退货对我来说还可以。

我认为您想要的是在开关外贴一个标签来打破?然后将执行步骤2和3。我更改了代码以使其成为易于管理的示例。提出问题时,这是更理想的选择。我不知道退出脚本是什么。

echo hi > file

#try {
    #Step 1
    $Prompt = '<Some Notification Dialog with two buttons>'
    $Prompt = 'yes'
:atswitch    switch ($Prompt){
        'YES' {
            'Display the Windows Control Panel'

            #Wait for user to manually uninstall an application - which removes a file from the path we will check later.
            $Timeout = New-Timespan -Seconds 30
            #$Stopwatch = [Dispatch.Stopwatch]::StartNew()

            while (1) {
                if (Test-Path -Path "file" -PathType Leaf) {
                    Write-Host "The file is still there, remove it!"
                    break atswitch
                }
                Start-Sleep 10
            }

            #After timeout is reached, notify user and exit the script
            Write-Host "Timeout reached, exiting script"
            'Exit-Script -ExitCode $mainExitCode #Variable is declared earlier in the script'
        }
        'NO' {
            'Do something and exit script'
        }
    }

    # Step 2
    'Code that does something here'

    # Step 3
    'Code that does something here2'
#} catch {
#    'Error Handling Code Here'
#}