Codice di esempio di GetProc03 (C#)GetProc03 (C#) Sample Code

Nel codice seguente viene illustrata l'implementazione di un Get-Process cmdlet che può accettare input da pipeline.The following code shows the implementation of a Get-Process cmdlet that can accept pipelined input. Questa implementazione definisce un Name parametro che accetta input della pipeline, recupera le informazioni sul processo dal computer locale in base ai nomi forniti, quindi usa il metodo WriteObject (System. Object, System. Boolean) come meccanismo di output per l'invio di oggetti alla pipeline.This implementation defines a Name parameter that accepts pipeline input, retrieves process information from the local computer based on the supplied names, and then uses the WriteObject(System.Object,System.Boolean) method as the output mechanism for sending objects to the pipeline.

Nota

È possibile scaricare il file di origine C# (getprov03.cs) per questo cmdlet Get-proc utilizzando Microsoft Windows Software Development Kit per i componenti di runtime di Windows Vista e .NET Framework 3,0.You can download the C# source file (getprov03.cs) for this Get-Proc cmdlet using the Microsoft Windows Software Development Kit for Windows Vista and .NET Framework 3.0 Runtime Components. Per istruzioni sul download, vedere come installare Windows PowerShell e scaricare Windows PowerShell SDK.For download instructions, see How to Install Windows PowerShell and Download the Windows PowerShell SDK. I file di origine scaricati sono disponibili nella <PowerShell Samples> Directory.The downloaded source files are available in the <PowerShell Samples> directory.

Codice di esempioCode Sample

namespace Microsoft.Samples.PowerShell.Commands
{
    using System;
    using System.Diagnostics;
    using System.Management.Automation;       // Windows PowerShell namespace.
    #region GetProcCommand

   /// <summary>
   /// This class implements the get-proc cmdlet.
   /// </summary>
   [Cmdlet(VerbsCommon.Get, "Proc")]
   public class GetProcCommand : Cmdlet
   {
      #region Parameters

       /// <summary>
       /// The names of the processes to act on.
       /// </summary>
       private string[] processNames;

      /// <summary>
      /// Gets or setsthe list of process names on 
      /// which the Get-Proc cmdlet will work.
      /// </summary>
      [Parameter(
         Position = 0,
         ValueFromPipeline = true,
         ValueFromPipelineByPropertyName = true)]
      [ValidateNotNullOrEmpty]
      public string[] Name
      {
         get { return this.processNames; }
         set { this.processNames = value; }
      }
       
      #endregion Parameters

      #region Cmdlet Overrides

      /// <summary>
      /// The ProcessRecord method calls the Process.GetProcesses 
      /// method to retrieve the processes specified by the Name 
      /// parameter. Then, the WriteObject method writes the 
      /// associated processes to the pipeline.
      /// </summary>
      protected override void ProcessRecord()
      {        
          // If no process names are passed to the cmdlet, get all 
          // processes.
          if (this.processNames == null)
          {
              WriteObject(Process.GetProcesses(), true);
          }
          else
          {
              // If process names are passed to the cmdlet, get and write 
              // the associated processes.
              foreach (string name in this.processNames)
              {
                  WriteObject(Process.GetProcessesByName(name), true);
              }
          } // if (processNames ...
      } // ProcessRecord
      #endregion Overrides
   } // End GetProcCommand class.

    #endregion GetProcCommand
}

Vedere ancheSee Also

Manuale del programmatore di Windows PowerShellWindows PowerShell Programmer's Guide

Windows PowerShell SDKWindows PowerShell SDK