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

[玩转系统] 如何重写输入处理方法

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

如何重写输入处理方法


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

  • System.Management.Automation.Cmdlet.BeginProcessing 方法用于执行一次性启动操作,该操作对 cmdlet 处理的所有对象都有效。 Windows PowerShell 运行时仅调用此方法一次。

  • System.Management.Automation.Cmdlet.ProcessRecord 方法用于处理传递给 cmdlet 的对象。 Windows PowerShell 运行时为传递给 cmdlet 的每个对象调用此方法。

  • System.Management.Automation.Cmdlet.EndProcessing 方法用于执行一次性后处理操作。 Windows PowerShell 运行时仅调用此方法一次。

重写 BeginProcessing 方法

  • 声明 System.Management.Automation.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 方法

  • 声明 System.Management.Automation.Cmdlet.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 方法

  • 声明 System.Management.Automation.Cmdlet.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.");
  }
}

参见

系统.管理.自动化.Cmdlet.BeginProcessing

系统.管理.自动化.Cmdlet.EndProcessing

系统.管理.自动化.Cmdlet.ProcessRecord

编写 Windows PowerShell Cmdlet

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

取消回复欢迎 发表评论:

关灯