Ejemplo GetProcessSample01GetProcessSample01 Sample

Este ejemplo muestra cómo implementar un cmdlet que recupera los procesos en el equipo local.This sample shows how to implement a cmdlet that retrieves the processes on the local computer. Este cmdlet es una versión simplificada del Get-Process cmdlet proporcionado por Windows PowerShell 2,0.This cmdlet is a simplified version of the Get-Process cmdlet that is provided by Windows PowerShell 2.0.

Cómo compilar el ejemplo mediante Visual Studio.How to build the sample by using Visual Studio.

  1. Con el SDK de Windows PowerShell 2,0 instalado, vaya a la carpeta GetProcessSample01.With the Windows PowerShell 2.0 SDK installed, navigate to the GetProcessSample01 folder. La ubicación predeterminada es C:\Archivos de programa (x86) \Microsoft SDKs\Windows\v7.0\Samples\sysmgmt\WindowsPowerShell\csharp\GetProcessSample01.The default location is C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0\Samples\sysmgmt\WindowsPowerShell\csharp\GetProcessSample01.

  2. Haga doble clic en el icono del archivo de solución (. sln).Double-click the icon for the solution (.sln) file. Se abrirá el proyecto de ejemplo en Microsoft Visual Studio.This opens the sample project in Microsoft Visual Studio.

  3. En el menú Compilar, seleccione Compilar solución.In the Build menu, select Build Solution.

La biblioteca del ejemplo se generará en las carpetas \Bin o \bin\debug predeterminadas.The library for the sample will be built in the default \bin or \bin\debug folders.

Ejecución del ejemploHow to run the sample

  1. Abra una ventana de símbolo del sistema.Open a Command Prompt window.

  2. Navegue hasta el directorio que contiene el archivo. dll de ejemplo.Navigate to the directory containing the sample .dll file.

  3. Ejecute InstallUtil "GetProcessSample01.dll".Run installutil "GetProcessSample01.dll".

  4. Inicie Windows PowerShell.Start Windows PowerShell.

  5. Ejecute el siguiente comando para agregar el complemento al shell.Run the following command to add the snap-in to the shell.

    Add-PSSnapin GetProcPSSnapIn01

  6. Escriba el siguiente comando para ejecutar el cmdlet.Enter the following command to run the cmdlet. get-proc

    get-proc

    Esta es una salida de ejemplo que se obtiene al seguir estos pasos.This is a sample output that results from following these steps.

    Id              Name            State      HasMoreData     Location             Command
    --              ----            -----      -----------     --------             -------
    1               26932870-d3b... NotStarted False                                 Write-Host "A f...
    
    
    Set-Content $env:temp\test.txt "This is a test file"
    
    A file was created in the TEMP directory
    

RequisitosRequirements

Este ejemplo requiere Windows PowerShell 1,0 o posterior.This sample requires Windows PowerShell 1.0 or later.

MuestraDemonstrates

Este ejemplo muestra lo siguiente.This sample demonstrates the following.

  • Crear un cmdlet de ejemplo básico.Creating a basic sample cmdlet.

  • Definir una clase de cmdlet mediante el atributo de cmdlet.Defining a cmdlet class by using the Cmdlet attribute.

  • Crear un complemento que funcione con Windows PowerShell 1,0 y Windows PowerShell 2,0.Creating a snap-in that works with both Windows PowerShell 1.0 and Windows PowerShell 2.0. Los ejemplos siguientes usan módulos en lugar de complementos para que requieran Windows PowerShell 2,0.Subsequent samples use modules instead of snap-ins so they require Windows PowerShell 2.0.

EjemploExample

En este ejemplo se muestra cómo crear un cmdlet simple y su complemento.This sample shows how to create a simple cmdlet and its snap-in.

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

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

Escribir un cmdlet de Windows PowerShellWriting a Windows PowerShell Cmdlet