Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to terminate or stop a PowerShell pipeline from within a filter

I have written a simple PowerShell filter that pushes the current object down the pipeline if its date is between the specified begin and end date. The objects coming down the pipeline are always in ascending date order so as soon as the date exceeds the specified end date I know my work is done and I would like to let tell the pipeline that the upstream commands can abandon their work so that the pipeline can finish its work. I am reading some very large log files and I will frequently want to examine just a portion of the log. I am pretty sure this is not possible but I wanted to ask to be sure.

like image 626
Dan Finucane Avatar asked Nov 10 '08 17:11

Dan Finucane


People also ask

How do I stop a PowerShell script from running?

You can interrupt and stop a PowerShell command while it is running by pressing Control-C. A script can be stopped with the command exit. This will also close the PowerShell console.

What does filter do in PowerShell?

Introduction to Filtering in PowerShell. Filtering refers to the process of restricting the output of a cmdlet or a statement based on certain conditions.

What is stop in PowerShell?

PowerShell Stop-Computer Cmdlet. If you want to use PowerShell to shutdown your machine, choose the Stop-Computer cmdlet. The PowerShell Stop-Computer cmdlet is very similar to the operating system's built-in shutdown command. A likely scenario is that you wish to down a remote server.


2 Answers

It is possible to break a pipeline with anything that would otherwise break an outside loop or halt script execution altogether (like throwing an exception). The solution then is to wrap the pipeline in a loop that you can break if you need to stop the pipeline. For example, the below code will return the first item from the pipeline and then break the pipeline by breaking the outside do-while loop:

do {
    Get-ChildItem|% { $_;break }
} while ($false)

This functionality can be wrapped into a function like this, where the last line accomplishes the same thing as above:

function Breakable-Pipeline([ScriptBlock]$ScriptBlock) {
    do {
        . $ScriptBlock
    } while ($false)
}

Breakable-Pipeline { Get-ChildItem|% { $_;break } }
like image 179
Maximum Cookie Avatar answered Sep 19 '22 19:09

Maximum Cookie


It is not possible to stop an upstream command from a downstream command.. it will continue to filter out objects that do not match your criteria, but the first command will process everything it was set to process.

The workaround will be to do more filtering on the upstream cmdlet or function/filter. Working with log files makes it a bit more comoplicated, but perhaps using Select-String and a regular expression to filter out the undesired dates might work for you.

Unless you know how many lines you want to take and from where, the whole file will be read to check for the pattern.

like image 25
Steven Murawski Avatar answered Sep 17 '22 19:09

Steven Murawski