Download Mastering-PowerShell
Transcript
functions can invoke other commands as well with predefined arguments. They are designed to
ensure that by simply typing a drive name you can switch to that drive just like you would when
using the older console:
# Actually, a function is being invoked here:
e:
Dir
Functions, Filters and the Pipeline
Can functions actually read and further process the results of other commands? They can, namely by
the pipeline, which PowerShell uses to connect more than one command to each other (see Chapter
5). In Chapter 5, you learned that the pipeline can command two modes: a slow sequential mode
and a rapid streaming mode. In which of the two modes the pipeline can operate really depends on
the statements used in the pipeline, and how you define your functions.
The Slow Sequential Mode: $input
In the simplest case, your function doesn't really support the pipeline. Your function is limited
merely to processing the results of the preceding pipeline command if the command has completed
its work. The results of the preceding command are always in the $input automatic variable. $input
is an array: depending on the circumstances, it can contain many elements, exactly one element, or
no element at all.
In the simplest case, a function will merely output the contents of $input again:
Function output
{
$input
}
# The function, when invoked alone,
# will return nothing because no pipeline
# results are available:
output
# If you create an array in the pipeline,
# the function will output the array:
1,2,3 | output
1
2
3
# The function is completely indifferent to
# which type of data is in the pipeline:
Dir | output
Directory: Microsoft.PowerShell.Core\FileSystem::
C:\Users\Tobias Weltner
Mode
LastWriteTime
Length Name
Table of Contents | About PowerShell Plus
274
Sponsors | Resources | © BBS Technologies