このサンプルでは、system.IO.FileSystemWatcher によって発生したイベントにユーザーが登録できるコマンドレット作成する方法を示します。 このコマンドレットを使用すると、ユーザーは特定のディレクトリにファイルが作成されたときに実行するアクションを登録できます。 このサンプルは、基本クラス 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) ファイルのアイコンをダブルクリックします。 これにより、Microsoft Visual Studio でサンプル プロジェクトが開きます。
[ビルド] メニューの [ソリューションのビルド] を選択して、既定の
\bin
フォルダーまたは\bin\debug
フォルダーにサンプルのライブラリをビルドします。
サンプルを実行する方法
次のモジュール フォルダーを作成します。
[user]\Documents\WindowsPowerShell\Modules\events01
サンプルのライブラリ ファイルをモジュール フォルダーにコピーします。
Windows PowerShell を起動します。
次のコマンドを実行して、コマンドレットを Windows PowerShell に読み込みます。
Import-Module events01
Register-FileSystemEvent コマンドレットを使用して、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 が必要です。
対象
このサンプルでは、次の例を示します。
イベント登録用のコマンドレットを記述する方法
このコマンドレットは、Microsoft.PowerShell.Commands.ObjectEventRegistrationBase クラスから派生し、Register-*Event
コマンドレットに共通するパラメーターをサポートします。 Microsoft.PowerShell.Commands.ObjectEventRegistrationBase から派生したコマンドレット は、特定のパラメーターを定義し、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;
}
}
}
こちらもご覧ください
PowerShell