2.8 KiB
Windows PowerShell Event Registration Sample
This sample shows how to create a cmdlet for event registration by deriving from ObjectEventRegistrationBase.
The sample creates the Register-FileSystemEvent cmdlet which subscribes to events raised by System.IO.FileSystemWatcher. With this cmdlet, users can register an action to execute when a specific event is raised, such as when a file is created under an specific directory.
Sample Objectives
This sample demonstrates the following:
- How to how to derive from the ObjectEventRegistrationBase class to create a cmdlet for event registration.
Related topics
Operating system requirements
Client
Windows 8.1
Server
Windows Server 2012 R2
Build the sample
-
Start Microsoft Visual Studio and select File > Open > Project/Solution.
-
Go to the directory named for the sample, and double-click the Visual Studio Solution (.sln) file.
-
Press F7 or use Build > Build Solution to build the sample.
The library will be built in the default \bin or \bin\Debug directory.
Run the sample
- Start PowerShell and import the library file in order to make the
Register-FileSystemEventcmdlet available in PowerShell. - Use the
Register-FileSystemEventcmdlet to register an action that will write a message when a file is created under the TEMP directory. - Create a file under the TEMP directory and note that the action is executed (i.e. the message is displayed).
This is the sample output from executing these 3 steps:
| cmd |
|---|
|