How to exit from ForEach-Object in PowerShell

前端 未结 9 1791
北海茫月
北海茫月 2020-11-27 20:30

I have the following code:

$project.PropertyGroup | Foreach-Object {
    if($_.GetAttribute(\'Condition\').Trim() -eq $propertyGroupConditionName.Trim()) {
           


        
9条回答
  •  [愿得一人]
    2020-11-27 21:29

    I found this question while looking for a way to have fine grained flow control to break from a specific block of code. The solution I settled on wasn't mentioned...

    Using labels with the break keyword

    From: about_break

    A Break statement can include a label that lets you exit embedded loops. A label can specify any loop keyword, such as Foreach, For, or While, in a script.

    Here's a simple example

    :myLabel for($i = 1; $i -le 2; $i++) {
            Write-Host "Iteration: $i"
            break myLabel
    }
    
    Write-Host "After for loop"
    
    # Results:
    # Iteration: 1
    # After for loop
    

    And then a more complicated example that shows the results with nested labels and breaking each one.

    :outerLabel for($outer = 1; $outer -le 2; $outer++) {
    
        :innerLabel for($inner = 1; $inner -le 2; $inner++) {
            Write-Host "Outer: $outer / Inner: $inner"
            #break innerLabel
            #break outerLabel
        }
    
        Write-Host "After Inner Loop"
    }
    
    Write-Host "After Outer Loop"
    
    # Both breaks commented out
    # Outer: 1 / Inner: 1
    # Outer: 1 / Inner: 2
    # After Inner Loop
    # Outer: 2 / Inner: 1
    # Outer: 2 / Inner: 2
    # After Inner Loop
    # After Outer Loop
    
    # break innerLabel Results
    # Outer: 1 / Inner: 1
    # After Inner Loop
    # Outer: 2 / Inner: 1
    # After Inner Loop
    # After Outer Loop
    
    # break outerLabel Results
    # Outer: 1 / Inner: 1
    # After Outer Loop
    

    You can also adapt it to work in other situations by wrapping blocks of code in loops that will only execute once.

    :myLabel do {
        1..2 | % {
    
            Write-Host "Iteration: $_"
            break myLabel
    
        }
    } while ($false)
    
    Write-Host "After do while loop"
    
    # Results:
    # Iteration: 1
    # After do while loop
    

提交回复
热议问题