Exemple GetProcessSample01
Cet exemple montre comment implémenter une applet de commande qui récupère les processus sur l’ordinateur local. Cette applet de commande est une version simplifiée de l’applet de commande Get-Process
fournie par Windows PowerShell 2.0.
Comment générer l’exemple à l’aide de Visual Studio
Une fois le Kit de développement logiciel (SDK) Windows PowerShell 2.0 installé, accédez au dossier GetProcessSample01. L’emplacement par défaut est
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0\Samples\sysmgmt\WindowsPowerShell\csharp\GetProcessSample01
.Double-cliquez sur l’icône du fichier de solution (.sln). L’exemple de projet s’ouvre dans Microsoft Visual Studio.
Dans le menu Générer, sélectionnez Générer la solution pour générer la bibliothèque pour l’exemple dans les dossiers
\bin
ou\bin\debug
par défaut.
Comment exécuter l’exemple
Ouvrez une fenêtre d’invite de commandes.
Accédez au répertoire contenant l’exemple de fichier .dll.
Exécutez
installutil "GetProcessSample01.dll"
.Démarrez Windows PowerShell.
Exécutez la commande suivante pour ajouter le composant logiciel enfichable à l’interpréteur de commandes.
Add-PSSnapin GetProcPSSnapIn01
Entrez la commande suivante pour exécuter l’applet de commande.
Get-Proc
Get-Proc
Il s’agit d’un exemple de sortie qui résulte de ces étapes.
Id Name State HasMoreData Location Command -- ---- ----- ----------- -------- ------- 1 26932870-d3b... NotStarted False Write-Host "A f...
Set-Content $Env:TEMP\test.txt "This is a test file"
A file was created in the TEMP directory
Spécifications
Cet exemple nécessite Windows PowerShell 1.0 ou version ultérieure.
Montre ce qui suit
Cet exemple illustre ce qui suit.
Création d’un exemple d’applet de commande de base.
Définition d’une classe d’applet de commande à l’aide de l’attribut Cmdlet.
Création d’un composant logiciel enfichable qui fonctionne avec Windows PowerShell 1.0 et Windows PowerShell 2.0. Les exemples suivants utilisent des modules plutôt que des composants logiciels enfichables afin qu’ils nécessitent Windows PowerShell 2.0.
Exemple :
Cet exemple montre comment créer une applet de commande simple et son composant logiciel enfichable.
using System;
using System.Diagnostics;
using System.Management.Automation; //Windows PowerShell namespace
using System.ComponentModel;
namespace Microsoft.Samples.PowerShell.Commands
{
#region GetProcCommand
/// <summary>
/// This class implements the Get-Proc cmdlet.
/// </summary>
[Cmdlet(VerbsCommon.Get, "Proc")]
public class GetProcCommand : Cmdlet
{
#region Cmdlet Overrides
/// <summary>
/// The ProcessRecord method calls the Process.GetProcesses
/// method to retrieve the processes of the local computer.
/// Then, the WriteObject method writes the associated processes
/// to the pipeline.
/// </summary>
protected override void ProcessRecord()
{
// Retrieve the current processes.
Process[] processes = Process.GetProcesses();
// Write the processes to the pipeline to make them available
// to the next cmdlet. The second argument (true) tells Windows
// PowerShell to enumerate the array and to send one process
// object at a time to the pipeline.
WriteObject(processes, true);
}
#endregion Overrides
} //GetProcCommand
#endregion GetProcCommand
#region PowerShell snap-in
/// <summary>
/// Create this sample as a PowerShell snap-in
/// </summary>
[RunInstaller(true)]
public class GetProcPSSnapIn01 : PSSnapIn
{
/// <summary>
/// Create an instance of the GetProcPSSnapIn01
/// </summary>
public GetProcPSSnapIn01()
: base()
{
}
/// <summary>
/// Get a name for this PowerShell snap-in. This name will be used in registering
/// this PowerShell snap-in.
/// </summary>
public override string Name
{
get
{
return "GetProcPSSnapIn01";
}
}
/// <summary>
/// Vendor information for this PowerShell snap-in.
/// </summary>
public override string Vendor
{
get
{
return "Microsoft";
}
}
/// <summary>
/// Gets resource information for vendor. This is a string of format:
/// resourceBaseName,resourceName.
/// </summary>
public override string VendorResource
{
get
{
return "GetProcPSSnapIn01,Microsoft";
}
}
/// <summary>
/// Description of this PowerShell snap-in.
/// </summary>
public override string Description
{
get
{
return "This is a PowerShell snap-in that includes the Get-Proc cmdlet.";
}
}
}
#endregion PowerShell snap-in
}