Bagikan melalui


Sampel GetProcessSample03

Sampel ini menunjukkan cara mengimplementasikan cmdlet yang mengambil proses di komputer lokal. Ini menyediakan parameter Name yang dapat menerima objek dari alur atau nilai dari properti objek yang nama propertinya sama dengan nama parameter. 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 GetProcessSample03. Lokasi default adalah C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0\Samples\sysmgmt\WindowsPowerShell\csharp\GetProcessSample03.

  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\GetProcessSample03

  2. Salin rakitan sampel ke folder modul.

  3. Mulai Windows PowerShell.

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

    Import-Module getprossessample03

  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.

  • Menentukan bahwa parameter mengambil input dari alur. Input dapat diambil dari objek atau nilai dari properti objek yang nama propertinya sama dengan nama parameter.

  • Mendeklarasikan atribut validasi untuk input parameter.

Contoh

Sampel ini menunjukkan implementasi cmdlet Get-Proc yang menyertakan parameter Name yang menerima input dari alur.

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 names of the
    /// process that the cmdlet will retrieve.
    /// </summary>
    [Parameter(
               Position = 0,
               ValueFromPipeline = true,
               ValueFromPipelineByPropertyName = true)]
    [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 processes 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 the cmdlet, get and write
        // the associated processes.
        foreach (string name in this.processNames)
        {
          WriteObject(Process.GetProcessesByName(name), true);
        }
      } // End if (processNames ...)
    } // End ProcessRecord.

    #endregion Overrides
  } // End GetProcCommand.
  #endregion GetProcCommand
}

Lihat Juga