Cómo invocar un cmdlet desde dentro de un cmdletHow to Invoke a Cmdlet from Within a Cmdlet

En este ejemplo se muestra cómo invocar un cmdlet desde otro cmdlet, lo que le permite agregar la funcionalidad del cmdlet invocado al cmdlet que está desarrollando.This example shows how to invoke a cmdlet from within another cmdlet, which allows you to add the functionality of the invoked cmdlet to the cmdlet you are developing. En este ejemplo, el Get-Process cmdlet se invoca para obtener los procesos que se están ejecutando en el equipo local.In this example, the Get-Process cmdlet is invoked to get the processes that are running on the local computer. La llamada al Get-Process cmdlet es equivalente al comando siguiente.The call to the Get-Process cmdlet is equivalent to the following command. Este comando recupera todos los procesos cuyos nombres comienzan con los caracteres "a" a "t".This command retrieves all the processes whose names start with the characters "a" through "t".

Get-Process -name [a-t]

Importante

Solo puede invocar los cmdlets que derivan directamente de la clase System. Management. Automation. cmdlet .You can invoke only those cmdlets that derive directly from the System.Management.Automation.Cmdlet class. No se puede invocar un cmdlet derivado de la clase System. Management. Automation. PSCmdlet .You cannot invoke a cmdlet that derives from the System.Management.Automation.PSCmdlet class.

Para invocar un cmdlet desde un cmdletTo invoke a cmdlet from within a cmdlet

  1. Asegúrese de que se hace referencia al ensamblado que define el cmdlet que se va a invocar y que using se ha agregado la instrucción adecuada.Ensure that the assembly that defines the cmdlet to be invoked is referenced and that the appropriate using statement is added. En este ejemplo, se agregan los siguientes espacios de nombres.In this example, the following namespaces are added.

    using System.Diagnostics;
    using System.Management.Automation;   // Windows PowerShell assembly.
    using Microsoft.PowerShell.Commands;  // Windows PowerShell assembly.
    
  2. En el método de procesamiento de entrada del cmdlet, cree una nueva instancia del cmdlet que se va a invocar.In the input processing method of the cmdlet, create a new instance of the cmdlet to be invoked. En este ejemplo, se crea un objeto de tipo Microsoft. PowerShell. Commands. Getprocesscommand junto con la cadena que contiene los argumentos que se usan cuando se invoca el cmdlet.In this example, an object of type Microsoft.PowerShell.Commands.Getprocesscommand is created along with the string that contains the arguments that are used when the cmdlet is invoked.

    GetProcessCommand gp = new GetProcessCommand();
    gp.Name = new string[] { "[a-t]*" };
    
  3. Llame al método System. Management. Automation. cmdlet. Invoke * para invocar el Get-Process cmdlet.Call the System.Management.Automation.Cmdlet.Invoke* method to invoke the Get-Process cmdlet.

      foreach (Process p in gp.Invoke<Process>())
      {
        Console.WriteLine(p.ToString());
      }
    }
    

EjemploExample

En este ejemplo, el Get-Process cmdlet se invoca desde el método System. Management. Automation. cmdlet. BeginProcessing de un cmdlet.In this example, the Get-Process cmdlet is invoked from within the System.Management.Automation.Cmdlet.BeginProcessing method of a cmdlet.

using System;
using System.Diagnostics;
using System.Management.Automation;   // Windows PowerShell assembly.
using Microsoft.PowerShell.Commands;  // Windows PowerShell assembly.

namespace SendGreeting
{
  // Declare the class as a cmdlet and specify an
  // appropriate verb and noun for the cmdlet name.
  [Cmdlet(VerbsCommunications.Send, "GreetingInvoke")]
  public class SendGreetingInvokeCommand : Cmdlet
  {
    // Declare the parameters for the cmdlet.
    [Parameter(Mandatory = true)]
    public string Name
    {
      get { return name; }
      set { name = value; }
    }
    private string name;

    // Override the BeginProcessing method to invoke
    // the Get-Process cmdlet.
    protected override void BeginProcessing()
    {
      GetProcessCommand gp = new GetProcessCommand();
      gp.Name = new string[] { "[a-t]*" };
      foreach (Process p in gp.Invoke<Process>())
      {
        Console.WriteLine(p.ToString());
      }
    }

    // Override the ProcessRecord method to process
    // the supplied user name and write out a
    // greeting to the user by calling the WriteObject
    // method.
    protected override void ProcessRecord()
    {
      WriteObject("Hello " + name + "!");
    }
  }
}

Consulte tambiénSee Also

Escribir un cmdlet de Windows PowerShellWriting a Windows PowerShell Cmdlet