Catatan
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba masuk atau mengubah direktori.
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba mengubah direktori.
Sampel ini menunjukkan cara mengimplementasikan cmdlet yang mengambil proses di komputer lokal. Cmdlet ini adalah versi yang disederhanakan dari cmdlet Get-Process yang disediakan oleh Windows PowerShell 2.0.
Cara membuat sampel dengan menggunakan Visual Studio
Dengan Windows PowerShell 2.0 SDK terinstal, navigasikan ke folder GetProcessSample01. Lokasi default adalah
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0\Samples\sysmgmt\WindowsPowerShell\csharp\GetProcessSample01.Klik dua kali ikon untuk file solusi (.sln). Ini membuka proyek sampel di Microsoft Visual Studio.
Di menu Build, pilih Build Solution untuk membangun pustaka sampel di folder
\binatau\bin\debugdefault.
Cara menjalankan sampel
Buka jendela Command Prompt.
Navigasikan ke direktori yang berisi sampel file .dll.
Jalankan
installutil "GetProcessSample01.dll".Mulai Windows PowerShell.
Jalankan perintah berikut untuk menambahkan snap-in ke shell.
Add-PSSnapin GetProcPSSnapIn01Masukkan perintah berikut untuk menjalankan cmdlet.
Get-ProcGet-ProcIni adalah contoh output yang dihasilkan dari mengikuti langkah-langkah ini.
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
Persyaratan
Sampel ini memerlukan Windows PowerShell 1.0 atau yang lebih baru.
Menunjukkan
Sampel ini menunjukkan hal berikut.
Membuat cmdlet sampel dasar.
Menentukan kelas cmdlet dengan menggunakan atribut Cmdlet.
Membuat snap-in yang berfungsi dengan Windows PowerShell 1.0 dan Windows PowerShell 2.0. Sampel berikutnya menggunakan modul alih-alih snap-in sehingga memerlukan Windows PowerShell 2.0.
Contoh
Sampel ini menunjukkan cara membuat cmdlet sederhana dan snap-in-nya.
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
}