Not
Bu sayfaya erişim yetkilendirme gerektiriyor. Oturum açmayı veya dizinleri değiştirmeyi deneyebilirsiniz.
Bu sayfaya erişim yetkilendirme gerektiriyor. Dizinleri değiştirmeyi deneyebilirsiniz.
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
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.Çözüm (.sln) dosyasının simgesine çift tıklayın. Bu işlem örnek projeyi Visual Studio'da açar.
Derleme menüsünde Çözümü Derle 'ı seçerek varsayılan
\binveya\bin\debugklasörlerinde örneğin kitaplığını oluşturun.
Örneği çalıştırma
Aşağıdaki modül klasörünü oluşturun:
[user]\Documents\WindowsPowerShell\Modules\GetProcessSample04Örnek derlemeyi modül klasörüne kopyalayın.
Windows PowerShell’i başlatın.
Derlemeyi Windows PowerShell'e yüklemek için aşağıdaki komutu çalıştırın:
Import-Module getprossessample04Cmdlet'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.
PowerShell