• gallary


    https://gallery.technet.microsoft.com/scriptcenter

    #By BigTeddy 05 September 2011
    
    #This script uses the .NET FileSystemWatcher class to monitor file events in folder(s).
    #The advantage of this method over using WMI eventing is that this can monitor sub-folders.
    #The -Action parameter can contain any valid Powershell commands.  I have just included two for example.
    #The script can be set to a wildcard filter, and IncludeSubdirectories can be changed to $true.
    #You need not subscribe to all three types of event.  All three are shown for example.
    # Version 1.1
    
    $folder = 'c:scripts	est' # Enter the root path you want to monitor.
    $filter = '*.*'  # You can enter a wildcard filter here.
    
    # In the following line, you can change 'IncludeSubdirectories to $true if required.
    $fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}
    
    # Here, all three events are registerd.  You need only subscribe to events that you need:
    
    Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
    $name = $Event.SourceEventArgs.Name
    $changeType = $Event.SourceEventArgs.ChangeType
    $timeStamp = $Event.TimeGenerated
    Write-Host "The file '$name' was $changeType at $timeStamp" -fore green
    Out-File -FilePath c:scriptsfilechangeoutlog.txt -Append -InputObject "The file '$name' was $changeType at $timeStamp"}
    
    Register-ObjectEvent $fsw Deleted -SourceIdentifier FileDeleted -Action {
    $name = $Event.SourceEventArgs.Name
    $changeType = $Event.SourceEventArgs.ChangeType
    $timeStamp = $Event.TimeGenerated
    Write-Host "The file '$name' was $changeType at $timeStamp" -fore red
    Out-File -FilePath c:scriptsfilechangeoutlog.txt -Append -InputObject "The file '$name' was $changeType at $timeStamp"}
    
    Register-ObjectEvent $fsw Changed -SourceIdentifier FileChanged -Action {
    $name = $Event.SourceEventArgs.Name
    $changeType = $Event.SourceEventArgs.ChangeType
    $timeStamp = $Event.TimeGenerated
    Write-Host "The file '$name' was $changeType at $timeStamp" -fore white
    Out-File -FilePath c:scriptsfilechangeoutlog.txt -Append -InputObject "The file '$name' was $changeType at $timeStamp"}
    
    # To stop the monitoring, run the following commands:
    # Unregister-Event FileDeleted
    # Unregister-Event FileCreated
    # Unregister-Event FileChanged
  • 相关阅读:
    Day 15 模块
    Day 14 三元运算符,列表推导式,内置函数
    Day 13 可迭代对象,迭代器对象,for循环迭代,生成器对象,枚举对象
    Day 12 开放封闭原则,装饰器初识
    Day 11 函数对象,函数嵌套,作用域,闭包
    Day 10 函数的形参,实参
    Day 09 函数基础
    Day 08 文件操作模式,文件复制,游标
    HTTP协议
    11,.JS-DOM价绍
  • 原文地址:https://www.cnblogs.com/Searchor/p/13439406.html
Copyright © 2020-2023  润新知