Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
In diesem Beispiel wird gezeigt, wie Sie einen Runspace erstellen und dann diesen Runspace verwenden, um zwei Cmdlets synchron mithilfe eines System.Management.Automation.PowerShell--Objekts auszuführen.
Anforderungen
In diesem Beispiel ist Windows PowerShell 2.0 erforderlich.
Veranschaulichung
In diesem Beispiel wird Folgendes veranschaulicht.
Erstellen eines System.Management.Automation.Runspaces.Runspace Objekt mithilfe der System.Management.Automation.Runspaces.RunspaceFactory Klasse.
Erstellen eines System.Management.Automation.PowerShell--Objekts, das den Runspace verwendet.
Hinzufügen von Cmdlets zur Pipeline des System.Management.Automation.PowerShell--Objekts.
Synchrones Ausführen der Cmdlets.
Extrahieren von Eigenschaften aus dem System.Management.Automation.PSObject objekte, die vom Befehl zurückgegeben werden.
Beispiel
In diesem Beispiel wird ein Runspace erstellt, der von einem System.Management.Automation.PSObject-Objekt zum Ausführen der Get-Process- und Measure-Object--Cmdlets verwendet wird.
namespace Microsoft.Samples.PowerShell.Runspaces
{
using System;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using PowerShell = System.Management.Automation.PowerShell;
/// <summary>
/// This class contains the Main entry point for this host application.
/// </summary>
internal class Runspace07
{
/// <summary>
/// This sample shows how to create a runspace and how to run commands
/// using a PowerShell object. It builds a pipeline that runs the
/// Get-Process cmdlet, which is piped to the Measure-Object
/// cmdlet to count the number of processes running on the system.
/// </summary>
/// <param name="args">The parameter is not used.</param>
/// <remarks>
/// This sample demonstrates the following:
/// 1. Creating a runspace using the RunspaceFactory class.
/// 2. Creating a PowerShell object that uses the runspace.
/// 3. Adding cmdlets to the pipeline of the PowerShell object.
/// 4. Running the cmdlets synchronously.
/// 5. Working with PSObject objects to extract properties
/// from the objects returned by the cmdlets.
/// </remarks>
private static void Main(string[] args)
{
Collection<PSObject> result; // Will hold the result
// of running the cmdlets.
// Create a runspace. We can't use the RunspaceInvoke class
// because we need to get at the underlying runspace to
// explicitly add the commands. Notice that no PSHost object is
// supplied to the CreateRunspace method so the default host is
// used. See the Host samples for more information on creating
// your own custom host.
using (Runspace myRunSpace = RunspaceFactory.CreateRunspace())
{
myRunSpace.Open();
// Create a PowerShell object and specify the runspace.
PowerShell powershell = PowerShell.Create();
powershell.Runspace = myRunSpace;
// Use the using statement so we dispose of the PowerShell object
// when we're done.
using (powershell)
{
// Add the Get-Process cmdlet to the PowerShell object. Notice
// we are specify the name of the cmdlet, not a script.
powershell.AddCommand("Get-Process");
// Add the Measure-Object cmdlet to count the number
// of objects being returned. Commands are always added to the end
// of the pipeline.
powershell.AddCommand("Measure-Object");
// Run the cmdlets synchronously and save the objects returned.
result = powershell.Invoke();
}
// Even after disposing of the pipeLine, we still need to set
// the powershell variable to null so that the garbage collector
// can clean it up.
powershell = null;
// Display the results of running the commands (checking that
// everything is ok first.
if (result == null || result.Count != 1)
{
throw new InvalidOperationException(
"pipeline.Invoke() returned the wrong number of objects");
}
PSMemberInfo count = result[0].Properties["Count"];
if (count == null)
{
throw new InvalidOperationException(
"The object returned doesn't have a 'count' property");
}
Console.WriteLine(
"Runspace07: The Get-Process cmdlet returned {0} objects",
count.Value);
// Close the runspace to release any resources.
myRunSpace.Close();
}
System.Console.WriteLine("Hit any key to exit...");
System.Console.ReadKey();
}
}
}