此範例示範如何建立 Cmdlet,讓使用者註冊 System.IO.FileSystemWatcher 所引發的事件,。 使用此 Cmdlet 時,用戶可以註冊動作,以在特定目錄下建立檔案時執行。 此範例衍生自 Microsoft.PowerShell.Commands.ObjectEventRegistrationBase 基類。
如何使用 Visual Studio 建置範例
安裝 Windows PowerShell 2.0 SDK 後,流覽至 Events01 資料夾。 預設位置為
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0\Samples\sysmgmt\WindowsPowerShell\csharp\Events01。按兩下解決方案 (.sln) 檔案的圖示。 這會在 Visual Studio 中開啟範例專案Microsoft。
在 [建置] 功能表中,選取 [建置方案],以在預設
\bin或\bin\debug資料夾中建置範例的連結庫。
如何執行範例
建立下列模組資料夾:
[user]\Documents\WindowsPowerShell\Modules\events01將範例的連結庫檔案複製到模組資料夾。
啟動 Windows PowerShell。
執行下列命令,將 Cmdlet 載入 Windows PowerShell:
Import-Module events01使用 Register-FileSystemEvent Cmdlet 註冊動作,以在 TEMP 目錄下建立檔案時寫入訊息。
Register-FileSystemEvent $Env:TEMP Created -Filter "*.txt" -Action { Write-Host "A file was created in the TEMP directory" }在 TEMP 目錄下建立檔案,並注意動作已執行(顯示訊息)。
這是遵循下列步驟所產生的範例輸出。
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
需求
此範例需要 Windows PowerShell 2.0。
演示
此範例示範下列專案。
如何撰寫事件註冊的 Cmdlet
Cmdlet 衍生自 Microsoft.PowerShell.Commands.ObjectEventRegistrationBase 類別,其支援 Register-*Event Cmdlet 通用的參數。 衍生自 Microsoft.PowerShell.Commands.ObjectEventRegistrationBase 的 Cmdlet 只需要定義其特定參數,並覆寫 GetSourceObject 和 GetSourceObjectEventName 抽象方法。
範例
此範例示範如何註冊 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;
}
}
}