Aracılığıyla paylaş


GetProcessSample02 Örneği

Bu örnek, yerel bilgisayardaki işlemleri alan bir cmdlet'in nasıl yazıldığını gösterir. Alınacak işlemleri belirtmek için kullanılabilecek 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 GetProcessSample02 klasörüne gidin. Varsayılan konum C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0\Samples\sysmgmt\WindowsPowerShell\csharp\GetProcessSample02.

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

  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 getprossessample02

  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.

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

Örnek

Bu örnek, 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 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
}

Ayrıca Bkz.