Runspace10 – minta
Ez a példa bemutatja, hogyan hozhat létre alapértelmezett kezdeti munkamenet-állapotot, hogyan adhat hozzá egy parancsmagot a System.Management.Automation.Runspaces.Initialsessionstate állapothoz,hogyan hozhat létre a kezdeti munkamenet-állapotot használó runspace-t, és hogyan futtathatja a parancsot egy System.Management.Automation.Powershell objektummal.
Követelmények
Ehhez a mintához Windows PowerShell 2.0 szükséges.
Útmutató ehhez:
Ez a minta a következőket mutatja be.
System.Management.Automation.Runspaces.Initialsessionstate objektum létrehozása.
Parancsmag hozzáadása (amelyet a gazdaalkalmazás határoz meg) a System.Management.Automation.Runspaces.Initialsessionstate objektumhoz.
A objektumot használó System.Management.Automation.Runspaces.Runspace objektum létrehozása.
A System.Management.Automation.Runspace objektumot használó System.Management.Automation.Runspaces.PowerShell-objektum létrehozása.
Adja hozzá a parancsot a System.Management.Automation.Powershell objektum folyamatához.
Tulajdonságok kinyerése a parancs által visszaadott System.Management.Automation.PSObject objektumokból.
Példa
Ez a minta egy olyan runspace-t hoz létre, amely egy System.Management.Automation.Runspaces.Initialsessionstate objektumot használ a runspace megnyitásakor elérhető elemek meghatározásához. Ebben a példában a Get-Proc parancsmag (amelyet a gazdaalkalmazás határoz meg) hozzá lesz adva a kezdeti munkamenet-állapothoz, és a parancsmag szinkron módon fut egy System.Management.Automation.Powershell objektummal.
namespace Microsoft.Samples.PowerShell.Runspaces
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using PowerShell = System.Management.Automation.PowerShell;
#region GetProcCommand
/// <summary>
/// Class that implements the GetProcCommand.
/// </summary>
[Cmdlet(VerbsCommon.Get, "Proc")]
public class GetProcCommand : Cmdlet
{
#region Cmdlet Overrides
/// <summary>
/// For each of the requested process names, retrieve and write
/// the associated processes.
/// </summary>
protected override void ProcessRecord()
{
// Get the current processes.
Process[] processes = Process.GetProcesses();
// Write the processes to the pipeline making them available
// to the next cmdlet. The second argument (true) tells the
// system to enumerate the array, and send one process object
// at a time to the pipeline.
WriteObject(processes, true);
}
#endregion Overrides
} // End GetProcCommand class.
#endregion GetProcCommand
/// <summary>
/// This class contains the Main entry point for this host application.
/// </summary>
internal class Runspace10
{
/// <summary>
/// This sample shows how to create a default initial session state, how to add
/// add a cmdlet to the InitialSessionState object, and then how to create
/// a Runspace object.
/// </summary>
/// <param name="args">Parameter is not used.</param>
/// This sample demonstrates:
/// 1. Creating an InitialSessionState object.
/// 2. Adding a cmdlet to the InitialSessionState object.
/// 3. Creating a runspace that uses the InitialSessionState object.
/// 4. Creating a PowerShell object that uses the Runspace object.
/// 5. Running the added command synchronously.
/// 6. Working with PSObject objects to extract properties
/// from the objects returned by the pipeline.
private static void Main(string[] args)
{
// Create a default InitialSessionState object. The default
// InitialSessionState object contains all the elements provided
// by Windows PowerShell.
InitialSessionState iss = InitialSessionState.CreateDefault();
// Add the get-proc cmdlet to the InitialSessionState object.
SessionStateCmdletEntry ssce = new SessionStateCmdletEntry("get-proc", typeof(GetProcCommand), null);
iss.Commands.Add(ssce);
// Create a Runspace object that uses the InitialSessionState object.
// Notice that no PSHost object is specified, so the default host is used.
// See the Hosting samples for information on creating your own custom host.
using (Runspace myRunSpace = RunspaceFactory.CreateRunspace(iss))
{
myRunSpace.Open();
using (PowerShell powershell = PowerShell.Create())
{
powershell.Runspace = myRunSpace;
// Add the get-proc cmdlet to the pipeline of the PowerShell object.
powershell.AddCommand("get-proc");
Collection<PSObject> results = powershell.Invoke();
Console.WriteLine("Process HandleCount");
Console.WriteLine("--------------------------------");
// Display the output of the pipeline.
foreach (PSObject result in results)
{
Console.WriteLine(
"{0,-20} {1}",
result.Members["ProcessName"].Value,
result.Members["HandleCount"].Value);
}
}
// Close the runspace to release resources.
myRunSpace.Close();
}
System.Console.WriteLine("Hit any key to exit...");
System.Console.ReadKey();
}
}
}
Lásd még:
Visszajelzés
https://aka.ms/ContentUserFeedback.
Hamarosan elérhető: 2024-ben fokozatosan kivezetjük a GitHub-problémákat a tartalom visszajelzési mechanizmusaként, és lecseréljük egy új visszajelzési rendszerre. További információ:Visszajelzés küldése és megtekintése a következőhöz: