Przykładowy kod RunSpace10RunSpace10 Code Sample

Oto kod źródłowy dla przykładu Runspace10.Here is the source code for the Runspace10 sample. Ta przykładowa aplikacja dodaje polecenie cmdlet do System. Management. Automation. obszarami działania. Runspaceconfiguration , a następnie używa zmodyfikowanych informacji o konfiguracji do utworzenia obszaru działania.This sample application adds a cmdlet to System.Management.Automation.Runspaces.Runspaceconfiguration and then uses the modified configuration information to create the runspace.

Uwaga

Plik źródłowy C# (runspace10.cs) można pobrać przy użyciu zestawu Windows Software Development Kit dla systemów Windows Vista i Microsoft .NET Framework 3,0 Runtime.You can download the C# source file (runspace10.cs) by using the Windows Software Development Kit for Windows Vista and Microsoft .NET Framework 3.0 Runtime Components. Aby uzyskać instrukcje dotyczące pobierania, zobacz jak zainstalować program Windows PowerShell i pobrać zestaw SDK programu Windows PowerShell.For download instructions, see How to Install Windows PowerShell and Download the Windows PowerShell SDK. Pobrane pliki źródłowe są dostępne w <PowerShell Samples> katalogu.The downloaded source files are available in the <PowerShell Samples> directory.

Przykładowy kodCode Sample

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 add a cmdlet to an InitialSessionState object and then 
    /// uses the modified InitialSessionState object when creating 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. Craeting a PowerShell object tht uses the Runspace object.
    /// 5. Running the pipeline of the PowerShell object 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();
    }
  }
}

Zobacz teżSee Also

Windows PowerShell — przewodnik programistyWindows PowerShell Programmer's Guide

Windows PowerShell SDKWindows PowerShell SDK