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.
Salah satu sumber input untuk cmdlet adalah objek pada alur yang berasal dari cmdlet hulu. Bagian ini menjelaskan cara menambahkan parameter ke cmdlet Get-Proc (dijelaskan dalam MembuatCmdlet Pertama Anda ) sehingga cmdlet dapat memproses objek alur.
Cmdlet Get-Proc ini menggunakan parameter Name yang menerima input dari objek alur, mengambil informasi proses dari komputer lokal berdasarkan nama yang disediakan, lalu menampilkan informasi tentang proses di baris perintah.
Menentukan Kelas Cmdlet
Langkah pertama dalam pembuatan cmdlet adalah selalu menamai cmdlet dan mendeklarasikan kelas .NET yang mengimplementasikan cmdlet. Cmdlet ini mengambil informasi proses, sehingga nama kata kerja yang dipilih di sini adalah "Dapatkan". (Hampir semua jenis cmdlet yang mampu mengambil informasi dapat memproses input baris perintah.) Untuk informasi selengkapnya tentang kata kerja cmdlet yang disetujui, lihat Nama Kata Kerja Cmdlet.
Berikut ini adalah definisi untuk cmdlet Get-Proc ini. Detail definisi ini diberikan dalam Membuat Cmdlet Pertama Anda.
[Cmdlet(VerbsCommon.Get, "proc")]
public class GetProcCommand : Cmdlet
<Cmdlet(VerbsCommon.Get, "Proc")> _
Public Class GetProcCommand
Inherits Cmdlet
Menentukan Input dari Alur
Bagian ini menjelaskan cara menentukan input dari alur untuk cmdlet. Cmdlet Get-Proc ini mendefinisikan properti yang mewakili parameter Name seperti yang dijelaskan dalam Menambahkan Parameter yangInput Baris Perintah Proses.
(Lihat topik tersebut untuk informasi umum tentang mendeklarasikan parameter.)
Namun, ketika cmdlet perlu memproses input alur, cmdlet harus memiliki parameter yang terikat ke nilai input oleh runtime Windows PowerShell. Untuk melakukan ini, Anda harus menambahkan kata kunci ValueFromPipeline atau menambahkan kata kunci ValueFromPipelineByProperty ke System.Management.Automation.ParameterAttribute deklarasi atribut. Tentukan kata kunci ValueFromPipeline jika cmdlet mengakses objek input lengkap. Tentukan ValueFromPipelineByProperty jika cmdlet hanya mengakses properti objek.
Berikut adalah deklarasi parameter untuk parameter Name cmdlet Get-Proc ini yang menerima input alur.
[Parameter(
Position = 0,
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true)]
[ValidateNotNullOrEmpty]
public string[] Name
{
get { return this.processNames; }
set { this.processNames = value; }
}
<Parameter(Position:=0, ValueFromPipeline:=True, _
ValueFromPipelineByPropertyName:=True), ValidateNotNullOrEmpty()> _
Public Property Name() As String()
Get
Return processNames
End Get
Set(ByVal value As String())
processNames = value
End Set
End Property
Deklarasi sebelumnya mengatur kata kunci ValueFromPipeline ke true sehingga runtime Windows PowerShell akan mengikat parameter ke objek masuk jika objek adalah jenis yang sama dengan parameter, atau jika dapat dipaksa ke jenis yang sama. Kata kunci ValueFromPipelineByPropertyName juga diatur ke true sehingga runtime Windows PowerShell akan memeriksa objek masuk untuk properti Name. Jika objek masuk memiliki properti seperti itu, runtime akan mengikat parameter Name ke properti Name objek masuk.
Nota
Pengaturan kata kunci atribut ValueFromPipeline untuk parameter lebih diutamakan daripada pengaturan untuk kata kunci ValueFromPipelineByPropertyName.
Mengesampingkan Metode Pemrosesan Input
Jika cmdlet Anda adalah menangani input alur, cmdlet perlu mengambil alih metode pemrosesan input yang sesuai. Metode pemrosesan input dasar diperkenalkan dalam Membuat Cmdlet Pertama Anda.
Cmdlet Get-Proc ini mengambil alih metode System.Management.Automation.Cmdlet.ProcessRecord untuk menangani input parameter Name yang disediakan oleh pengguna atau skrip. Metode ini akan mendapatkan proses untuk setiap nama proses yang diminta atau semua proses jika tidak ada nama yang disediakan. Perhatikan bahwa dalam System.Management.Automation.Cmdlet.ProcessRecord, panggilan ke WriteObject(System.Object,System.Boolean) adalah mekanisme output untuk mengirim objek output ke alur. Parameter kedua dari panggilan ini, enumerateCollection, diatur ke true untuk memberi tahu runtime Windows PowerShell untuk menghitung array objek proses, dan menulis satu proses sekaligus ke baris perintah.
protected override void ProcessRecord()
{
// If no process names are passed to the cmdlet, get all processes.
if (processNames == null)
{
// Write the processes to the pipeline making them available
// to the next cmdlet. The second argument of this call tells
// PowerShell to enumerate the array, and send one process at a
// time to the pipeline.
WriteObject(Process.GetProcesses(), true);
}
else
{
// If process names are passed to the cmdlet, get and write
// the associated processes.
foreach (string name in processNames)
{
WriteObject(Process.GetProcessesByName(name), true);
} // End foreach (string name...).
}
}
Protected Overrides Sub ProcessRecord()
Dim processes As Process()
'/ If no process names are passed to the cmdlet, get all processes.
If processNames Is Nothing Then
processes = Process.GetProcesses()
Else
'/ If process names are specified, write the processes to the
'/ pipeline to display them or make them available to the next cmdlet.
For Each name As String In processNames
'/ The second parameter of this call tells PowerShell to enumerate the
'/ array, and send one process at a time to the pipeline.
WriteObject(Process.GetProcessesByName(name), True)
Next
End If
End Sub 'ProcessRecord
Sampel Kode
Untuk kode sampel C# lengkap, lihat Sampel GetProcessSample03.
Menentukan Tipe objek dan Pemformatan
Windows PowerShell meneruskan informasi antar cmdlet menggunakan objek .NET. Akibatnya, cmdlet mungkin perlu menentukan jenisnya sendiri, atau cmdlet mungkin perlu memperluas jenis yang ada yang disediakan oleh cmdlet lain. Untuk informasi selengkapnya tentang menentukan jenis baru atau memperluas jenis yang sudah ada, lihat Memperluas Tipe Objek dan Pemformatan.
Membangun Cmdlet
Setelah menerapkan cmdlet, cmdlet harus didaftarkan ke Windows PowerShell melalui snap-in Windows PowerShell. Untuk informasi selengkapnya tentang mendaftarkan cmdlet, lihat Cara Mendaftarkan Cmdlet, Penyedia, dan Aplikasi Host.
Menguji Cmdlet
Ketika cmdlet Anda telah terdaftar di Windows PowerShell, uji dengan menjalankannya di baris perintah. Misalnya, uji kode untuk cmdlet sampel. Untuk informasi selengkapnya tentang menggunakan cmdlet dari baris perintah, lihat Memulai Windows PowerShell.
Pada prompt Windows PowerShell, masukkan perintah berikut untuk mengambil nama proses melalui alur.
PS> type ProcessNames | Get-ProcOutput berikut muncul.
Handles NPM(K) PM(K) WS(K) VS(M) CPU(s) Id ProcessName ------- ------ ----- ----- ----- ------ -- ----------- 809 21 40856 4448 147 9.50 2288 iexplore 737 21 26036 16348 144 22.03 3860 iexplore 39 2 1024 388 30 0.08 3396 notepad 3927 62 71836 26984 467 195.19 1848 OUTLOOKMasukkan baris berikut untuk mendapatkan objek proses yang memiliki properti
Namedari proses yang disebut "IEXPLORE". Contoh ini menggunakan cmdletGet-Process(disediakan oleh Windows PowerShell) sebagai perintah upstream untuk mengambil proses "IEXPLORE".PS> Get-Process iexplore | Get-ProcOutput berikut muncul.
Handles NPM(K) PM(K) WS(K) VS(M) CPU(s) Id ProcessName ------- ------ ----- ----- ----- ------ -- ----------- 801 21 40720 6544 142 9.52 2288 iexplore 726 21 25872 16652 138 22.09 3860 iexplore 801 21 40720 6544 142 9.52 2288 iexplore 726 21 25872 16652 138 22.09 3860 iexplore
Lihat Juga
Menambahkan Parameter yang memproses Input Baris Perintah
Membuat Cmdlet Pertama Anda
Memperluas Tipe Objek dan Pemformatan