Aracılığıyla paylaş


GetProcessSample04 Örneği

Bu örnek, yerel bilgisayardaki işlemleri alan bir cmdlet'in nasıl uygulandığını gösterir. İşlem alınırken bir hata oluşursa sonlandırıcı olmayan bir hata oluşturur. 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 GetProcessSample04 klasörüne gidin. Varsayılan konum C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0\Samples\sysmgmt\WindowsPowerShell\csharp\GetProcessSample04.

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

  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 getprossessample04

  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.

  • Sonlandırılmayan bir hatayı yakalama ve hata akışına hata iletisi yazma.

Örnek

Bu örnek, sonlandırılmayan hataları işleyen ve hata akışına hata iletileri yazan bir cmdlet'in nasıl oluşturulacağı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 to act on.
       /// </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,
         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 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.
              // If a non-terminating error occurs while retrieving processes,
              // call the WriteError method to send an error record to the
              // error stream.
              foreach (string name in this.processNames)
              {
                  Process[] processes;

                  try
                  {
                      processes = Process.GetProcessesByName(name);
                  }
                  catch (InvalidOperationException ex)
                  {
                      WriteError(new ErrorRecord(
                         ex,
                         "UnableToAccessProcessByName",
                         ErrorCategory.InvalidOperation,
                         name));
                      continue;
                  }

                  WriteObject(processes, true);
              } // foreach (string name...
          } // else
      } // ProcessRecord

      #endregion Overrides
    } // End GetProcCommand class.

   #endregion GetProcCommand
}

Ayrıca Bkz.