Voorbeeld GetProcessSample01

In dit voorbeeld ziet u hoe u een cmdlet implementeert die de processen op de lokale computer op haalt. Deze cmdlet is een vereenvoudigde versie van de Get-Process cmdlet die wordt geleverd door Windows PowerShell 2.0.

Het voorbeeld bouwen met behulp van Visual Studio.

  1. Als de Windows PowerShell 2.0 SDK is geïnstalleerd, gaat u naar de map GetProcessSample01. De standaardlocatie is C:\Program Files (x86)\Microsoft SDK's\Windows\v7.0\Samples\sysmgmt\WindowsPowerShell\csharp\GetProcessSample01.

  2. Dubbelklik op het pictogram voor het oplossingsbestand (.sln). Hiermee opent u het voorbeeldproject in Microsoft Visual Studio.

  3. Selecteer in het menu Bouwen de optie Oplossing bouwen.

De bibliotheek voor het voorbeeld wordt gebouwd in de standaardmappen \bin of \bin\debug.

Het voorbeeld uitvoeren

  1. Open een opdrachtpromptvenster.

  2. Navigeer naar de map met het .dll bestand.

  3. Voer installutil 'GetProcessSample01.dll' uit.

  4. Start Windows PowerShell.

  5. Voer de volgende opdracht uit om de module toe te voegen aan de shell.

    Add-PSSnapin GetProcPSSnapIn01

  6. Voer de volgende opdracht in om de cmdlet uit te voeren. get-proc

    get-proc

    Dit is een voorbeelduitvoer die het resultaat is van het volgen van deze stappen.

    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
    

Vereisten

Voor dit voorbeeld is Windows PowerShell 1.0 of hoger vereist.

Demonstreert

In dit voorbeeld wordt het volgende gedemonstreerd.

  • Een eenvoudige voorbeeld-cmdlet maken.

  • Een cmdlet-klasse definiëren met behulp van het kenmerk Cmdlet.

  • Een module maken die werkt met zowel Windows PowerShell 1.0 als Windows PowerShell 2.0. In volgende voorbeelden worden modules in plaats van modules gebruikt, dus ze vereisen Windows PowerShell 2.0.

Voorbeeld

In dit voorbeeld ziet u hoe u een eenvoudige cmdlet en de module maakt.

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
}

Zie ook

Een Windows PowerShell-cmdlet schrijven