Condividi tramite


Esempio di GetProcessSample02

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

Come compilare l'esempio con Visual Studio

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

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

  3. Nel menu Compila selezionare Compila soluzione per compilare la libreria per l'esempio nelle cartelle predefinite \bin o \bin\debug.

Come 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 cmdlet tramite l'attributo Cmdlet.

  • Dichiarazione di un parametro di 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 parametro Name.

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