Events01 示例

此示例演示如何创建一个 cmdlet,该 cmdlet 允许用户注册 System.IO.FileSystemWatcher引发的事件。 使用此 cmdlet,用户可以注册在特定目录下创建文件时要执行的作。 此示例派生自 Microsoft.PowerShell.Commands.ObjectEventRegistrationBase 基类。

如何使用 Visual Studio 生成示例

  1. 安装 Windows PowerShell 2.0 SDK 后,导航到 Events01 文件夹。 默认位置为 C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0\Samples\sysmgmt\WindowsPowerShell\csharp\Events01

  2. 双击解决方案(.sln)文件的图标。 这将在 Visual Studio 中打开示例项目Microsoft。

  3. 生成 菜单中,选择 “生成解决方案”,以在默认 \bin\bin\debug 文件夹中为示例生成库。

如何运行示例

  1. 创建以下模块文件夹:

    [user]\Documents\WindowsPowerShell\Modules\events01

  2. 将示例的库文件复制到模块文件夹。

  3. 启动 Windows PowerShell。

  4. 运行以下命令,将 cmdlet 加载到 Windows PowerShell 中:

    Import-Module events01
    
  5. 使用 Register-FileSystemEvent cmdlet 注册在 TEMP 目录下创建文件时将写入消息的作。

    Register-FileSystemEvent $Env:TEMP Created -Filter "*.txt" -Action { Write-Host "A file was created in the TEMP directory" }
    
  6. 在 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 只需定义其特定参数并重写 GetSourceObjectGetSourceObjectEventName 抽象方法。

示例

此示例演示如何注册 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;
        }
    }
}

另请参阅