Åsidosätta bearbetningsmetoder för indata

De här exemplen visar hur du skriver över indatabearbetningsmetoderna i en cmdlet. Dessa metoder används för att utföra följande åtgärder:

Åsidosätta metoden BeginProcessing

Följande klass skriver ut ett exempelmeddelande. Om du vill använda den här klassen ändrar du verbet och substantivet i attributet Cmdlet, ändrar namnet på klassen så att det återspeglar det nya verbet och substantivet och lägger sedan till de funktioner som du behöver för att åsidosätta metoden 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.");
  }
}

Åsidosätta metoden ProcessRecord

Följande klass skriver ut ett exempelmeddelande. Om du vill använda den här klassen ändrar du verbet och substantivet i attributet Cmdlet, ändrar namnet på klassen så att det återspeglar det nya verbet och substantivet och lägger sedan till de funktioner som du behöver för att åsidosätta metoden 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.");
    }
}

Åsidosätta endprocessing-metoden

Följande klass skriver ut ett exempel. Om du vill använda den här klassen ändrar du verbet och substantivet i attributet Cmdlet, ändrar namnet på klassen så att det återspeglar det nya verbet och substantivet och lägger sedan till de funktioner som du behöver för att åsidosätta metoden 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.");
  }
}

Se även

System.Management.Automation.Cmdlet.BeginProcessing

System.Management.Automation.Cmdlet.EndProcessing

System.Management.Automation.Cmdlet.ProcessRecord

Skriva en Windows PowerShell-cmdlet