Bearbeiten

Freigeben über


Cmdlet Input Processing Methods

Cmdlets must override one or more of the input processing methods described in this topic to perform their work. These methods allow the cmdlet to perform operations of pre-processing, input processing, and post-processing. These methods also allow you to stop cmdlet processing. For a more detailed example of how to use these methods, see SelectStr Tutorial.

Pre-Processing Operations

Cmdlets should override the System.Management.Automation.Cmdlet.BeginProcessing method to add any preprocessing operations that are valid for all the records that will be processed later by the cmdlet. When PowerShell processes a command pipeline, PowerShell calls this method once for each instance of the cmdlet in the pipeline. For more information about how PowerShell invokes the command pipeline, see Cmdlet Processing Lifecycle.

The following code shows an implementation of the BeginProcessing method.

protected override void BeginProcessing()
{
  // Replace the WriteObject method with the logic required by your cmdlet.
  WriteObject("This is a test of the BeginProcessing template.");
}

Input Processing Operations

Cmdlets can override the System.Management.Automation.Cmdlet.ProcessRecord method to process the input that is sent to the cmdlet. When PowerShell processes a command pipeline, PowerShell calls this method for each input record that is processed by the cmdlet. For more information about how PowerShell invokes the command pipeline, see Cmdlet Processing Lifecycle.

The following code shows an implementation of the ProcessRecord method.

protected override void ProcessRecord()
{
  // Replace the WriteObject method with the logic required by your cmdlet.
  WriteObject("This is a test of the ProcessRecord template.");
}

Post-Processing Operations

Cmdlets should override the System.Management.Automation.Cmdlet.EndProcessing method to add any post-processing operations that are valid for all the records that were processed by the cmdlet. For example, your cmdlet might have to clean up object variables after it is finished processing.

When PowerShell processes a command pipeline, PowerShell calls this method once for each instance of the cmdlet in the pipeline. However, it is important to remember that the PowerShell runtime will not call the EndProcessing method if the cmdlet is canceled midway through its input processing or if a terminating error occurs in any part of the cmdlet. For this reason, a cmdlet that requires object cleanup should implement the complete System.IDisposable pattern, including a finalizer, so that the runtime can call both the EndProcessing and System.IDisposable.Dispose methods at the end of processing. For more information about how PowerShell invokes the command pipeline, see Cmdlet Processing Lifecycle.

The following code shows an implementation of the EndProcessing method.

protected override void EndProcessing()
{
  // Replace the WriteObject method with the logic required by your cmdlet.
  WriteObject("This is a test of the EndProcessing template.");
}

See Also

System.Management.Automation.Cmdlet.BeginProcessing

System.Management.Automation.Cmdlet.ProcessRecord

System.Management.Automation.Cmdlet.EndProcessing

SelectStr Tutorial

System.IDisposable

Windows PowerShell Shell SDK