添加用于处理管道输入的参数

cmdlet 的输入源之一是管道上源自上游 cmdlet 的对象。 本部分介绍如何将参数添加到 Get-Proc cmdlet (创建第一个 cmdlet) ,以便 cmdlet 可以处理管道对象。

此Get-Proc cmdlet 使用一个参数,该参数接受来自管道对象的输入,根据提供的名称从本地计算机检索进程信息,然后在命令行中显示有关进程 Name 的信息。

定义 Cmdlet 类

cmdlet 创建的第一步是始终命名 cmdlet 并声明实现 cmdlet 的 .NET 类。 此 cmdlet 检索进程信息,因此此处选择的谓词名称为"Get"。 (几乎任何能够检索信息的 cmdlet 都可以处理命令行输入。) 有关已批准的 cmdlet 谓词的信息,请参阅 Cmdlet 谓词名称

下面是此 cmdlet Get-Proc的定义。 创建第一个 Cmdlet 中提供此定义的详细信息

[Cmdlet(VerbsCommon.Get, "proc")]
public class GetProcCommand : Cmdlet
<Cmdlet(VerbsCommon.Get, "Proc")> _
Public Class GetProcCommand
    Inherits Cmdlet

定义管道中的输入

本部分介绍如何为 cmdlet 定义管道中的输入。 此Get-Proc cmdlet 定义表示 参数的属性,如添加处理命令行 Name 输入 的参数中所述。 (有关声明 parameters.) 的一般信息,请参阅该主题

但是,当 cmdlet 需要处理管道输入时,该 cmdlet 必须将其参数绑定到输入值,Windows PowerShell运行时。 为此,必须添加 关键字或将 关键字 ValueFromPipeline 添加到 ValueFromPipelineByProperty System.Management.Automation.Parameterattribute 属性声明。 如果 ValueFromPipeline cmdlet 访问完整的输入对象,请指定 关键字。 如果 ValueFromPipelineByProperty cmdlet 仅访问 对象的属性,请指定 。

下面是接受管道输入的此 Get-Proc Name cmdlet 的参数声明。

[Parameter(
   Position = 0,
   ValueFromPipeline = true,
   ValueFromPipelineByPropertyName = true)]
[ValidateNotNullOrEmpty]
public string[] Name
{
   get { return this.processNames; }
   set { this.processNames = value; }
}
<Parameter(Position:=0, ValueFromPipeline:=True, _
ValueFromPipelineByPropertyName:=True), ValidateNotNullOrEmpty()> _
Public Property Name() As String()
    Get
        Return processNames
    End Get

    Set(ByVal value As String())
        processNames = value
    End Set

End Property

上一个声明将 关键字设置为 ,以便 Windows PowerShell 运行时将参数绑定到传入对象(如果该对象与 参数的类型相同,或者如果可以强制转换为同一 ValueFromPipeline true 类型)。 ValueFromPipelineByPropertyName关键字也设置为 true ,以便Windows PowerShell运行时检查传入对象中的 Name 属性。 如果传入对象具有此类属性,则运行时将参数 Name 绑定到 Name 传入对象的 属性。

备注

参数的 ValueFromPipeline attribute 关键字设置优先于 关键字 ValueFromPipelineByPropertyName 的设置。

重写输入处理方法

如果 cmdlet 要处理管道输入,则它需要重写相应的输入处理方法。 创建第一个 Cmdlet 中引入了基本的输入处理方法

此Get-Proc cmdlet 重写 System.Management.Automation.Cmdlet.ProcessRecord 方法以处理用户或脚本 Name 提供的参数输入。 如果没有提供名称,此方法将获取每个请求的进程名称或所有进程的进程。 请注意,在 System.Management.Automation.Cmdlet.ProcessRecord中,对 WriteObject (System.Object,System.Boolean) 的调用是输出机制,用于将输出对象发送到管道。 此调用的第二个参数 设置为 ,以告知 Windows PowerShell 运行时枚举进程对象数组,并一次将一个进程 enumerateCollection true 写入命令行。

protected override void ProcessRecord()
{
  // If no process names are passed to the cmdlet, get all processes.
  if (processNames == null)
  {
      // Write the processes to the pipeline making them available
      // to the next cmdlet. The second argument of this call tells
      // PowerShell to enumerate the array, and send one process at a
      // time to the pipeline.
      WriteObject(Process.GetProcesses(), true);
  }
  else
  {
    // If process names are passed to the cmdlet, get and write
    // the associated processes.
    foreach (string name in processNames)
    {
      WriteObject(Process.GetProcessesByName(name), true);
    } // End foreach (string name...).
  }
}
Protected Overrides Sub ProcessRecord()
    Dim processes As Process()

    '/ If no process names are passed to the cmdlet, get all processes.
    If processNames Is Nothing Then
        processes = Process.GetProcesses()
    Else

        '/ If process names are specified, write the processes to the
        '/ pipeline to display them or make them available to the next cmdlet.
        For Each name As String In processNames
            '/ The second parameter of this call tells PowerShell to enumerate the
            '/ array, and send one process at a time to the pipeline.
            WriteObject(Process.GetProcessesByName(name), True)
        Next
    End If

End Sub 'ProcessRecord

代码示例

有关完整的 C# 示例代码,请参阅 GetProcessSample03 示例

定义对象类型和格式设置

Windows PowerShell .Net 对象在 cmdlet 之间传递信息。 因此,cmdlet 可能需要定义自己的类型,或者 cmdlet 可能需要扩展另一个 cmdlet 提供的现有类型。 有关定义新类型或扩展现有类型的信息,请参阅 扩展对象类型和格式设置。

生成 Cmdlet

实现 cmdlet 后,必须通过Windows PowerShell管理单元Windows PowerShell注册该 cmdlet。 有关注册 cmdlet 的信息,请参阅 如何注册 Cmdlet、提供程序和主机应用程序

测试 Cmdlet

当 cmdlet 已注册到 Windows PowerShell,请通过命令行运行它来测试它。 例如,测试示例 cmdlet 的代码。 有关从命令行使用 cmdlet 的信息,请参阅入门cmdlet Windows PowerShell。

  • 在Windows PowerShell提示符下,输入以下命令以通过管道检索进程名称。

    PS> type ProcessNames | get-proc
    

    将显示以下输出。

    Handles  NPM(K)  PM(K)   WS(K)  VS(M)  CPU(s)    Id  ProcessName
    -------  ------  -----   ----- -----   ------    --  -----------
        809      21  40856    4448    147    9.50  2288  iexplore
        737      21  26036   16348    144   22.03  3860  iexplore
         39       2   1024     388     30    0.08  3396  notepad
       3927      62  71836   26984    467  195.19  1848  OUTLOOK
    
  • 输入以下行,从名为 Name "IEXPLORE"的进程获取具有 属性的进程对象。 此示例使用 Get-Process (提供的 cmdlet Windows PowerShell) 作为上游命令来检索"IEXPLORE"进程。

    PS> get-process iexplore | get-proc
    

    将显示以下输出。

    Handles  NPM(K)  PM(K)   WS(K)  VS(M)  CPU(s)    Id  ProcessName
    -------  ------  -----   ----- -----   ------    --  -----------
        801      21  40720    6544    142    9.52  2288  iexplore
        726      21  25872   16652    138   22.09  3860  iexplore
        801      21  40720    6544    142    9.52  2288  iexplore
        726      21  25872   16652    138   22.09  3860  iexplore
    

另请参阅

添加用于处理命令行输入的参数

创建第一个 Cmdlet

扩展对象类型和格式设置

如何注册 Cmdlet、提供程序和主机应用程序

Windows PowerShell 参考

Cmdlet 示例