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. Ini menghasilkan kesalahan yang tidak mengakhiri jika terjadi kesalahan saat mengambil proses. Cmdlet ini adalah versi yang disederhanakan dari cmdlet Get-Process
yang disediakan oleh Windows PowerShell 2.0.
Cara membuat sampel menggunakan Visual Studio
Dengan Windows PowerShell 2.0 SDK terinstal, navigasikan ke folder GetProcessSample04. Lokasi default adalah
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0\Samples\sysmgmt\WindowsPowerShell\csharp\GetProcessSample04
.Klik dua kali ikon untuk file solusi (.sln). Ini membuka proyek sampel di Visual Studio.
Di menu Build, pilih Build Solution untuk membangun pustaka sampel di folder
\bin
atau\bin\debug
default.
Cara menjalankan sampel
Buat folder modul berikut:
[user]\Documents\WindowsPowerShell\Modules\GetProcessSample04
Salin rakitan sampel ke folder modul.
Mulai Windows PowerShell.
Jalankan perintah berikut untuk memuat rakitan ke Windows PowerShell:
Import-Module getprossessample04
Jalankan perintah berikut untuk menjalankan cmdlet:
Get-Proc
Persyaratan
Sampel ini memerlukan Windows PowerShell 2.0.
Menunjukkan
Sampel ini menunjukkan hal berikut.
Mendeklarasikan kelas cmdlet menggunakan atribut Cmdlet.
Mendeklarasikan parameter cmdlet menggunakan atribut Parameter.
Menentukan posisi parameter.
Menentukan bahwa parameter mengambil input dari alur. Input dapat diambil dari objek atau nilai dari properti objek yang nama propertinya sama dengan nama parameter.
Mendeklarasikan atribut validasi untuk input parameter.
Menjebak kesalahan yang tidak mengakhiri dan menulis pesan kesalahan ke aliran kesalahan.
Contoh
Sampel ini menunjukkan cara membuat cmdlet yang menangani kesalahan yang tidak mengakhiri dan menulis pesan kesalahan ke aliran kesalahan.
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 to act on.
/// </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,
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true)]
[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 processes to the pipeline.
/// </summary>
protected override void ProcessRecord()
{
// If no process names are passed to cmdlet, get all
// processes.
if (this.processNames == null)
{
WriteObject(Process.GetProcesses(), true);
}
else
{
// If process names are passed to the cmdlet, get and write
// the associated processes.
// If a non-terminating error occurs while retrieving processes,
// call the WriteError method to send an error record to the
// error stream.
foreach (string name in this.processNames)
{
Process[] processes;
try
{
processes = Process.GetProcessesByName(name);
}
catch (InvalidOperationException ex)
{
WriteError(new ErrorRecord(
ex,
"UnableToAccessProcessByName",
ErrorCategory.InvalidOperation,
name));
continue;
}
WriteObject(processes, true);
} // foreach (string name...
} // else
} // ProcessRecord
#endregion Overrides
} // End GetProcCommand class.
#endregion GetProcCommand
}