如何编写 cmdlet

本文介绍如何编写 cmdlet。 Send-GreetingCmdlet 使用单个用户名作为输入,然后将问候语写入该用户。 尽管此 cmdlet 不会执行许多工作,但本示例演示了 cmdlet 的主要部分。

编写 cmdlet 的步骤

  1. 若要将类声明为 cmdlet,请使用 cmdlet 特性。 Cmdlet 属性指定该 cmdlet 名称的动词和名词。

    有关 Cmdlet 特性的详细信息,请参阅 CmdletAttribute 声明

  2. 指定类的名称。

  3. 指定该 cmdlet 派生自以下任一类:

  4. 若要定义 cmdlet 的参数,请使用 Parameter 特性。 在这种情况下,只指定了一个必需的参数。

    有关 参数 属性的详细信息,请参阅 ParameterAttribute 声明

  5. 重写处理输入的输入处理方法。 在这种情况下,将重写 ProcessRecord 方法。

  6. 若要编写问候语,请使用方法 WriteObject。 该问候语按以下格式显示:

    Hello <UserName>!
    

示例

using System.Management.Automation;  // Windows PowerShell assembly.

namespace SendGreeting
{
  // Declare the class as a cmdlet and specify the
  // appropriate verb and noun for the cmdlet name.
  [Cmdlet(VerbsCommunications.Send, "Greeting")]
  public class SendGreetingCommand : Cmdlet
  {
    // Declare the parameters for the cmdlet.
    [Parameter(Mandatory=true)]
    public string Name
    {
      get { return name; }
      set { name = value; }
    }
    private string name;

    // Override the ProcessRecord method to process
    // the supplied user name and write out a
    // greeting to the user by calling the WriteObject
    // method.
    protected override void ProcessRecord()
    {
      WriteObject("Hello " + name + "!");
    }
  }
}

另请参阅

System.object。

System.web. PSCmdlet

"ProcessRecord"。

"WriteObject"。

CmdletAttribute 声明

ParameterAttribute 声明

编写 Windows PowerShell Cmdlet