解释 PowerShell 高级函数中的 ValueFromPipeline。
powershellmicrosoft technologiessoftware & coding更新于 2026/1/7 8:07:17
考虑以下示例,我们创建了高级函数来获取特定的进程信息,例如进程名称、进程 ID (PID)、启动时间、响应状态等。
function Get-ProcessInformation{
[cmdletbinding()]
param(
[Parameter(Mandatory=$True)]
[string[]]$name
)
Begin{
Write-Verbose "Program started"
}
Process{
Write-Verbose "Extracting Process Inforamtion" Get-Process $name | Select Name, ID, StartTime, Responding | ft - AutoSize
}
End{
Write-Verbose "Function ends here"
}
}
以上是高级函数示例。运行上述命令后,它将返回所选表的进程信息。
输出
PS C:\WINDOWS\system32> Get-ProcessInformation -name Chrome Name Id StartTime Responding ---- -- --------- ---------- chrome 1284 24-04-2020 21:29:25 True chrome 1316 24-04-2020 21:29:37 True chrome 2004 25-04-2020 17:12:40 True chrome 2676 26-04-2020 10:59:37 True chrome 3096 24-04-2020 23:39:00 True chrome 4660 25-04-2020 22:43:34 True chrome 6040 24-04-2020 21:29:31 True chrome 6548 26-04-2020 10:59:30 True chrome 6592 26-04-2020 10:59:39 True chrome 6644 25-04-2020 16:41:59 True chrome 6780 26-04-2020 09:53:33 True chrome 7392 25-04-2020 11:47:22 True chrome 10540 26-04-2020 10:56:49 True chrome 11288 24-04-2020 21:33:05 True chrome 12088 26-04-2020 10:59:48 True chrome 12100 24-04-2020 21:29:38 True chrome 13112 26-04-2020 10:59:48 True chrome 13460 26-04-2020 10:59:45 True
现在我们将上面创建的函数作为管道输出传递并检查它是否有效?
PS C:\WINDOWS\system32> "Chrome" | Get-ProcessInformation cmdlet Get-ProcessInformation at command pipeline position 1 Supply values for the following parameters: name: Get-ProcessInformation : Cannot bind argument to parameter 'name' because it is an empty string. At line:1 char:12 + "Chrome" | Get-ProcessInformation + ~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Get-ProcessInformation], Param eterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAl lowed,Get-ProcessInformation
此方法无效,因为此函数不兼容用作管道输入。它不接受 "chrome" 作为输入,并且由于 Mandatory 参数的存在,它会请求 Name 变量的值。我们只需添加 ValueFromPipeline() 参数即可。由于我们已经在函数中使用了 Mandatory 参数,因此只需再添加一个管道参数即可,如下所示。
function Get-ProcessInformation{
[cmdletbinding()]
param(
[Parameter(Mandatory=$True,ValuefromPipeline=$True)]
[string[]]$name
)
Begin{
Write-Verbose "Program started"
}
Process{
Write-Verbose "Extracting Process Inforamtion" Get-Process $name | Select Name, ID, StartTime, Responding | ft - AutoSize
}
End{
Write-Verbose "Function ends here"
}
}
现在,让我们检查一下它是否有效。
PS C:\WINDOWS\system32> "Chrome" | Get-ProcessInformation Name Id StartTime Responding ---- -- --------- ---------- chrome 1284 24-04-2020 21:29:25 True chrome 1316 24-04-2020 21:29:37 True chrome 2004 25-04-2020 17:12:40 True chrome 2676 26-04-2020 10:59:37 True chrome 3096 24-04-2020 23:39:00 True chrome 4660 25-04-2020 22:43:34 True chrome 6040 24-04-2020 21:29:31 True chrome 6548 26-04-2020 10:59:30 True chrome 6592 26-04-2020 10:59:39 True chrome 6644 25-04-2020 16:41:59 True chrome 6780 26-04-2020 09:53:33 True chrome 7392 25-04-2020 11:47:22 True chrome 10540 26-04-2020 10:56:49 True chrome 11288 24-04-2020 21:33:05 True chrome 12088 26-04-2020 10:59:48 True
是的,它有效。现在,如果我们提供两个现有的进程名称作为输入,它将无法提供所需的结果,因为我们在参数部分声明的字符串不是数组,而是一个字符串变量。我们现在将其声明为字符串数组。
function Get-ProcessInformation{
[cmdletbinding()]
param(
[Parameter(Mandatory=$True,ValuefromPipeline=$True)]
[string[]]$name
)
Begin{
Write-Verbose "Program started"
}
Process{
Write-Verbose "Extracting Process Inforamtion" Get-Process $name | Select Name, ID, StartTime, Responding | ft - AutoSize
}
End{
Write-Verbose "Function ends here"
}
}
现在检查输出。
PS C:\WINDOWS\system32> "Chrome","SkypeApp" | Get-ProcessInformation Name Id StartTime Responding ---- -- --------- ---------- chrome 1284 24-04-2020 21:29:25 True chrome 1316 24-04-2020 21:29:37 True chrome 2004 25-04-2020 17:12:40 True chrome 2676 26-04-2020 10:59:37 True chrome 3096 24-04-2020 23:39:00 True chrome 4660 25-04-2020 22:43:34 True chrome 6040 24-04-2020 21:29:31 True chrome 6548 26-04-2020 10:59:30 True chrome 6592 26-04-2020 10:59:39 True chrome 6644 25-04-2020 16:41:59 True chrome 6780 26-04-2020 09:53:33 True Name Id StartTime Responding ---- -- --------- ---------- SkypeApp 2552 21-04-2020 18:31:02 True
这里,我们获取了所需的输出,但对于每个新进程,我们获取的是标头,而我们不需要它。我们需要合并后的输出,因此我们将在此处使用 foreach 循环,并使用 PSCustomObject 来收集数据。
function Get-ProcessInformation{
[cmdletbinding()]
param(
[Parameter(Mandatory=$True,ValuefromPipeline=$True)]
[string]$name
)
Begin{
Write-Verbose "Program started"
$out = @()
}
Process{
Write-Verbose "Extracting Process Inforamtion" $procs = Get-Process $name
foreach($proc in $procs){
$out += [PSCustomObject]@{
"Process_Name" = $proc.Name
"PID" = $proc.Id
"Start_Time" = $proc.StartTime
"Responding?" = $proc.Responding
}
}
}
End{
$out
Write-Verbose "Function ends here"
}
}
当您检查上面的输出时,输出已组合并且符合预期。
PS C:\WINDOWS\system32> "Chrome","SkypeApp" | Get-ProcessInformation Process_Name PID Start_Time Responding? ------------ --- ---------- ----------- chrome 1284 24-04-2020 21:29:25 True chrome 1316 24-04-2020 21:29:37 True chrome 2004 25-04-2020 17:12:40 True chrome 2676 26-04-2020 10:59:37 True chrome 3096 24-04-2020 23:39:00 True chrome 4660 25-04-2020 22:43:34 True chrome 6040 24-04-2020 21:29:31 True chrome 6548 26-04-2020 10:59:30 True chrome 6592 26-04-2020 10:59:39 True chrome 6644 25-04-2020 16:41:59 True chrome 6780 26-04-2020 09:53:33 True chrome 7392 25-04-2020 11:47:22 True chrome 10540 26-04-2020 10:56:49 True chrome 11288 24-04-2020 21:33:05 True chrome 12088 26-04-2020 10:59:48 True chrome 12100 24-04-2020 21:29:38 True SkypeApp 2552 21-04-2020 18:31:02 True
相关文章
打印
下一节:如何使用 PowerShell 获取进程的端口号? ❯❮ 上一节:解释 PowerShell 高级函数中的 AllowEmptyString() 和 AllowEmptyCollection()。

