// // Copyright (c) 2009 Microsoft Corporation. All rights reserved. // // DISCLAIMER OF WARRANTY: The software is licensed “as-is.” You // bear the risk of using it. Microsoft gives no express warranties, // guarantees or conditions. You may have additional consumer rights // under your local laws which this agreement cannot change. To the extent // permitted under your local laws, Microsoft excludes the implied warranties // of merchantability, fitness for a particular purpose and non-infringement. namespace Microsoft.Samples.PowerShell.Runspaces { using System; using System.Collections; using System.Collections.ObjectModel; using System.Management.Automation; using System.Management.Automation.Runspaces; using System.Windows.Forms; using PowerShell = System.Management.Automation.PowerShell; /// /// This class contains the Main entry point for this host application. /// internal class Runspace02 { /// /// This method creates the form where the output is displayed. /// private static void CreateForm() { Form form = new Form(); DataGridView grid = new DataGridView(); form.Controls.Add(grid); grid.Dock = DockStyle.Fill; // Create a PowerShell object. Creating this object takes care of // building all of the other data structures needed to run the command. using (PowerShell powershell = PowerShell.Create()) { powershell.AddCommand("get-process").AddCommand("sort-object").AddArgument("ID"); if (Runspace.DefaultRunspace == null) { Runspace.DefaultRunspace = powershell.Runspace; } Collection results = powershell.Invoke(); // The generic collection needs to be re-wrapped in an ArrayList // for data-binding to work. ArrayList objects = new ArrayList(); objects.AddRange(results); // The DataGridView will use the PSObjectTypeDescriptor type // to retrieve the properties. grid.DataSource = objects; } form.ShowDialog(); } /// /// This sample uses a PowerShell object to run the /// Get-Process cmdlet synchronously. Windows Forms and /// data binding are then used to display the results in a /// DataGridView control. /// /// Parameter not used. /// /// This sample demonstrates the following: /// 1. Creating a PowerShell object. /// 2. Adding commands and arguments to the pipeline of /// the powershell object. /// 3. Running the commands synchronously. /// 4. Using a DataGridView control to display the output /// of the PowerShell object in a Windows Forms application. /// private static void Main(string[] args) { Runspace02.CreateForm(); } } }