如何替代输入处理方法

这些示例显示如何覆盖 cmdlet 中的输入处理方法。 这些方法用于执行以下操作:

重写 BeginProcessing 方法

以下类打印示例消息。 若要使用此类,请更改 Cmdlet 属性中的谓词和名词,更改类的名称以反映新的谓词和名词,然后将所需的功能添加到 System.Management.Automation.Cmdlet.BeginProcessing 方法的重写中。

[Cmdlet(VerbsDiagnostic.Test, "BeginProcessingClass")]
public class TestBeginProcessingClassTemplate : Cmdlet
{
  // Override the BeginProcessing method to add preprocessing
  //operations to the cmdlet.
  protected override void BeginProcessing()
  {
    // Replace the WriteObject method with the logic required
    // by your cmdlet. It is used here to generate the following
    // output:
    // "This is a test of the BeginProcessing template."
    WriteObject("This is a test of the BeginProcessing template.");
  }
}

重写 ProcessRecord 方法

以下类打印示例消息。 若要使用此类,请更改 Cmdlet 属性中的谓词和名词,更改类的名称以反映新的谓词和名词,然后将所需的功能添加到 System.Management.Automation.Cmdlet.ProcessRecord 方法的重写中。

[Cmdlet(VerbsDiagnostic.Test, "ProcessRecordClass")]
public class TestProcessRecordClassTemplate : Cmdlet
{
    // Override the ProcessRecord method to add processing
    //operations to the cmdlet.
    protected override void ProcessRecord()
    {
        // Replace the WriteObject method with the logic required
        // by your cmdlet. It is used here to generate the following
        // output:
        // "This is a test of the ProcessRecord template."
        WriteObject("This is a test of the ProcessRecord template.");
    }
}

重写 EndProcessing 方法

以下类打印示例。 若要使用此类,请更改 Cmdlet 属性中的谓词和名词,更改类的名称以反映新的谓词和名词,然后将所需的功能添加到 System.Management.Automation.Cmdlet.EndProcessing 方法的重写中。

[Cmdlet(VerbsDiagnostic.Test, "EndProcessingClass")]
public class TestEndProcessingClassTemplate : Cmdlet
{
  // Override the EndProcessing method to add postprocessing
  //operations to the cmdlet.
  protected override void EndProcessing()
  {
    // Replace the WriteObject method with the logic required
    // by your cmdlet. It is used here to generate the following
    // output:
    // "This is a test of the BeginProcessing template."
    WriteObject("This is a test of the EndProcessing template.");
  }
}

另请参阅

System.Management.Automation.Cmdlet.BeginProcessing

System.Management.Automation.Cmdlet.EndProcessing

System.Management.Automation.Cmdlet.ProcessRecord

编写 Windows PowerShell Cmdlet