GetProcessSample01 – minta

Ez a példa bemutatja, hogyan valósítható meg egy parancsmag, amely lekéri a folyamatokat a helyi számítógépen. Ez a parancsmag a parancsmag egyszerűsített verziója, amelyet a Get-Process 2.0 Windows PowerShell biztosít.

A minta összeállítása a Visual Studio.

  1. A Windows PowerShell 2.0 SDK telepítése után keresse meg a GetProcessSample01 mappát. Az alapértelmezett hely a következő: C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0\Samples\sysmgmt\WindowsPowerShell\csharp\GetProcessSample01.

  2. Kattintson duplán a megoldásfájl (.sln) ikonjára. Ez megnyitja a mintaprojektet a Microsoft Visual Studio.

  3. A Build (Build) menüben válassza a Build Solution (Megoldás összeállítása) lehetőséget.

A minta könyvtára az alapértelmezett \bin vagy \bin\debug mappában lesz felépítve.

A minta futtatása

  1. Nyisson meg egy parancssori ablakot.

  2. Lépjen a mintafájlt tartalmazó .dll könyvtárba.

  3. Futtassa az installutil "GetProcessSample01.dll" parancsokat.

  4. Indítsa el a Windows PowerShellt.

  5. A beépülő modul a rendszerhéjhoz való hozzáadásához futtassa a következő parancsot.

    Add-PSSnapin GetProcPSSnapIn01

  6. A parancsmag futtatásához írja be a következő parancsot. get-proc

    get-proc

    Ez egy mintakimenet, amely az alábbi lépésekből ad vissza eredményt.

    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
    

Követelmények

Ehhez a mintához Windows PowerShell 1.0-s vagy újabb szükséges.

Útmutató ehhez:

Ez a minta a következőket mutatja be.

  • Alapszintű minta parancsmag létrehozása.

  • Parancsmagosztály meghatározása a Parancsmag attribútum használatával.

  • Az 1.0-s és Windows PowerShell 2.0-s Windows PowerShell is működik. A további minták beépülő modulok helyett modulokat fognak használni, ezért Windows PowerShell 2.0-s.

Példa

Ez a példa bemutatja, hogyan hozhat létre egy egyszerű parancsmagot és annak beépülő modulját.

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
}

Lásd még:

Windows PowerShell-parancsmag írása