显示管道中的最后一个对象
Show Last Object From Pipline
我有一个 PowerShell 函数 (Out()
)。当我想得到结果时,它会从管道中取出最后一个对象。例如:我想显示 (gps
):
中的所有对象
function Out() {
[CmdletBinding()]
[Alias()]
Param(
[Parameter(Mandatory=$false,
ValueFromPipeline=$true,
Position=0)]
$Out
)
$Out
}
结果:
PS C:\> gps | Out
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName
------- ------ ----- ----- ------ -- -- -----------
505 27 7796 6220 0.78 13160 0 wmpnetwk
将输出放在 Process {}
块中:
function Out() {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$false, ValueFromPipeline=$true, Position=0)]
$Out
)
Process {
$Out
}
}
为从管道接收到的每个对象调用该块。
Process
This block is used to provide record-by-record processing for the function. This block might be used any number of times, or not at all, depending on the input to the function. For example, if the function is the first command in the pipeline, the Process block will be used one time. If the function is not the first command in the pipeline, the Process
block is used one time for every input that the function receives from the pipeline. If there is no pipeline input, the Process
block is not used.
This block must be defined if a function parameter is set to accept pipeline input. If this block is not defined and the parameter accepts input from the pipeline, the function will miss the values that are passed to the function through the pipeline.
我有一个 PowerShell 函数 (Out()
)。当我想得到结果时,它会从管道中取出最后一个对象。例如:我想显示 (gps
):
function Out() {
[CmdletBinding()]
[Alias()]
Param(
[Parameter(Mandatory=$false,
ValueFromPipeline=$true,
Position=0)]
$Out
)
$Out
}
结果:
PS C:\> gps | Out Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName ------- ------ ----- ----- ------ -- -- ----------- 505 27 7796 6220 0.78 13160 0 wmpnetwk
将输出放在 Process {}
块中:
function Out() {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$false, ValueFromPipeline=$true, Position=0)]
$Out
)
Process {
$Out
}
}
为从管道接收到的每个对象调用该块。
Process
This block is used to provide record-by-record processing for the function. This block might be used any number of times, or not at all, depending on the input to the function. For example, if the function is the first command in the pipeline, the Process block will be used one time. If the function is not the first command in the pipeline, the
Process
block is used one time for every input that the function receives from the pipeline. If there is no pipeline input, theProcess
block is not used.This block must be defined if a function parameter is set to accept pipeline input. If this block is not defined and the parameter accepts input from the pipeline, the function will miss the values that are passed to the function through the pipeline.