Przykład GetProcessSample01

W tym przykładzie pokazano, jak zaimplementować polecenie cmdlet, które pobiera procesy na komputerze lokalnym. To polecenie cmdlet jest uproszczoną wersją polecenia cmdlet dostarczanego przez program Get-Process Windows PowerShell 2.0.

Jak skompilować przykład przy użyciu Visual Studio.

  1. Po zainstalowaniu Windows PowerShell 2.0 SDK przejdź do folderu GetProcessSample01. Domyślna lokalizacja to C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0\Samples\sysmgmt\WindowsPowerShell\csharp\GetProcessSample01.

  2. Kliknij dwukrotnie ikonę pliku rozwiązania (sln). Spowoduje to otwarcie przykładowego projektu w Microsoft Visual Studio.

  3. W menu Kompilacja wybierz pozycję Build Solution (Skompilowanie rozwiązania).

Biblioteka przykładu zostanie s zbudowana w domyślnych folderach \bin lub \bin\debug.

Jak uruchomić przykład

  1. Otwórz okno wiersza polecenia.

  2. Przejdź do katalogu zawierającego przykładowy .dll plik.

  3. Uruchom installutil "GetProcessSample01.dll".

  4. Uruchom program Windows PowerShell.

  5. Uruchom następujące polecenie, aby dodać przystawkę do powłoki.

    Add-PSSnapin GetProcPSSnapIn01

  6. Wprowadź następujące polecenie, aby uruchomić polecenie cmdlet . get-proc

    get-proc

    Są to przykładowe dane wyjściowe, które są skutkiem następujących kroków.

    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
    

Wymagania

Ten przykład wymaga Windows PowerShell 1.0 lub nowszej.

Demonstracje

W tym przykładzie przedstawiono następujące informacje.

  • Tworzenie podstawowego przykładowego polecenia cmdlet.

  • Definiowanie klasy polecenia cmdlet przy użyciu atrybutu polecenia cmdlet.

  • Tworzenie przystawki, która działa zarówno z programem Windows PowerShell 1.0, jak i Windows PowerShell 2.0. Kolejne przykłady używają modułów zamiast przyciągnijek, więc wymagają Windows PowerShell 2.0.

Przykład

W tym przykładzie pokazano, jak utworzyć proste polecenie cmdlet i jego przystawkę.

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
}

Zobacz też

Pisanie polecenia cmdlet programu Windows PowerShell