温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Powershell 之监控文件系统变化

发布时间:2020-06-21 01:30:01 来源:网络 阅读:898 作者:海底小纵队 栏目:系统运维
#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()



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI