Aracılığıyla paylaş


Events01 Örneği

Bu örnek, kullanıcının System.IO.FileSystemWatchertarafından tetiklenen olaylara kaydolmasını sağlayan bir cmdlet'in nasıl oluşturulacağını gösterir. Bu cmdlet ile, kullanıcılar belirli bir dizin altında bir dosya oluşturulduğunda yürütülecek eylemi kaydedebilir. Bu örnek, Microsoft.PowerShell.Commands.ObjectEventRegistrationBase temel sınıfından türetilir.

Visual Studio kullanarak örnek oluşturma

  1. Windows PowerShell 2.0 SDK'sı yüklü olarak Events01 klasörüne gidin. Varsayılan konum C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0\Samples\sysmgmt\WindowsPowerShell\csharp\Events01.

  2. Çözüm (.sln) dosyasının simgesine çift tıklayın. Bu işlem örnek projeyi Microsoft Visual Studio'da açar.

  3. Derleme menüsünde Çözümü Derle 'ı seçerek varsayılan \bin veya \bin\debug klasörlerinde örneğin kitaplığını oluşturun.

Örneği çalıştırma

  1. Aşağıdaki modül klasörünü oluşturun:

    [user]\Documents\WindowsPowerShell\Modules\events01

  2. Örneğin kitaplık dosyasını modül klasörüne kopyalayın.

  3. Windows PowerShell’i başlatın.

  4. Cmdlet'i Windows PowerShell'e yüklemek için aşağıdaki komutu çalıştırın:

    Import-Module events01
    
  5. TEMP dizini altında bir dosya oluşturulduğunda ileti yazacak eylemi kaydetmek için Register-FileSystemEvent cmdlet'ini kullanın.

    Register-FileSystemEvent $Env:TEMP Created -Filter "*.txt" -Action { Write-Host "A file was created in the TEMP directory" }
    
  6. TEMP dizini altında bir dosya oluşturun ve eylemin yürütüldüğünü (ileti görüntülenir) unutmayın.

Bu, aşağıdaki adımları izleyerek sonuç veren örnek bir çıktıdır.

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

Gereksinimler

Bu örnek Için Windows PowerShell 2.0 gerekir.

Gösterir

Bu örnekte aşağıdakiler gösterilmektedir.

Olay kaydı için cmdlet yazma

Cmdlet, Register-*Event cmdlet'leri için ortak parametreler için destek sağlayan Microsoft.PowerShell.Commands.ObjectEventRegistrationBase sınıfından türetilir. Microsoft.PowerShell.Commands.ObjectEventRegistrationBase türetilen cmdlet'lerin yalnızca belirli parametrelerini tanımlaması ve GetSourceObject ve GetSourceObjectEventName soyut yöntemleri geçersiz kılması gerekir.

Örnek

Bu örnek, System.IO.FileSystemWatchertarafından tetiklenen olaylar için nasıl kayıt yapılacağını gösterir.

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

Ayrıca Bkz.