Unlike POSIX-compatible shells such as bash
, PowerShell does not automatically relay pipeline input received by a given script or function to commands you call from inside that script or function.
To do so, you must use the automatic $input
variable[1]:
For instance:
function foo {
# Relay the pipeline input received by this function
# to an external program, using `cat` as an example.
$input | & /bin/cat -n # append $args to pass options through
}
'MyString' | foo
Note that &
is optional in this case, because /bin/cat
is an unquoted, literal path - see this answer for when &
is required.
The output (on a Unix-like platform) is: 1 MyString
, showing that the cat
utility received the foo
function's own pipeline input via its stdin, thanks to use of $input
.
Without piping $input
to cat
, the latter would receive no stdin input at all (and in the case of cat
would block, waiting for interactive input).
If you want to support passing filename arguments too - in lieu of pipeline input - more work is needed:
function foo {
if ($MyInvocation.ExpectingInput) { # Pipeline input present
# Relay the pipeline input received by this function
# to an external program, using `cat` as an example.
$input | /bin/cat -n $args
} else { # NO pipeline input.
/bin/cat -n $args
}
}
'MyString' | foo # pipeline input
foo file.txt # filename argument
Note:
Only non-advanced functions and scripts can make use of the $input
variable, and its use implies that all input piped to the enclosing function/script is collected in full, up front, before sending it on begins.
To truly stream a script / function's own pipeline input to a single invocation of an external program - i.e. to relay the input as it is being received by the script / function - requires direct use of .NET APIs, namely System.Diagnostics.ProcessStartInfo
and System.Diagnostics.Process
.
[1] Similarly, $input
is required to access data piped to a PowerShell command from outside PowerShell, via PowerShell's CLI; e.g., from bash
:
echo hi | pwsh -c '$input | ForEach-Object { "[{0}]" -f $_ }'
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…