我修改了一个监视文件夹的小脚本,并在该文件夹发生更改时发出警报。以下是我到目前为止的情况:
$folder = 'folder\to\monitor'
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $true;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}
Register-ObjectEvent $fsw Changed -SourceIdentifier FileChanged1 -Action {
$path = $Event.SourceEventArgs.FullPath
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
$datestamp = get-date -uformat "%Y-%m-%d@%H-%M-%S"
$Computer = get-content env:computername
$Body = " $path on $Computer was $changeType at $timeStamp"
$PIECES=$path.split("\")
$newfolder=$PIECES[-2]
Out-File -FilePath c:\Scripts\config_changes.txt -Append -InputObject " $path on $Computer was $changeType at $timeStamp"
Send-MailMessage -To "email@domain.com" -From "email@domain.com" -Subject $Body -SmtpServer "192.168.#.##" -Body " "
#Copy-Item $path "c:\scripts\versions\$newfolder"
#Rename-Item "c:\Scripts\Versions\$newfolder\web.config" "web.config-$datestamp"
#Remove-Item -path "c:\Scripts\Versions\$newfolder\web.config" -force}
# To stop the monitoring, run the following commands:
# Unregister-Event FileChanged1
但是,我想制定"标准"对于文件更改更具体一点。根据我所做的研究,FileSystemWatcher.Notify过滤器将我限制为文件名,上次写入等。我想要做的是检查是否对内容进行了任何更改的文件。如果没有任何内容发生变化,那么我就不希望它发出警报。
即使没有更改,文件也会不断被覆盖,因此即使没有内容更改,修改日期也会继续更改。