How to Invoke a Cmdlet from Within a Cmdlet

 

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. In this example, the Get-Process cmdlet is invoked to get the processes that are running on the local computer. The call to the Get-Process cmdlet is equivalent to the following command. This command retrieves all the processes whose names start with the characters "a" through "t".

PS>get-process -name [a-t]*

System_CAPS_importantImportant

You can invoke only those cmdlets that derive directly from the Cmdlet class. You cannot invoke a cmdlet that derives from the PSCmdlet class.

To invoke a cmdlet from within a cmdlet

  1. Ensure that the assembly that defines the cmdlet to be invoked is referenced and that the appropriate using statement is added. 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. In the input processing method of the cmdlet, create a new instance of the cmdlet to be invoked. In this example, an object of type T: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. Call the Overload:System.Management.Automation.Cmdlet.Invoke method to invoke the Get-Process cmdlet.

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

Example

In this example, the Get-Process cmdlet is invoked from within the 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 + "!");
    }
  }
}
Show: