美文网首页
利用powershell监控文件夹变动

利用powershell监控文件夹变动

作者: 种子说 | 来源:发表于2020-12-23 17:23 被阅读0次

    利用powershell监控某个文件夹下面的文件变化

    ### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
    $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.Path = "D:\" #监控路径
    $watcher.Filter = "*.*" #监控文件类型
    $watcher.IncludeSubdirectories = $true
    $watcher.EnableRaisingEvents = $true  
    
    ### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
    $action = { $path = $Event.SourceEventArgs.FullPath
        $changeType = $Event.SourceEventArgs.ChangeType
        $logline = "$(Get-Date), $changeType, $path"
        Add-content "E:\文件监测\log.txt" -value $logline #日志文件
    }    
    ### DECIDE WHICH EVENTS SHOULD BE WATCHED 
    Register-ObjectEvent $watcher "Created" -Action $action
    Register-ObjectEvent $watcher "Changed" -Action $action
    Register-ObjectEvent $watcher "Deleted" -Action $action
    Register-ObjectEvent $watcher "Renamed" -Action $action
    while ($true) { sleep 5 }
    

    使用powershell运行后如图:


    ps文件监控运行界面

    相关文章

      网友评论

          本文标题:利用powershell监控文件夹变动

          本文链接:https://www.haomeiwen.com/subject/mzlunktx.html