Freigeben über


GetProcessSample01-Beispiel

In diesem Beispiel wird gezeigt, wie Sie ein Cmdlet implementieren, das die Prozesse auf dem lokalen Computer abruft. Dieses Cmdlet ist eine vereinfachte Version des cmdlets Get-Process, das von Windows PowerShell 2.0 bereitgestellt wird.

Erstellen des Beispiels mithilfe von Visual Studio

  1. Navigieren Sie mit dem installierten Windows PowerShell 2.0 SDK zum Ordner "GetProcessSample01". Der Standardspeicherort ist C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0\Samples\sysmgmt\WindowsPowerShell\csharp\GetProcessSample01.

  2. Doppelklicken Sie auf das Symbol für die Lösungsdatei (.sln). Dadurch wird das Beispielprojekt in Microsoft Visual Studio geöffnet.

  3. Wählen Sie im Menü ErstellenProjektmappe erstellen aus, um die Bibliothek für das Beispiel in den Standardordnern \bin oder \bin\debug zu erstellen.

Wie das Beispiel ausgeführt wird

  1. Öffnen Sie ein Eingabeaufforderungsfenster.

  2. Navigieren Sie zu dem Verzeichnis, das das Beispiel .dll Datei enthält.

  3. Führen Sie installutil "GetProcessSample01.dll"aus.

  4. Starten Sie Windows PowerShell.

  5. Führen Sie den folgenden Befehl aus, um das Snap-In der Shell hinzuzufügen.

    Add-PSSnapin GetProcPSSnapIn01

  6. Geben Sie den folgenden Befehl ein, um das Cmdlet auszuführen. Get-Proc

    Get-Proc

    Dies ist eine Beispielausgabe, die aus den folgenden Schritten resultiert.

    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
    

Anforderungen

In diesem Beispiel ist Windows PowerShell 1.0 oder höher erforderlich.

Veranschaulichung

In diesem Beispiel wird Folgendes veranschaulicht.

  • Erstellen eines einfachen Beispiel-Cmdlets.

  • Definieren einer Cmdlet-Klasse mithilfe des Cmdlet-Attributs.

  • Erstellen eines Snap-Ins, das sowohl mit Windows PowerShell 1.0 als auch mit Windows PowerShell 2.0 funktioniert. In nachfolgenden Beispielen werden Module anstelle von Snap-Ins verwendet, sodass windows PowerShell 2.0 erforderlich ist.

Beispiel

In diesem Beispiel wird gezeigt, wie Sie ein einfaches Cmdlet und dessen Snap-In erstellen.

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 a 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
}

Siehe auch