Windows Dev Center

Collapse the table of content
Expand the table of content
Expand Minimize
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

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 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 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 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

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 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

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 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

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 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

Community Additions

Show:
© 2015 Microsoft