Powershell 之监控文件系统变化

#region Import Assemblies
#----------------------------------------------
[void][Reflection.Assembly]::Load("System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
[void][Reflection.Assembly]::Load("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
[void][Reflection.Assembly]::Load("System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
[void][Reflection.Assembly]::Load("mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
[void][Reflection.Assembly]::Load("System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
[void][Reflection.Assembly]::Load("System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
[void][Reflection.Assembly]::Load("System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
#endregion Import Assemblies

#设置监控路径
$script:folder="C:\"

$form=New-Object System.Windows.Forms.Form
$form.FormBorderStyle="Fixed3D"
$form.StartPosition = ‘CenterScreen‘
$form.ClientSize="200,80"
$form.Text="Monitor"

$buttonStart=New-Object System.Windows.Forms.Button
$buttonStart.Text="Start Watch"
$buttonStart.Location="45,10"
$buttonStart.add_click({start-watch})
$buttonStart.Size="100,20"

$buttonStop=New-Object System.Windows.Forms.Button
$buttonStop.Text="Stop Watch"
$buttonStop.Location="45,40"
$buttonStop.add_click({stop-watch})
$buttonStop.Size="100,20"

$form.Controls.AddRange(@($buttonStart,$buttonStop))

$script:watcher = New-Object System.IO.FileSystemWatcher $folder

#开始执行监控
function start-watch{
    $NotifyFilters=New-Object System.IO.NotifyFilters
    #$watcher.NotifyFilter="Size,LastWrite,LastAccess,CreationTime,Security"
    $watcher.Filter = "*.*"
    $watcher.InternalBufferSize = 65536
    #是否包含子目录
    $watcher.IncludeSubDirectories = $True
    #是否触发事件,必须开启
    $watcher.EnableRaisingEvents = $True
    $watcher.SynchronizingObject = $form
    $form.Text="Monitoring"
    $buttonStart.Enabled=$false

    #创建时触发
    $watcher.add_Created({created})
    $watcher.add_Changed({changed})
    $watcher.add_Deleted({deleted})
    $watcher.add_Renamed({renamed})

}

function msg($message){
    [Windows.Forms.MessageBox]::Show($message)
}

# $_.changetype 获取操作类型
# $_.fullpath   获取文件绝对路径

function created(){
    #创建文件
    #自己编写处理逻辑
    msg($_.fullpath)
}

function changed(){
    #文件信息变更
    #自己编写处理逻辑
    msg($_.fullpath)
}

function deleted(){
    #删除文件
    #自己编写处理逻辑
    msg($_.fullpath)
}

function renamed(){
    #名称变更
    #自己编写处理逻辑
    msg($_.fullpath)
}

#停止监控
function stop-watch{
    $watcher.EnableRaisingEvents = $false
    $form.Text="Monitor"
    $buttonStart.Enabled=$true
}

$form.ShowDialog()
时间: 2024-08-06 16:01:09

Powershell 之监控文件系统变化的相关文章

UITextField监控文字变化方法

项目里需要在不隐藏键盘的情况下,来更改button 的状态. 这里就是要对uitextfield监控,uitextfield继承于UIControl,所以可以 使用addTarget方法,具体如下. 1,在textFieldDidBeginEditing增加 -(void)textFieldDidBeginEditing:(UITextField *)textField { [m_phoneNum addTarget:self action:@selector(textLengthChange:

PowerShell 学习笔记——文件系统

一个操作系统必须具备的元素就是:文件系统.在 Windows 中,使用资源管理器和 PowerShell 都可以管理文件系统,但它们是有区别的. 一.文件系统的结构 Windows中,文件系统由磁盘驱动器.文件夹和文件三种对象组成,磁盘是最上层对象,文件是最小对象,文件夹是容器. 对于 PowerShell 而言,文件系统也是一种提供程序. 这里的"Drives"是指 PowerShell 通过某个提供程序创建出来的PSDrive,用以连接到某些存储数据的介质.比如上图中"P

php 监控文件变化 并上传到服务器

最近有个思路.想要监控本地文件变化 并上传到指定服务器. 刚开始有这个想法的时候找了很多资料.关于 windows 和linux 上怎么监控文件变化的 最后选择了 比较MD5值的方法. 我也写了一段 但是也大同小异. 于是 其中借用了别人的代码 他的github 是 https://github.com/tansuo1989/mydemo/blob/master/php-demo/file_watch.php 话不多说 上代码 <?php /** * Created by PhpStorm. *

inotify机制监控文件系统事件原理及使用

1.基本描述 inotify提供了一种监控文件系统事件的机制,可以用来监控单个的文件以及目录.当一个目录被监控,inotify会返回该目录以及该目录下面文件的事件. 2.原理以及使用 2.1内核原理 inotify机制借用了内核里面的notify通知链技术,针对文件系统里面的使用主要是在inode结构体里面加入了相关的字段(内核版本3.10.0-327): struct inode {       ...#ifdef CONFIG_FSNOTIFY      __u32 i_fsnotify_m

delphi 监控文件系统

elphi 监控文件系统 你是否想为你的Windows加上一双眼睛,察看使用者在机器上所做的各种操作(例如建立.删除文件:改变文件或目录名字)呢? 这里介绍一种利用Windows未公开函数实现这个功能的方法. 在Windows下有一个未公开函数SHChangeNotifyRegister可以把你的窗口添加到系统的系统消息监视链中,该函数在Delphi中的定义如下: Function SHChangeNotifyRegister(hWnd,uFlags,dwEventID,uMSG,cItems:

监控文件系统的变化

首先安装aide文件监控工具 yum install aide -y /etc/aide.conf  配置文件 3 @@define DBDIR /var/lib/aide 4 @@define LOGDIR /var/log/aide  以上是它的变量 7 database=file:@@{DBDIR}/aide.db.gz  是以.gz的格式压缩,这是压缩后的数据库存放位置 在/var/lib/aide目录下. 12database_out=file:@@{DBDIR}/aide.db.ne

通过inotify监控linux文件系统变化

http://www.mjmwired.net/kernel/Documentation/filesystems/inotify.txt http://www.ibm.com/developerworks/linux/library/l-ubuntu-inotify/index.html?ca=drs- http://linux.die.net/man/7/inotify http://en.wikipedia.org/wiki/Inotify   Systems administration

Java监控文件变化

NIO.2的Path类提供了如下的一个方法来监听文件系统的变化. register(WatcherService watcher,WatchEvent.Kind<?>... events):用watcher监听该path代表的目录下文件变化.event参数指定要监听哪些类型的事件. WatchService有三个方法来监听目录的文件变化事件. WatchKey poll():获取下一个WatchKey,如果没有WatchKey发生就立即返回null: WatcheKey poll(long t

监控磁盘文件变化代码

//文件监听器 public class FileListener extends FileAlterationListenerAdaptor{ //日志操作 private static final Logger logger = LoggerFactory.getLogger(FileListener.class); @Override public void onDirectoryChange(File directory) { System.out.println("文件目录变更了:&q