如何替代输入处理方法
这些示例显示如何覆盖 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 方法
以下类打印示例消息。 若要使用此类,请更改 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
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈