1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
using System; using System.Management.Automation; // Windows PowerShell namespace. namespace HostPS1 { class HostPS1 { static void Main(string[] args) { // Call the PowerShell.Create() method to create an // empty pipeline. PowerShell ps = PowerShell.Create(); // Call the PowerShell.AddCommand(string) method, add // the Get-Process cmdlet to the pipeline. Do // not include spaces before or after the cmdlet name // because that will cause the command to fail. ps.AddCommand("Get-Process"); Console.WriteLine("Process Id"); Console.WriteLine("----------------------------"); // Call the PowerShell.Invoke() method to run the // commands of the pipeline in the default runspace. foreach (PSObject result in ps.Invoke()) { Console.WriteLine("{0,-24}{1}", result.Members["ProcessName"].Value, result.Members["Id"].Value); } // End foreach. } // End Main. } // End HostPs1. } |