O'Reilly logo

Monad (AKA PowerShell) by Andy Oakley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Transform Objects as They Pass Through the Pipeline

Functions remain one of the most versatile aspects of the MSH language. Their use in both command-line and pipeline situations makes them very flexible. However, there are times when we're creating solely for pipeline use, and it's more convenient to run a script block on each object in the pipeline than getting them delivered in one large chunk in the $input variable. Let's say we're going through each line of a 400 MB daily IIS logfile to figure out the set of requested URLs that resulted in a 404 error message. Loading all 400 MB into memory probably isn't the best approach; we would do better to create a small script block that could match a 404 line in the log and record the requested URL into a variable.

To accommodate these cases, MSH offers a special type of function, called a filter, that is designed to be placed into the pipeline and used to inspect, modify, or augment data as it passes between processes.

Let's create a couple of filters to see how they work.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required