考虑下面的示例,我们创建了Advanced函数来获取特定的过程信息,例如Process Name ,Process ID(PID),Start Time,Responding status等。
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
是的,它有效。现在,如果我们提供两个现有的进程名称作为输入怎么办,它将不会提供所需的结果,因为我们在参数部分声明的字符串不是数组,而是单个字符串变量。我们现在将其声明为String数组。
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