解释 PowerShell 高级函数中的 ValueFromPipeline。
考虑以下示例,我们创建了一个高级函数来获取特定进程的信息,例如**进程名称**、**进程 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
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP