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) 图标。 这会在 Microsoft Visual Studio 中打开示例项目。

  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 只需定义其特定参数并重写 和 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;
        }
    }
}

另请参阅

编写 Windows PowerShell Cmdlet