How to exit from ForEach-Object in PowerShell

前端 未结 9 1829
北海茫月
北海茫月 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:19

    Below is a suggested approach to Question #1 which I use if I wish to use the ForEach-Object cmdlet. It does not directly answer the question because it does not EXIT the pipeline. However, it may achieve the desired effect in Q#1. The only drawback an amateur like myself can see is when processing large pipeline iterations.

        $zStop = $false
        (97..122) | Where-Object {$zStop -eq $false} | ForEach-Object {
        $zNumeric = $_
        $zAlpha = [char]$zNumeric
        Write-Host -ForegroundColor Yellow ("{0,4} = {1}" -f ($zNumeric, $zAlpha))
        if ($zAlpha -eq "m") {$zStop = $true}
        }
        Write-Host -ForegroundColor Green "My PSVersion = 5.1.18362.145"
    

    I hope this is of use. Happy New Year to all.

提交回复
热议问题