Udostępnij za pomocą


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 Get-Process udostępnianego przez program Windows PowerShell 2.0.

Jak utworzyć przykład przy użyciu programu Visual Studio

  1. Po zainstalowaniu zestawu 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 programie Microsoft Visual Studio.

  3. W menu Kompilacja wybierz pozycję Build Solution, aby skompilować bibliotekę dla przykładu 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 plik .dll.

  3. Uruchom program 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

    Jest to przykładowe dane wyjściowe, które wynika z poniższych 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 programu Windows PowerShell 1.0 lub nowszego.

Demonstruje

W tym przykładzie pokazano następujące elementy.

  • Tworzenie podstawowego przykładowego polecenia cmdlet.

  • Definiowanie klasy poleceń cmdlet przy użyciu atrybutu Cmdlet.

  • Tworzenie przystawki, która współdziała zarówno z programem Windows PowerShell 1.0, jak i programem Windows PowerShell 2.0. Kolejne przykłady używają modułów zamiast przystawek, więc wymagają programu 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 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
}

Zobacz też