Megjegyzés
Az oldalhoz való hozzáféréshez engedély szükséges. Megpróbálhat bejelentkezni vagy módosítani a címtárat.
Az oldalhoz való hozzáféréshez engedély szükséges. Megpróbálhatja módosítani a címtárat.
Ez a minta bemutatja, hogyan implementálhat egy parancsmagot, amely lekéri a folyamatokat a helyi számítógépen. Ez a parancsmag a Windows PowerShell 2.0 által biztosított Get-Process parancsmag egyszerűsített verziója.
A minta létrehozása a Visual Studióval
Ha telepítve van a Windows PowerShell 2.0 SDK, lépjen a GetProcessSample01 mappába. Az alapértelmezett hely
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0\Samples\sysmgmt\WindowsPowerShell\csharp\GetProcessSample01.Kattintson duplán a megoldásfájl (.sln) ikonjára. Ez megnyitja a mintaprojektet a Microsoft Visual Studióban.
A Build menüben válassza a Megoldás összeállítása lehetőséget a minta kódtárának az alapértelmezett
\binvagy\bin\debugmappákban való létrehozásához.
A minta futtatása
Nyisson meg egy parancssori ablakot.
Lépjen a mintafájlt tartalmazó könyvtárra .dll.
Futtassa az
installutil "GetProcessSample01.dll"parancsot.Indítsa el a Windows PowerShellt.
Futtassa a következő parancsot a beépülő modul a rendszerhéjhoz való hozzáadásához.
Add-PSSnapin GetProcPSSnapIn01A parancsmag futtatásához írja be a következő parancsot.
Get-ProcGet-ProcEz egy mintakimenet, amely az alábbi lépések követéséből ered.
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
Követelmények
Ehhez a mintához a Windows PowerShell 1.0-s vagy újabb verziója szükséges.
Megmutatja
Ez a minta a következőket mutatja be.
Egyszerű mintaparancsmag létrehozása.
Parancsmagosztály definiálása a Parancsmag attribútum használatával.
Olyan beépülő modul létrehozása, amely a Windows PowerShell 1.0-val és a Windows PowerShell 2.0-val is működik. A későbbi minták beépülő modulok helyett modulokat használnak, így a Windows PowerShell 2.0-ra van szükségük.
Példa
Ez a minta bemutatja, hogyan hozhat létre egyszerű parancsmagot és annak beépülő modulját.
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
}