How does Select-Object stop the pipeline in PowerShell v3?

前端 未结 3 1037
温柔的废话
温柔的废话 2020-12-15 10:44

In PowerShell v2, the following line:

1..3| foreach { Write-Host \"Value : $_\"; $_ }| select -First 1

Would display:

Value         


        
3条回答
  •  醉酒成梦
    2020-12-15 11:11

    I know that throwing a PipelineStoppedException stops the pipeline. The following example will simulate what you see with Select -first 1 in v3.0, in v2.0:

    filter Select-Improved($first) {
        begin{
            $count = 0
        }
        process{
            $_
            $count++
            if($count -ge $first){throw (new-object System.Management.Automation.PipelineStoppedException)}
        }
    }
    
    trap{continue}
    1..3| foreach { Write-Host "Value : $_"; $_ }| Select-Improved -first 1
    write-host "after"
    

提交回复
热议问题