Sdílet prostřednictvím


Ukázka GetProcessSample01

Tato ukázka ukazuje, jak implementovat rutinu, která načte procesy v místním počítači. Tato rutina je zjednodušená verze rutiny Get-Process, kterou poskytuje Windows PowerShell 2.0.

Postup sestavení ukázky pomocí sady Visual Studio

  1. S nainstalovanou sadou Windows PowerShell 2.0 SDK přejděte do složky GetProcessSample01. Výchozí umístění je C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0\Samples\sysmgmt\WindowsPowerShell\csharp\GetProcessSample01.

  2. Poklikejte na ikonu souboru řešení (.sln). Tím se otevře ukázkový projekt v sadě Microsoft Visual Studio.

  3. V nabídce Sestavení vyberte Sestavit řešení a sestavte knihovnu ukázky ve výchozích \bin nebo \bin\debug složkách.

Spuštění ukázky

  1. Otevřete okno příkazového řádku.

  2. Přejděte do adresáře obsahujícího ukázkový soubor .dll.

  3. Spusťte installutil "GetProcessSample01.dll".

  4. Spusťte Windows PowerShell.

  5. Spuštěním následujícího příkazu přidejte modul snap-in do prostředí.

    Add-PSSnapin GetProcPSSnapIn01

  6. Spuštěním rutiny zadejte následující příkaz. Get-Proc

    Get-Proc

    Toto je ukázkový výstup, který je výsledkem následujících kroků.

    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
    

Požadavky

Tato ukázka vyžaduje Windows PowerShell 1.0 nebo novější.

Demonstruje

Tato ukázka ukazuje následující:

  • Vytvoření základní ukázkové rutiny

  • Definování třídy rutiny pomocí atributu Rutina.

  • Vytvoření modulu snap-in, který funguje s Prostředím Windows PowerShell 1.0 i Windows PowerShellem 2.0 Následující ukázky používají moduly místo modulů snap-in, aby vyžadovaly Prostředí Windows PowerShell 2.0.

Příklad

Tato ukázka ukazuje, jak vytvořit jednoduchou rutinu a její modul 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 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
}

Viz také