How to Override Input Processing Methods
These examples show how to overwrite the input processing methods within a cmdlet. These methods are used to perform the following operations:
The System.Management.Automation.Cmdlet.BeginProcessing method is used to perform one-time startup operations that are valid for all the objects processed by the cmdlet. The Windows PowerShell runtime calls this method only once.
The System.Management.Automation.Cmdlet.ProcessRecord method is used to process the objects passed to the cmdlet. The Windows PowerShell runtime calls this method for each object passed to the cmdlet.
The System.Management.Automation.Cmdlet.EndProcessing method is used to perform one-time post processing operations. The Windows PowerShell runtime calls this method only once.
To override the BeginProcessing method
- Declare a protected override of the System.Management.Automation.Cmdlet.BeginProcessing method.
The following class prints a sample message. To use this class, change the verb and noun in the Cmdlet attribute, change the name of the class to reflect the new verb and noun, and then add the functionality you require to the override of the System.Management.Automation.Cmdlet.BeginProcessing method.
[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.");
}
}
To override the ProcessRecord method
- Declare a protected override of the System.Management.Automation.Cmdlet.ProcessRecord method.
The following class prints a sample message. To use this class, change the verb and noun in the Cmdlet attribute, change the name of the class to reflect the new verb and noun, and then add the functionality you require to the override of the System.Management.Automation.Cmdlet.ProcessRecord method.
[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.");
}
}
To override the EndProcessing method
- Declare a protected override of the System.Management.Automation.Cmdlet.EndProcessing method.
The following class prints a sample. To use this class, change the verb and noun in the Cmdlet attribute, change the name of the class to reflect the new verb and noun, and then add the functionality you require to the override of the System.Management.Automation.Cmdlet.EndProcessing method.
[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.");
}
}
See Also
System.Management.Automation.Cmdlet.BeginProcessing
System.Management.Automation.Cmdlet.EndProcessing
PowerShell