Remarque
L’accès à cette page nécessite une autorisation. Vous pouvez essayer de vous connecter ou de modifier des répertoires.
L’accès à cette page nécessite une autorisation. Vous pouvez essayer de modifier des répertoires.
Cet exemple montre comment utiliser la classe System.Management.Automation.PowerShell pour exécuter les applets de commande Get-Process et tri-object de manière synchrone. L’applet de commande Get-ProcessSystem.Diagnostics.Process pour chaque processus exécuté sur l’ordinateur local, et le Sort-Object trie les objets en fonction de leur propriété System.Diagnostics.Process.Id*. Les résultats de ces commandes sont affichés à l’aide d’un contrôle System.Windows.Forms.DataGridView.
Spécifications
Cet exemple nécessite Windows PowerShell 2.0.
Montre ce qui suit
Cet exemple illustre ce qui suit.
Création d’un objet System.Management.Automation.PowerShell pour exécuter des commandes.
Ajout de commandes au pipeline de objet System.Management.Automation.PowerShell.
Exécution synchrone des commandes.
Utilisation d’un contrôle System.Windows.Forms.DataGridView pour afficher la sortie des commandes dans une application Windows Forms.
Exemple :
Cet exemple exécute les applets de commande Get-Process et Sort-Object de manière synchrone dans l’espace d’exécution par défaut fourni par Windows PowerShell. La sortie s’affiche dans un formulaire à l’aide d’un contrôle System.Windows.Forms.DataGridView.
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;
/// <summary>
/// This class contains the Main entry point for this host application.
/// </summary>
internal class Runspace02
{
/// <summary>
/// This method creates the form where the output is displayed.
/// </summary>
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<PSObject> 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();
}
/// <summary>
/// This sample uses a PowerShell object to run the Get-Process
/// and Sort-Object cmdlets synchronously. Windows Forms and
/// data binding are then used to display the results in a
/// DataGridView control.
/// </summary>
/// <param name="args">The parameter is not used.</param>
/// <remarks>
/// 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 commands in a Windows Forms application.
/// </remarks>
private static void Main(string[] args)
{
Runspace02.CreateForm();
}
}
}