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 membuat cmdlet yang memungkinkan pengguna mendaftar untuk peristiwa yang dinaikkan oleh System.IO.FileSystemWatcher. Dengan cmdlet ini, pengguna dapat mendaftarkan tindakan untuk dijalankan saat file dibuat di bawah direktori tertentu. Sampel ini berasal dari kelas dasar Microsoft.PowerShell.Commands.ObjectEventRegistrationBase.
Cara membuat sampel dengan menggunakan Visual Studio
Dengan Windows PowerShell 2.0 SDK terinstal, navigasikan ke folder Events01. Lokasi default adalah
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0\Samples\sysmgmt\WindowsPowerShell\csharp\Events01.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
Buat folder modul berikut:
[user]\Documents\WindowsPowerShell\Modules\events01Salin file pustaka untuk sampel ke folder modul.
Mulai Windows PowerShell.
Jalankan perintah berikut untuk memuat cmdlet ke Windows PowerShell:
Import-Module events01Gunakan cmdlet Register-FileSystemEvent untuk mendaftarkan tindakan yang akan menulis pesan saat file dibuat di bawah direktori TEMP.
Register-FileSystemEvent $Env:TEMP Created -Filter "*.txt" -Action { Write-Host "A file was created in the TEMP directory" }Buat file di bawah direktori TEMP dan perhatikan bahwa tindakan dijalankan (pesan ditampilkan).
Ini adalah contoh output yang dihasilkan dengan 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 2.0.
Menunjukkan
Sampel ini menunjukkan hal berikut.
Cara menulis cmdlet untuk pendaftaran peristiwa
Cmdlet berasal dari kelas Microsoft.PowerShell.Commands.ObjectEventRegistrationBase, yang menyediakan dukungan untuk parameter yang umum untuk cmdlet Register-*Event. Cmdlet yang berasal dari Microsoft.PowerShell.Commands.ObjectEventRegistrationBase hanya perlu menentukan parameter khusus mereka dan mengambil alih metode abstrak GetSourceObject dan GetSourceObjectEventName.
Contoh
Sampel ini menunjukkan cara mendaftar untuk peristiwa yang dibesarkan oleh System.IO.FileSystemWatcher.
namespace Sample
{
using System;
using System.IO;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using Microsoft.PowerShell.Commands;
[Cmdlet(VerbsLifecycle.Register, "FileSystemEvent")]
public class RegisterObjectEventCommand : ObjectEventRegistrationBase
{
/// <summary>The FileSystemWatcher that exposes the events.</summary>
private FileSystemWatcher fileSystemWatcher = new FileSystemWatcher();
/// <summary>Name of the event to which the cmdlet registers.</summary>
private string eventName = null;
/// <summary>
/// Gets or sets the path that will be monitored by the FileSystemWatcher.
/// </summary>
[Parameter(Mandatory = true, Position = 0)]
public string Path
{
get
{
return this.fileSystemWatcher.Path;
}
set
{
this.fileSystemWatcher.Path = value;
}
}
/// <summary>
/// Gets or sets the name of the event to which the cmdlet registers.
/// <para>
/// Currently System.IO.FileSystemWatcher exposes 6 events: Changed, Created,
/// Deleted, Disposed, Error, and Renamed. Check the documentation of
/// FileSystemWatcher for details on each event.
/// </para>
/// </summary>
[Parameter(Mandatory = true, Position = 1)]
public string EventName
{
get
{
return this.eventName;
}
set
{
this.eventName = value;
}
}
/// <summary>
/// Gets or sets the filter that will be user by the FileSystemWatcher.
/// </summary>
[Parameter(Mandatory = false)]
public string Filter
{
get
{
return this.fileSystemWatcher.Filter;
}
set
{
this.fileSystemWatcher.Filter = value;
}
}
/// <summary>
/// Derived classes must implement this method to return the object that generates
/// the events to be monitored.
/// </summary>
/// <returns> This sample returns an instance of System.IO.FileSystemWatcher</returns>
protected override object GetSourceObject()
{
return this.fileSystemWatcher;
}
/// <summary>
/// Derived classes must implement this method to return the name of the event to
/// be monitored. This event must be exposed by the input object.
/// </summary>
/// <returns> This sample returns the event specified by the user with the -EventName parameter.</returns>
protected override string GetSourceObjectEventName()
{
return this.eventName;
}
}
}