Επεξεργασία

Κοινή χρήση μέσω


How to Invoke a Cmdlet from Within a Cmdlet

This example shows how to invoke a binary cmdlet that derives from [System.Management.Automation.Cmdlet] directly from within another binary cmdlet, which allows you to add the functionality of the invoked cmdlet to the binary 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".

Get-Process -name [a-t]*

Important

You can invoke only those cmdlets that derive directly from the System.Management.Automation.Cmdlet class. You can't invoke a cmdlet that derives from the System.Management.Automation.PSCmdlet class. For an example, see How to invoke a PSCmdlet from within a PSCmdlet.

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;   // PowerShell assembly.
    using Microsoft.PowerShell.Commands;  // PowerShell cmdlets assembly you want to call.
    
  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 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 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 System.Management.Automation.Cmdlet.BeginProcessing method of a cmdlet.

using System;
using System.Diagnostics;
using System.Management.Automation;   // PowerShell assembly.
using Microsoft.PowerShell.Commands;  // PowerShell cmdlets assembly you want to call.

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; set; }

    // 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>())
      {
        WriteVerbose(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 + "!");
    }
  }
}

See Also

Writing a Windows PowerShell Cmdlet