Esempio di GetProcessSample02

In questo esempio viene illustrato come scrivere un cmdlet che recupera i processi nel computer locale. Fornisce un Name parametro che può essere usato per specificare i processi da recuperare. Questo cmdlet è una versione semplificata del Get-Process cmdlet fornito da Windows PowerShell 2.0.

Come compilare l'esempio usando Visual Studio.

  1. Dopo avere Windows PowerShell SDK 2.0, passare alla cartella GetProcessSample02. Il percorso predefinito è C:\Programmi (x86)\Microsoft SDKs\Windows\v7.0\Samples\sysmgmt\WindowsPowerShell\csharp\GetProcessSample02.

  2. Fare doppio clic sull'icona per il file di soluzione (con estensione sln). Verrà aperto il progetto di esempio in Visual Studio.

  3. Scegliere Compila soluzione dal menu Compila.

    La libreria per l'esempio verrà compilata nelle cartelle predefinite \bin o \bin\debug.

Per eseguire l'esempio

  1. Creare la cartella del modulo seguente:

    [user]/documents/windowspowershell/modules/GetProcessSample02

  2. Copiare l'assembly di esempio nella cartella del modulo.

  3. Avviare Windows PowerShell.

  4. Eseguire il comando seguente per caricare l'assembly in Windows PowerShell:

    import-module getprossessample02

  5. Eseguire il comando seguente per eseguire il cmdlet :

    get-proc

Requisiti

Questo esempio richiede Windows PowerShell 2.0.

Dimostra

In questo esempio viene illustrato quanto segue.

  • Dichiarazione di una classe di cmdlet tramite l'attributo Cmdlet.

  • Dichiarazione di un parametro del cmdlet tramite l'attributo Parameter.

  • Specifica della posizione del parametro.

  • Dichiarazione di un attributo di convalida per l'input del parametro.

Esempio

Questo esempio illustra un'implementazione del cmdlet Get-Proc che include un Name parametro .

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 retrieved by the cmdlet.
    /// </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)]
    [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 process objects to the pipeline.
    /// </summary>
    protected override void ProcessRecord()
    {
      // If no process names are passed to the cmdlet, get all
      // processes.
      if (this.processNames == null)
      {
        WriteObject(Process.GetProcesses(), true);
      }
      else
      {
        // If process names are passed to cmdlet, get and write
        // the associated processes.
        foreach (string name in this.processNames)
        {
          WriteObject(Process.GetProcessesByName(name), true);
        }
      } // End if (processNames...).
    } // End ProcessRecord.
    #endregion Cmdlet Overrides
  } // End GetProcCommand class.
  #endregion GetProcCommand
}

Vedere anche

Scrittura di un cmdlet di Windows PowerShell