Megosztás a következőn keresztül:


Events01 – minta

Ez a minta bemutatja, hogyan hozhat létre olyan parancsmagot, amely lehetővé teszi, hogy a felhasználó regisztráljon a System.IO.FileSystemWatcher által létrehozott eseményekre. Ezzel a parancsmaggal a felhasználók regisztrálhatnak egy műveletet, amely akkor hajtható végre, ha egy fájl egy adott könyvtárban jön létre. Ez a minta a Microsoft.PowerShell.Commands.ObjectEventRegistrationBase alaposztályból származik.

A minta létrehozása a Visual Studióval

  1. Ha telepítve van a Windows PowerShell 2.0 SDK, lépjen az Events01 mappába. Az alapértelmezett hely C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0\Samples\sysmgmt\WindowsPowerShell\csharp\Events01.

  2. Kattintson duplán a megoldásfájl (.sln) ikonjára. Ez megnyitja a mintaprojektet a Microsoft Visual Studióban.

  3. A Build menüben válassza a Megoldás összeállítása lehetőséget a minta kódtárának az alapértelmezett \bin vagy \bin\debug mappákban való létrehozásához.

A minta futtatása

  1. Hozza létre a következő modulmappát:

    [user]\Documents\WindowsPowerShell\Modules\events01

  2. Másolja a minta kódtárfájlja a modulmappába.

  3. Indítsa el a Windows PowerShellt.

  4. Futtassa a következő parancsot a parancsmag Windows PowerShellbe való betöltéséhez:

    Import-Module events01
    
  5. A Register-FileSystemEvent parancsmaggal regisztrálhat egy olyan műveletet, amely üzenetet ír, amikor egy fájl jön létre a TEMP könyvtárban.

    Register-FileSystemEvent $Env:TEMP Created -Filter "*.txt" -Action { Write-Host "A file was created in the TEMP directory" }
    
  6. Hozzon létre egy fájlt a TEMP könyvtár alatt, és figyelje meg, hogy a művelet végrehajtása (az üzenet megjelenik).

Ez egy mintakimenet, amely az alábbi lépések végrehajtásával jön létre.

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

Követelmények

Ehhez a mintához Windows PowerShell 2.0 szükséges.

Megmutatja

Ez a minta a következőket mutatja be.

Parancsmag írása eseményregisztrációhoz

A parancsmag a Microsoft.PowerShell.Commands.ObjectEventRegistrationBase osztály származik, amely támogatja a Register-*Event parancsmagok által gyakran használt paramétereket. A Microsoft.PowerShell.Commands.ObjectEventRegistrationBase származó parancsmagoknak csak az adott paramétereket kell meghatározniuk, és felül kell bírálniuk a GetSourceObject és GetSourceObjectEventName absztrakt metódusokat.

Példa

Ez a minta bemutatja, hogyan regisztrálhat System.IO.FileSystemWatcheráltal létrehozott eseményekre.

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;
        }
    }
}

Lásd még: