Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
In questo esempio viene illustrato come richiamare un cmdlet binario che deriva da [System.Management.Automation.Cmdlet]
direttamente dall'interno di un altro cmdlet binario, che consente di aggiungere la funzionalità del cmdlet richiamato al cmdlet binario in fase di sviluppo. In questo esempio viene richiamato il cmdlet Get-Process
per ottenere i processi in esecuzione nel computer locale. La chiamata al cmdlet Get-Process
equivale al comando seguente. Questo comando recupera tutti i processi i cui nomi iniziano con i caratteri "a" tramite "t".
Get-Process -Name [a-t]*
Importante
È possibile richiamare solo i cmdlet che derivano direttamente dalla classe System.Management.Automation.Cmdlet. Non è possibile richiamare un cmdlet che deriva dalla classe System.Management.Automation.PSCmdlet. Per un esempio, vedere How to invoke a PSCmdlet from within a PSCmdlet.
Assicurarsi che all'assembly che definisce il cmdlet da richiamare venga fatto riferimento e che venga aggiunta l'istruzione
using
appropriata. In questo esempio vengono aggiunti gli spazi dei nomi seguenti.using System.Diagnostics; using System.Management.Automation; // PowerShell assembly. using Microsoft.PowerShell.Commands; // PowerShell cmdlets assembly you want to call.
Nel metodo di elaborazione di input del cmdlet creare una nuova istanza del cmdlet da richiamare. In questo esempio viene creato un oggetto di tipo Microsoft.PowerShell.Commands.GetProcessCommand insieme alla stringa che contiene gli argomenti utilizzati quando viene richiamato il cmdlet.
GetProcessCommand gp = new GetProcessCommand(); gp.Name = new string[] { "[a-t]*" };
Chiamare il metodo System.Management.Automation.Cmdlet.Invoke* per richiamare il cmdlet
Get-Process
.foreach (Process p in gp.Invoke<Process>()) { Console.WriteLine(p.ToString()); } }
In questo esempio, il cmdlet Get-Process
viene richiamato dall'interno del metodo System.Management.Automation.Cmdlet.BeginProcessing di un cmdlet.
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 + "!");
}
}
}
Feedback su PowerShell
PowerShell è un progetto di open source. Selezionare un collegamento per fornire feedback: