Código de ejemplo GetProc01 (C#)GetProc01 (C#) Sample Code

En el código siguiente se muestra la implementación del cmdlet de ejemplo GetProc01.The following code shows the implementation of the GetProc01 sample cmdlet. Tenga en cuenta que el cmdlet se simplifica al mantener el trabajo real de la recuperación del proceso en el método System. Diagnostics. Process. GetProcesses * .Notice that the cmdlet is simplified by leaving the actual work of process retrieval to the System.Diagnostics.Process.Getprocesses* method.

Nota

Puede descargar el archivo de código fuente de C# (getproc01.cs) para este cmdlet de Get-Proc mediante el kit de desarrollo de software de Microsoft Windows para los componentes de tiempo de ejecución de Windows Vista y .NET Framework 3,0.You can download the C# source file (getproc01.cs) for this Get-Proc cmdlet using the Microsoft Windows Software Development Kit for Windows Vista and .NET Framework 3.0 Runtime Components. Para obtener instrucciones de descarga, consulte Cómo instalar Windows PowerShell y descargar el SDK de Windows PowerShell.For download instructions, see How to Install Windows PowerShell and Download the Windows PowerShell SDK. Los archivos de origen descargados están disponibles en el <PowerShell Samples> directorio.The downloaded source files are available in the <PowerShell Samples> directory.

Ejemplo de códigoCode Sample

using System;
using System.Diagnostics;
using System.Management.Automation;             //Windows PowerShell namespace

using System.ComponentModel;

// This sample shows how to create a simple cmdlet. To test this 
// cmdlet, the snapin must be registered.  First, run the command:
//     installutil GetProcessSample01.dll
// Then run:
//     Add-PSSnapin GetProcessSnapIn01
// After the snapin has been loaded, you can run:
//     get-proc

namespace Microsoft.Samples.PowerShell.Commands
{

   #region GetProcCommand

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

      /// <summary>
      /// The ProcessRecord method calls the Process.GetProcesses 
      /// method to retrieve the processes of the local computer. 
      /// Then, the WriteObject method writes the associated processes 
      /// to the pipeline.
      /// </summary>
      protected override void ProcessRecord()
      {
         // Retrieve the current processes.
         Process[] processes = Process.GetProcesses();

         // Write the processes to the pipeline to make them available
         // to the next cmdlet. The second argument (true) tells Windows 
         // PowerShell to enumerate the array and to send one process 
         // object at a time to the pipeline.
         WriteObject(processes, true);
      }

      #endregion Overrides

   } //GetProcCommand

   #endregion GetProcCommand

   #region PowerShell snap-in

   /// <summary>
   /// Create this sample as an PowerShell snap-in
   /// </summary>
   [RunInstaller(true)]
   public class GetProcPSSnapIn01 : PSSnapIn
   {
       /// <summary>
       /// Create an instance of the GetProcPSSnapIn01
       /// </summary>
       public GetProcPSSnapIn01()
           : base()
       {
       }

       /// <summary>
       /// Get a name for this PowerShell snap-in. This name will be used in registering
       /// this PowerShell snap-in.
       /// </summary>
       public override string Name
       {
           get
           {
               return "GetProcPSSnapIn01";
           }
       }

       /// <summary>
       /// Vendor information for this PowerShell snap-in.
       /// </summary>
       public override string Vendor
       {
           get
           {
               return "Microsoft";
           }
       }

       /// <summary>
       /// Gets resource information for vendor. This is a string of format: 
       /// resourceBaseName,resourceName. 
       /// </summary>
       public override string VendorResource
       {
           get
           {
               return "GetProcPSSnapIn01,Microsoft";
           }
       }

       /// <summary>
       /// Description of this PowerShell snap-in.
       /// </summary>
       public override string Description
       {
           get
           {
               return "This is a PowerShell snap-in that includes the get-proc cmdlet.";
           }
       }
   }

   #endregion PowerShell snap-in
}

Consulte tambiénSee Also

Guía del programador de Windows PowerShellWindows PowerShell Programmer's Guide

Windows PowerShell SDKWindows PowerShell SDK