Select-Object如何在PowerShell v3中停止管道?

Ryn*_*ant 10 powershell powershell-3.0

在PowerShell v2中,以下行:

1..3| foreach { Write-Host "Value : $_"; $_ }| select -First 1
Run Code Online (Sandbox Code Playgroud)

会显示:

Value : 1
1
Value : 2
Value : 3
Run Code Online (Sandbox Code Playgroud)

因为所有元素都被推下了管道.但是,在v3中,上面的行仅显示:

Value : 1
1
Run Code Online (Sandbox Code Playgroud)

管道在2和3发送之前停止Foreach-Object(注意:-Wait开关Select-Object允许所有元素到达foreach块).

如何Select-Object停止管道,我现在可以从一个foreach或从我自己的函数中停止管道?

编辑:我知道我可以在do ... while循环中包装一个管道并继续管道.我还发现在v3中我可以做这样的事情(它在v2中不起作用):

function Start-Enumerate ($array) {
    do{ $array } while($false)  
}

Start-Enumerate (1..3)| foreach {if($_ -ge 2){break};$_}; 'V2 Will Not Get Here'
Run Code Online (Sandbox Code Playgroud)

但是Select-Object不需要这些技术中的任何一种,所以我希望有一种方法可以从管道中的单个点停止管道.

Sha*_*evy 3

查看这篇文章,了解如何取消管道:
http://powershell.com/cs/blogs/tobias/archive/2010/01/01/cancelling-a-pipeline.aspx

在 PowerShell 3.0 中,这是引擎的改进。从 CTP1 示例文件夹 ('\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
Run Code Online (Sandbox Code Playgroud)