Bagikan melalui


Sampel GetProcessSample02

Sampel ini menunjukkan cara menulis cmdlet yang mengambil proses di komputer lokal. Ini menyediakan parameter Name yang dapat digunakan untuk menentukan proses yang akan diambil. Cmdlet ini adalah versi yang disederhanakan dari cmdlet Get-Process yang disediakan oleh Windows PowerShell 2.0.

Cara membuat sampel menggunakan Visual Studio

  1. Dengan Windows PowerShell 2.0 SDK terinstal, navigasikan ke folder GetProcessSample02. Lokasi default adalah C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0\Samples\sysmgmt\WindowsPowerShell\csharp\GetProcessSample02.

  2. Klik dua kali ikon untuk file solusi (.sln). Ini membuka proyek sampel di Visual Studio.

  3. Di menu Build, pilih Build Solution untuk membangun pustaka sampel di folder \bin atau \bin\debug default.

Cara menjalankan sampel

  1. Buat folder modul berikut:

    [user]\Documents\WindowsPowerShell\Modules\GetProcessSample02

  2. Salin rakitan sampel ke folder modul.

  3. Mulai Windows PowerShell.

  4. Jalankan perintah berikut untuk memuat rakitan ke Windows PowerShell:

    Import-Module getprossessample02

  5. Jalankan perintah berikut untuk menjalankan cmdlet:

    Get-Proc

Persyaratan

Sampel ini memerlukan Windows PowerShell 2.0.

Menunjukkan

Sampel ini menunjukkan hal berikut.

  • Mendeklarasikan kelas cmdlet menggunakan atribut Cmdlet.

  • Mendeklarasikan parameter cmdlet menggunakan atribut Parameter.

  • Menentukan posisi parameter.

  • Mendeklarasikan atribut validasi untuk input parameter.

Contoh

Sampel ini menunjukkan implementasi cmdlet Get-Proc yang menyertakan parameter Name.

namespace Microsoft.Samples.PowerShell.Commands
{
  using System;
  using System.Diagnostics;
  using System.Management.Automation;     // Windows PowerShell namespace

  #region GetProcCommand

  /// <summary>
  /// This class implements the Get-Proc cmdlet.
  /// </summary>
  [Cmdlet(VerbsCommon.Get, "Proc")]
  public class GetProcCommand : Cmdlet
  {
    #region Parameters

    /// <summary>
    /// The names of the processes retrieved by the cmdlet.
    /// </summary>
    private string[] processNames;

    /// <summary>
    /// Gets or sets the list of process names on which
    /// the Get-Proc cmdlet will work.
    /// </summary>
    [Parameter(Position = 0)]
    [ValidateNotNullOrEmpty]
    public string[] Name
    {
      get { return this.processNames; }
      set { this.processNames = value; }
    }

    #endregion Parameters

    #region Cmdlet Overrides

    /// <summary>
    /// The ProcessRecord method calls the Process.GetProcesses
    /// method to retrieve the processes specified by the Name
    /// parameter. Then, the WriteObject method writes the
    /// associated process objects to the pipeline.
    /// </summary>
    protected override void ProcessRecord()
    {
      // If no process names are passed to the cmdlet, get all
      // processes.
      if (this.processNames == null)
      {
        WriteObject(Process.GetProcesses(), true);
      }
      else
      {
        // If process names are passed to cmdlet, get and write
        // the associated processes.
        foreach (string name in this.processNames)
        {
          WriteObject(Process.GetProcessesByName(name), true);
        }
      } // End if (processNames...).
    } // End ProcessRecord.
    #endregion Cmdlet Overrides
  } // End GetProcCommand class.
  #endregion GetProcCommand
}

Lihat Juga