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

前端 未结 3 1035
温柔的废话
温柔的废话 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:15

    Check this post on how you can cancel a pipeline:
    http://powershell.com/cs/blogs/tobias/archive/2010/01/01/cancelling-a-pipeline.aspx

    In PowerShell 3.0 it's an engine improvement. From the CTP1 samples folder ('\Engines Demos\Misc\ConnectBugFixes.ps1'):

    # Connect Bug 332685
    # Select-Object optimization
    # Submitted by Shay Levi
    # Connect Suggestion 286219
    # PSV2: Lazy pipeline - ability for cmdlets to say "NO MORE"
    # Submitted by Karl Prosser
    
    # Stop the pipeline once the objects have been selected
    # Useful for commands that return a lot of objects, like dealing with the event log
    
    # In PS 2.0, this took a long time even though we only wanted the first 10 events
    Start-Process powershell.exe -Args '-Version 2 -NoExit -Command Get-WinEvent | Select-Object -First 10'
    
    # In PS 3.0, the pipeline stops after retrieving the first 10 objects
    Get-WinEvent | Select-Object -First 10
    

提交回复
热议问题