Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Adding and invoking commands

 

After creating a runspace, you can add Windows PowerShellcommands and scripts to a pipeline, and then invoke the pipeline synchronously or asynchronously.

The PowerShell class provides several methods to add commands, parameters, and scripts to the pipeline. You can invoke the pipeline synchronously by calling an overload of the Invoke method, or asynchronously by calling an overload of the BeginInvoke and then the EndInvoke method.

You use the AddCommand method of the PowerShell class to add commands to the pipeline. For example, suppose you want to get the list of running processes on the machine. The way to run this command is as follows.

  1. Create a PowerShell object.

    PowerShell ps = PowerShell.Create();
    
  2. Add the command that you want to execute.

    ps.AddCommand(“Get-Process”);
    
  3. Invoke the command.

    ps.Invoke();
    

    If you call the AddCommand method more than once before you call the Invoke method, the result of the first command is piped to the second, and so on. If you do not want to pipe the result of a previous command to a command, add it by calling the M:System.Management.Automation.PowerShell.AddStatement instead.

The previous example executes a single command without any parameters. You can add parameters to the command by using the AddParameter method For example, the following code gets a list of all of the processes that are named PowerShell running on the machine.

PowerShell.Create().AddCommand(“Get-Process”)
                   .AddParameter(“Name”, “PowerShell”)
                   .Invoke();

You can add additional parameters by calling AddParameter repeatedly.

PowerShell.Create().AddCommand(“Get-Process”)
                   .AddParameter(“Name”, “PowerShell”)
                   .AddParameter(“Id”, “12768”)
                   .Invoke();

You can also add a dictionary of parameter names and values by calling the AddParameters method.

IDictionary parameters = new Dictionary<String, String>();
parameters.Add("Name", "PowerShell");

parameters.Add("Id", "12768");
PowerShell.Create().AddCommand(“Get-Process”)
   .AddParameters(parameters)
      .Invoke()

You can simulate batching by using the M:System.Management.Automation.PowerShell.AddStatement method, which adds an additional statement to the end of the pipeline The following code gets a list of running processes with the name PowerShell, and then gets the list of running services.

PowerShell ps = PowerShell.Create();
ps.AddCommand(“Get-Process”).AddParameter(“Name”, “PowerShell”);
ps.AddStatement().AddCommand(“Get-Service”);
ps.Invoke();

You can run an existing script by calling the AddScript method. The following example adds a script to the pipeline and runs it. This example assumes there is already a script named MyScript.ps1 in a folder named D:\PSScripts.

PowerShell ps = PowerShell.Create();
ps.AddScript(“D:\PSScripts\MyScript.ps1”).Invoke();

There is also a version of the AddScript method that takes a boolean parameter named useLocalScope. If this parameter is set to true, then the script is run in the local scope. The following code will run the script in the local scope.

PowerShell ps = PowerShell.Create();
ps.AddScript(@“D:\PSScripts\MyScript.ps1”, true).Invoke();

After you add elements to the pipeline, you invoke it. To invoke the pipeline synchronously, you call an overload of the Invoke method. The following example shows how to synchronously invoke a pipeline.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation;


namespace HostPS1e
{
  class HostPS1e
  {
    static void Main(string[] args)
    {
      // Using the PowerShell.Create and AddCommand 
      // methods, create a command pipeline.
      PowerShell ps = PowerShell.Create().AddCommand ("Sort-Object");

      // Using the PowerShell.Invoke method, run the command
      // pipeline using the supplied input.
      foreach (PSObject result in ps.Invoke(new int[] { 3, 1, 6, 2, 5, 4 }))
      {
          Console.WriteLine("{0}", result);
      } // End foreach.
    } // End Main.
  } // End HostPS1e.
}

You invoke a pipeline asynchronously by calling an overload of the BeginInvoke to create an IAsyncResult object, and then calling the EndInvoke method.

The following example shows how to invoke a pipeline asynchronoulsy.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation;


namespace HostPS3
{
  class HostPS3
  {
    static void Main(string[] args)
    {
      // Use the PowerShell.Create and Powers.AddCommand 
      // methods to create a command pipeline that includes 
      // Get-Process cmdlet. Do not include spaces immediatly 
      // before or after the cmdlet name as that will cause 
      // the command to fail.
      PowerShell ps = PowerShell.Create().AddCommand("Get-Process");

      // Create an IAsyncResult object and call the
      // BeginInvoke method to start running the 
      // command pipeline asynchronously.
      IAsyncResult async = ps.BeginInvoke();

      // Using the PowerShell.Invoke method, run the command
      // pipeline using the default runspace.
      foreach (PSObject result in ps.EndInvoke(async))
      {
        Console.WriteLine("{0,-20}{1}",
                result.Members["ProcessName"].Value,
                result.Members["Id"].Value);
      } // End foreach.
      System.Console.WriteLine("Hit any key to exit.");
      System.Console.ReadKey();
    } // End Main.
  } // End HostPS3.
}
Show: