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. 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 GetProcessSample01 klasörüne gidin. Varsayılan konum
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0\Samples\sysmgmt\WindowsPowerShell\csharp\GetProcessSample01
.Çözüm (.sln) dosyasının simgesine çift tıklayın. Bu işlem örnek projeyi Microsoft Visual Studio'da açar.
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
Bir Komut İstemi penceresi açın.
Örnek .dll dosyasını içeren dizine gidin.
installutil "GetProcessSample01.dll"
'i çalıştırın.Windows PowerShell’i başlatın.
Ek bileşeni kabuğa eklemek için aşağıdaki komutu çalıştırın.
Add-PSSnapin GetProcPSSnapIn01
Cmdlet'ini çalıştırmak için aşağıdaki komutu girin.
Get-Proc
Get-Proc
Bu, aşağıdaki adımların sonucunda elde edilen örnek bir çıktıdır.
Id Name State HasMoreData Location Command -- ---- ----- ----------- -------- ------- 1 26932870-d3b... NotStarted False Write-Host "A f...
Set-Content $Env:TEMP\test.txt "This is a test file"
A file was created in the TEMP directory
Gereksinimler
Bu örnek Için Windows PowerShell 1.0 veya üzeri gerekir.
Gösterir
Bu örnekte aşağıdakiler gösterilmektedir.
Temel bir örnek cmdlet'i oluşturma.
Cmdlet özniteliğini kullanarak bir cmdlet sınıfı tanımlama.
Hem Windows PowerShell 1.0 hem de Windows PowerShell 2.0 ile çalışan bir ek bileşen oluşturma. Sonraki örnekler, Windows PowerShell 2.0'a ihtiyaç duymaları için ek bileşenler yerine modüller kullanır.
Örnek
Bu örnek, basit bir cmdlet ve ek bileşeninin nasıl oluşturulacağını gösterir.
using System;
using System.Diagnostics;
using System.Management.Automation; //Windows PowerShell namespace
using System.ComponentModel;
namespace Microsoft.Samples.PowerShell.Commands
{
#region GetProcCommand
/// <summary>
/// This class implements the Get-Proc cmdlet.
/// </summary>
[Cmdlet(VerbsCommon.Get, "Proc")]
public class GetProcCommand : Cmdlet
{
#region Cmdlet Overrides
/// <summary>
/// The ProcessRecord method calls the Process.GetProcesses
/// method to retrieve the processes of the local computer.
/// Then, the WriteObject method writes the associated processes
/// to the pipeline.
/// </summary>
protected override void ProcessRecord()
{
// Retrieve the current processes.
Process[] processes = Process.GetProcesses();
// Write the processes to the pipeline to make them available
// to the next cmdlet. The second argument (true) tells Windows
// PowerShell to enumerate the array and to send one process
// object at a time to the pipeline.
WriteObject(processes, true);
}
#endregion Overrides
} //GetProcCommand
#endregion GetProcCommand
#region PowerShell snap-in
/// <summary>
/// Create this sample as a PowerShell snap-in
/// </summary>
[RunInstaller(true)]
public class GetProcPSSnapIn01 : PSSnapIn
{
/// <summary>
/// Create an instance of the GetProcPSSnapIn01
/// </summary>
public GetProcPSSnapIn01()
: base()
{
}
/// <summary>
/// Get a name for this PowerShell snap-in. This name will be used in registering
/// this PowerShell snap-in.
/// </summary>
public override string Name
{
get
{
return "GetProcPSSnapIn01";
}
}
/// <summary>
/// Vendor information for this PowerShell snap-in.
/// </summary>
public override string Vendor
{
get
{
return "Microsoft";
}
}
/// <summary>
/// Gets resource information for vendor. This is a string of format:
/// resourceBaseName,resourceName.
/// </summary>
public override string VendorResource
{
get
{
return "GetProcPSSnapIn01,Microsoft";
}
}
/// <summary>
/// Description of this PowerShell snap-in.
/// </summary>
public override string Description
{
get
{
return "This is a PowerShell snap-in that includes the Get-Proc cmdlet.";
}
}
}
#endregion PowerShell snap-in
}
Ayrıca Bkz.
PowerShell