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.
Cmdlet'lerinizi tasarlarken, aynı veri parçası üzerinde çeşitli eylemler gerçekleştirmeniz gereken durumlarla karşılaşabilirsiniz. Örneğin, verileri alıp ayarlamanız veya bir işlemi başlatmanız ve durdurmanız gerekebilir. Her eylemi gerçekleştirmek için ayrı cmdlet'ler oluşturmanız gerekse de, cmdlet tasarımınız tek tek cmdlet'ler için sınıfların türetildiği bir temel sınıf içermelidir.
Temel sınıf uygularken aşağıdakileri göz önünde bulundurun.
Temel sınıftaki tüm türetilmiş cmdlet'ler tarafından kullanılan tüm ortak parametreleri bildirin.
Uygun cmdlet sınıfına cmdlet'e özgü parametreler ekleyin.
Temel sınıfta uygun giriş işleme yöntemini geçersiz kılın.
Tüm cmdlet sınıflarında System.Management.Automation.CmdletAttribute özniteliğini bildirin, ancak temel sınıfta bildirmeyin.
Adı ve açıklaması cmdlet kümesini yansıtan bir System.Management.Automation.PSSnapIn veya System.Management.Automation.CustomPSSnapIn sınıfı uygulayın.
Örnek
Aşağıdaki örnek, aynı temel sınıftan türetilen Get-Proc ve Stop-Proc cmdlet'i tarafından kullanılan bir temel sınıfın uygulamasını gösterir.
using System;
using System.Diagnostics;
using System.Management.Automation; //Windows PowerShell namespace.
namespace Microsoft.Samples.PowerShell.Commands
{
#region ProcessCommands
/// <summary>
/// This class implements a Stop-Proc cmdlet. The parameters
/// for this cmdlet are defined by the BaseProcCommand class.
/// </summary>
[Cmdlet(VerbsLifecycle.Stop, "Proc", SupportsShouldProcess = true)]
public class StopProcCommand : BaseProcCommand
{
public override void ProcessObject(Process process)
{
if (ShouldProcess(process.ProcessName, "Stop-Proc"))
{
process.Kill();
}
}
}
/// <summary>
/// This class implements a Get-Proc cmdlet. The parameters
/// for this cmdlet are defined by the BaseProcCommand class.
/// </summary>
[Cmdlet(VerbsCommon.Get, "Proc")]
public class GetProcCommand : BaseProcCommand
{
public override void ProcessObject(Process process)
{
WriteObject(process);
}
}
/// <summary>
/// This class is the base class that defines the common
/// functionality used by the Get-Proc and Stop-Proc
/// cmdlets.
/// </summary>
public class BaseProcCommand : Cmdlet
{
#region Parameters
// Defines the Name parameter that is used to
// specify a process by its name.
[Parameter(
Position = 0,
Mandatory = true,
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true
)]
public string[] Name
{
get { return processNames; }
set { processNames = value; }
}
private string[] processNames;
// Defines the Exclude parameter that is used to
// specify which processes should be excluded when
// the cmdlet performs its action.
[Parameter()]
public string[] Exclude
{
get { return excludeNames; }
set { excludeNames = value; }
}
private string[] excludeNames = new string[0];
#endregion Parameters
public virtual void ProcessObject(Process process)
{
throw new NotImplementedException("This method should be overridden.");
}
#region Cmdlet Overrides
// <summary>
// For each of the requested process names, retrieve and write
// the associated processes.
// </summary>
protected override void ProcessRecord()
{
// Set up the wildcard characters used in resolving
// the process names.
WildcardOptions options = WildcardOptions.IgnoreCase |
WildcardOptions.Compiled;
WildcardPattern[] include = new WildcardPattern[Name.Length];
for (int i = 0; i < Name.Length; i++)
{
include[i] = new WildcardPattern(Name[i], options);
}
WildcardPattern[] exclude = new WildcardPattern[Exclude.Length];
for (int i = 0; i < Exclude.Length; i++)
{
exclude[i] = new WildcardPattern(Exclude[i], options);
}
foreach (Process p in Process.GetProcesses())
{
foreach (WildcardPattern wIn in include)
{
if (wIn.IsMatch(p.ProcessName))
{
bool processThisOne = true;
foreach (WildcardPattern wOut in exclude)
{
if (wOut.IsMatch(p.ProcessName))
{
processThisOne = false;
break;
}
}
if (processThisOne)
{
ProcessObject(p);
}
break;
}
}
}
}
#endregion Cmdlet Overrides
}
#endregion ProcessCommands
}
Ayrıca Bkz.
PowerShell