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

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

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

定义 Cmdlet 类

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

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

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

从管道定义输入

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

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

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

[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

前面的声明将 ValueFromPipeline 关键字设置为 true,以便当对象与参数的类型相同时,Windows PowerShell 运行时会将该参数绑定到传入对象,或者该参数是否可以强制绑定到同一类型。 ValueFromPipelineByPropertyName 关键字也设置为 true,以便 Windows PowerShell 运行时将检查传入对象的 Name 属性。 如果传入对象具有此类属性,运行时会将 Name 参数绑定到传入对象的 Name 属性。

注释

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

重写输入处理方法

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

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

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 的详细信息,请参阅 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
    
  • 输入以下行,从名为“IEXPLORE”的进程获取具有 Name 属性的进程对象。 此示例使用 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 示例