当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 添加处理管道输入的参数

作者:精品下载站 日期:2024-12-14 02:49:29 浏览:14 分类:玩电脑

添加处理管道输入的参数


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 后,必须通过 Windows PowerShell 管理单元将其注册到 Windows PowerShell。有关注册 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 示例

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯