PowerShell 标准输出和重定向
PowerShell stdout and redirect
我想构建类似
的powershell管道
cmd | transform_a | stdout_and | transform_b | store_variable
^
|
copy input to next consumer and to console
我尝试使用 Tee-Object
但没有成功。我不想这样做
dir | select -last 5 | tee lastFiveLines | select -first 1
echo $lastFiveLines
虽然它有效。相反,我希望直接打印内容。
您可以尝试使用 foreach 循环和 Out-Default
或 Out-Host
来跳过管道的其余部分(主机无论如何都是默认输出),同时也将对象发送到管道中。示例:
Get-ChildItem |
Select-Object Name, FullName |
ForEach-Object {
#Send Name-value directly to console (default output)
$_.Name | Out-Default
#Send original object down the pipeline
$_
} |
Select-Object -ExpandProperty FullName | % { Start-sleep -Seconds 1; "Hello $_" }
您可以创建一个过滤器以便于重复使用。
#Bad filter-name, but fits the question.
filter stdout_and {
#Send Name-value directly to console (default output)
$_.Name | Out-Default
#Send original object down the pipeline
$_
}
Get-ChildItem |
Select-Object Name, FullName |
stdout_and |
Select-Object -ExpandProperty FullName | % { Start-sleep -Seconds 1; "Hello $_" }
我想构建类似
的powershell管道cmd | transform_a | stdout_and | transform_b | store_variable
^
|
copy input to next consumer and to console
我尝试使用 Tee-Object
但没有成功。我不想这样做
dir | select -last 5 | tee lastFiveLines | select -first 1
echo $lastFiveLines
虽然它有效。相反,我希望直接打印内容。
您可以尝试使用 foreach 循环和 Out-Default
或 Out-Host
来跳过管道的其余部分(主机无论如何都是默认输出),同时也将对象发送到管道中。示例:
Get-ChildItem |
Select-Object Name, FullName |
ForEach-Object {
#Send Name-value directly to console (default output)
$_.Name | Out-Default
#Send original object down the pipeline
$_
} |
Select-Object -ExpandProperty FullName | % { Start-sleep -Seconds 1; "Hello $_" }
您可以创建一个过滤器以便于重复使用。
#Bad filter-name, but fits the question.
filter stdout_and {
#Send Name-value directly to console (default output)
$_.Name | Out-Default
#Send original object down the pipeline
$_
}
Get-ChildItem |
Select-Object Name, FullName |
stdout_and |
Select-Object -ExpandProperty FullName | % { Start-sleep -Seconds 1; "Hello $_" }