Bir Cmdlet İçinden Bir Cmdlet Nasıl Çağırılır
Bu örnek, [System.Management.Automation.Cmdlet]'ten türetilen bir ikili cmdlet'in doğrudan başka bir ikili cmdlet'ten nasıl çağrılacağını gösterir; bu, çağrılan cmdlet'in işlevselliğini ikili cmdlet'e eklemenize olanak tanır gelişiyorsun. Bu örnekte, yerel bilgisayarda çalışan işlemleri almak için Get-Process cmdlet'i çağrıldı. Get-Process cmdlet'ine yapılan çağrı aşağıdaki komuta eşdeğerdir. Bu komut, adları "a" ile "t" arasındaki karakterlerle başlayan tüm işlemleri getirir.
Get-Process -name [a-t]*
Bu önemli
Yalnızca doğrudan System.Management.Automation.Cmdlet sınıfından türetilen cmdlet'leri çağırabilirsiniz. System.Management.Automation.PSCmdlet sınıfından türetilen bir cmdlet'i çağıramazsınız. Örnek için bkz. PSCmdlet'in içinden PSCmdlet'in nasıl çağrılacağı.
Bir cmdlet'in içinden bir cmdlet'i çağırmak için
Çağrılacak cmdlet'i tanımlayan derlemeye başvurulduğundan ve uygun
usingifadesinin eklendiğinden emin olun. Bu örnekte aşağıdaki ad alanları eklenmiştir.using System.Diagnostics; using System.Management.Automation; // PowerShell assembly. using Microsoft.PowerShell.Commands; // PowerShell cmdlets assembly you want to call.Cmdlet'in giriş işleme yönteminde, çağrılacak cmdlet'in yeni bir örneğini oluşturun. Bu örnekte, cmdlet çağrıldığında kullanılan bağımsız değişkenleri içeren dizeyle birlikte Microsoft.PowerShell.Commands.GetProcessCommand türünde bir nesne oluşturulur.
GetProcessCommand gp = new GetProcessCommand(); gp.Name = new string[] { "[a-t]*" };-
Get-Processcmdlet'ini çağırmak için System.Management.Automation.Cmdlet.Invoke* yöntemini çağırın.foreach (Process p in gp.Invoke<Process>()) { Console.WriteLine(p.ToString()); } }
Örnek
Bu örnekte, Get-Process cmdlet'i, bir cmdlet'in System.Management.Automation.Cmdlet.BeginProcessing yöntemi içinden çağrılır.
using System;
using System.Diagnostics;
using System.Management.Automation; // PowerShell assembly.
using Microsoft.PowerShell.Commands; // PowerShell cmdlets assembly you want to call.
namespace SendGreeting
{
// Declare the class as a cmdlet and specify an
// appropriate verb and noun for the cmdlet name.
[Cmdlet(VerbsCommunications.Send, "GreetingInvoke")]
public class SendGreetingInvokeCommand : Cmdlet
{
// Declare the parameters for the cmdlet.
[Parameter(Mandatory = true)]
public string Name { get; set; }
// Override the BeginProcessing method to invoke
// the Get-Process cmdlet.
protected override void BeginProcessing()
{
GetProcessCommand gp = new GetProcessCommand();
gp.Name = new string[] { "[a-t]*" };
foreach (Process p in gp.Invoke<Process>())
{
WriteVerbose(p.ToString());
}
}
// Override the ProcessRecord method to process
// the supplied user name and write out a
// greeting to the user by calling the WriteObject
// method.
protected override void ProcessRecord()
{
WriteObject("Hello " + Name + "!");
}
}
}
Ayrıca Bakınız
Windows PowerShell Cmdlet'i Yazma