Aracılığıyla paylaş


GetProcessSample03 Örneği

Bu örnek, yerel bilgisayardaki işlemleri alan bir cmdlet'in nasıl uygulandığını gösterir. İşlem hattından bir nesneyi veya özellik adı parametre adıyla aynı olan bir nesnenin özelliğinden bir değeri kabul edebilen bir Name parametresi sağlar. Bu cmdlet, Windows PowerShell 2.0 tarafından sağlanan Get-Process cmdlet'in basitleştirilmiş bir sürümüdür.

Visual Studio kullanarak örnek oluşturma

  1. Windows PowerShell 2.0 SDK'sı yüklü olarak GetProcessSample03 klasörüne gidin. Varsayılan konum C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0\Samples\sysmgmt\WindowsPowerShell\csharp\GetProcessSample03.

  2. Çözüm (.sln) dosyasının simgesine çift tıklayın. Bu işlem örnek projeyi Visual Studio'da açar.

  3. Derleme menüsünde Çözümü Derle 'ı seçerek varsayılan \bin veya \bin\debug klasörlerinde örneğin kitaplığını oluşturun.

Örneği çalıştırma

  1. Aşağıdaki modül klasörünü oluşturun:

    [user]\Documents\WindowsPowerShell\Modules\GetProcessSample03

  2. Örnek derlemeyi modül klasörüne kopyalayın.

  3. Windows PowerShell’i başlatın.

  4. Derlemeyi Windows PowerShell'e yüklemek için aşağıdaki komutu çalıştırın:

    Import-Module getprossessample03

  5. Cmdlet'ini çalıştırmak için aşağıdaki komutu çalıştırın:

    Get-Proc

Gereksinimler

Bu örnek Için Windows PowerShell 2.0 gerekir.

Gösterir

Bu örnekte aşağıdakiler gösterilmektedir.

  • Cmdlet özniteliğini kullanarak bir cmdlet sınıfı bildirme.

  • Parameter özniteliğini kullanarak bir cmdlet parametresi bildirme.

  • Parametresinin konumunu belirtme.

  • parametresinin işlem hattından giriş aldığını belirtme. Giriş, bir nesneden veya özellik adı parametre adıyla aynı olan bir nesnenin özelliğinden alınabilir.

  • Parametre girişi için doğrulama özniteliği bildiriyor.

Örnek

Bu örnek, işlem hattından giriş kabul eden bir Name parametresi içeren Get-Proc cmdlet'in uygulamasını gösterir.

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
}

Ayrıca Bkz.