Stopping the Pipeline

Usually, once a pipeline runs, you cannot stop it prematurely, even if you already received the information you were seeking. Simply use this filter to stop a pipeline:

filter Stop-Pipeline([scriptblock]$condition = {$true}) {
$_
if (& $condition) {continue}
}

Now, to stop a pipeline, use it like this:

Get-EventLog Application | Stop-Pipeline { $_.InstanceID -gt 10000}

This will get you all Application events until you find one with an InstanceID of greater than 10,000. The pipeline exists once it is found.

If you'd like to use this technique inside a script, you will need to make sure Stop-Pipeline does not stop your entire script. You can do that by embedding the pipeline inside a dummy loop:

$result = do {
Get-EventLog Application | Stop-Pipeline { $_.InstanceID -gt 10000}
} while ($false)

Twitter This Tip! ReTweet this Tip!


Posted Mar 12 2010, 08:00 AM by ps1

Comments

Don T wrote re: Stopping the Pipeline
on 06-08-2011 4:51 PM

I did this

filter Stop-Pipeline([scriptblock]$condition={$true}) {

$_

if (& $condition ) {Throw (New-Object System.Management.Automation.PipelineStoppedException)}

}

$MyResult = do {

1..254 | ForEach -Process `

{ WmiObject -Class Win32_PingStatus -Filter ("Address='10.121.0." + $_ + "'")`

-ComputerName . } `

| Select-Object Address, StatusCode | Where-Object { $_.StatusCode -ne 0 } | Stop-Pipeline { $_.StatusCode -ne 0} | Out-Host

} while ($false)

Was hoping $MyResult would have the first available IP address  that displays on the Host but it is $null

Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.