Powershell FileSystemWatcher 脚本在一些新文件上触发两次
Powershell FileSystemWatcher script firing twice on some new files
我正在使用 FileSystemWatcher 监视文档扫描到的文件夹。当检测到新文件时,它会发送电子邮件通知某人。它按原样工作,但有时(不是每个文件)它会在新文件上触发 2 或 3 次,并为同一文件发送电子邮件 2-3 次。我猜这与扫描仪创建文件的方式或类似方式有关。
我正在尝试找出一种方法来防止这种情况发生,以确保每个文件只发送一封电子邮件。任何建议将不胜感激。
$PathToMonitor = "\path\to\folder"
$FileSystemWatcher = New-Object System.IO.FileSystemWatcher
$FileSystemWatcher.Path = $PathToMonitor
$FileSystemWatcher.Filter = "*.*"
$FileSystemWatcher.IncludeSubdirectories = $false
$FileSystemWatcher.EnableRaisingEvents = $true
$Action = {
if ($EventArgs.Name -notlike "*.pdf" -and $EventArgs.Name -notlike "*.tif") {
return
}
$details = $event.SourceEventArgs
$Name = $details.Name
$Timestamp = $event.TimeGenerated
$text = "{0} was submitted on {1}." -f $Name, $Timestamp
$FromAddress = "Email1 <email1@email.com>"
$ToAddress = "Email2 <Email2@email.com>"
$Subject = "New File"
$SMTPserver = "123.4.5.678"
Send-MailMessage -From $FromAddress -To $ToAddress -Subject $Subject -Body $text -SmtpServer $SMTPserver
}
$handlers = . {
Register-ObjectEvent -InputObject $FileSystemWatcher -EventName Created -Action $Action -SourceIdentifier FSCreateConsumer
}
try {
do {
Wait-Event -Timeout 5
} while ($true)
}
finally {
Unregister-Event -SourceIdentifier FSCreateConsumer
$handlers | Remove-Job
$FileSystemWatcher.EnableRaisingEvents = $false
$FileSystemWatcher.Dispose()
}
这可能是因为你听多了notifications。默认值为 LastWrite
、FileName
和 DirectoryName
FileName
足以满足您的需求,可能会避免您的问题。
$FileSystemWatcher.NotifyFilter = [System.IO.NotifyFilters]::FileName
顺便说一句,我不知道你为什么要用Wait-Event -Timeout 5
。脚本在没有 try{}
块的情况下工作正常。
编辑:添加 ConcurrentDictionary 以避免重复事件
试试这个示例代码。我只包含了您脚本的开头部分。完结未动。
$PathToMonitor = "\path\to\folder"
$KeepFiles = 5 #minutes
$MonitoredFiles = New-Object -TypeName 'System.Collections.Concurrent.ConcurrentDictionary[[System.String],[System.DateTime]]'
$FileSystemWatcher = New-Object System.IO.FileSystemWatcher
$FileSystemWatcher.Path = $PathToMonitor
$FileSystemWatcher.Filter = "*.*"
$FileSystemWatcher.IncludeSubdirectories = $false
$FileSystemWatcher.NotifyFilter = [System.IO.NotifyFilters]::FileName
$FileSystemWatcher.EnableRaisingEvents = $true
$Action = {
if ($EventArgs.Name -notlike "*.pdf" -and $EventArgs.Name -notlike "*.tif") {
return
}
#Cleaning events -gt 5mn
$Now = [System.DateTime]::Now
$OriginEventDate = [System.DateTime]::MinValue
foreach($MonitoredFile in [System.Linq.Enumerable]::ToList(($MonitoredFiles.Keys))) {
if ($MonitoredFiles.TryGetValue($MonitoredFile, [ref]$OriginEventDate)) {
if ($OriginEventDate.AddMinutes($KeepFiles) -gt $Now) {
try {
[void]$MonitoredFiles.Remove($MonitoredFile)
}
catch {}
}
}
}
$ProcessEvent = $false
# any same file creation event within 5mn are discarded
$OriginEventDate = [System.DateTime]::MinValue
if ($MonitoredFiles.TryGetValue($event.SourceEventArgs.Name, [ref]$OriginEventDate)) {
if ($OriginEventDate -ne [System.DateTime]::MinValue -and $OriginEventDate.AddMinutes($KeepFiles) -le $Now) {
return
}
else {
$ProcessEvent = $true
}
}
else {
#not successful means a concurrent event was successful, so discard this one.
if ($MonitoredFiles.TryAdd($event.SourceEventArgs.Name, $event.SourceEventArgs.TimeGenerated)) {
$ProcessEvent = $true
}
else {
return
}
}
if ($ProcessEvent) {
$details = $event.SourceEventArgs
$Name = $details.Name
$Timestamp = $event.TimeGenerated
$text = "{0} was submitted on {1}." -f $Name, $Timestamp
$FromAddress = "Email1 <email1@email.com>"
$ToAddress = "Email2 <Email2@email.com>"
$Subject = "New File"
$SMTPserver = "123.4.5.678"
Send-MailMessage -From $FromAddress -To $ToAddress -Subject $Subject -Body $text -SmtpServer $SMTPserver
}
}
我必须调整这两项才能使其正常工作:
if ($MonitoredFiles.TryAdd($Event.SourceEventArgs.Name, $Event.TimeGenerated))
if ($OriginEventDate -ne [System.DateTime]::MinValue -and $OriginEventDate.AddMinutes($KeepFiles) -ge $Now)
我正在使用 FileSystemWatcher 监视文档扫描到的文件夹。当检测到新文件时,它会发送电子邮件通知某人。它按原样工作,但有时(不是每个文件)它会在新文件上触发 2 或 3 次,并为同一文件发送电子邮件 2-3 次。我猜这与扫描仪创建文件的方式或类似方式有关。
我正在尝试找出一种方法来防止这种情况发生,以确保每个文件只发送一封电子邮件。任何建议将不胜感激。
$PathToMonitor = "\path\to\folder"
$FileSystemWatcher = New-Object System.IO.FileSystemWatcher
$FileSystemWatcher.Path = $PathToMonitor
$FileSystemWatcher.Filter = "*.*"
$FileSystemWatcher.IncludeSubdirectories = $false
$FileSystemWatcher.EnableRaisingEvents = $true
$Action = {
if ($EventArgs.Name -notlike "*.pdf" -and $EventArgs.Name -notlike "*.tif") {
return
}
$details = $event.SourceEventArgs
$Name = $details.Name
$Timestamp = $event.TimeGenerated
$text = "{0} was submitted on {1}." -f $Name, $Timestamp
$FromAddress = "Email1 <email1@email.com>"
$ToAddress = "Email2 <Email2@email.com>"
$Subject = "New File"
$SMTPserver = "123.4.5.678"
Send-MailMessage -From $FromAddress -To $ToAddress -Subject $Subject -Body $text -SmtpServer $SMTPserver
}
$handlers = . {
Register-ObjectEvent -InputObject $FileSystemWatcher -EventName Created -Action $Action -SourceIdentifier FSCreateConsumer
}
try {
do {
Wait-Event -Timeout 5
} while ($true)
}
finally {
Unregister-Event -SourceIdentifier FSCreateConsumer
$handlers | Remove-Job
$FileSystemWatcher.EnableRaisingEvents = $false
$FileSystemWatcher.Dispose()
}
这可能是因为你听多了notifications。默认值为 LastWrite
、FileName
和 DirectoryName
FileName
足以满足您的需求,可能会避免您的问题。
$FileSystemWatcher.NotifyFilter = [System.IO.NotifyFilters]::FileName
顺便说一句,我不知道你为什么要用Wait-Event -Timeout 5
。脚本在没有 try{}
块的情况下工作正常。
编辑:添加 ConcurrentDictionary 以避免重复事件
试试这个示例代码。我只包含了您脚本的开头部分。完结未动。
$PathToMonitor = "\path\to\folder"
$KeepFiles = 5 #minutes
$MonitoredFiles = New-Object -TypeName 'System.Collections.Concurrent.ConcurrentDictionary[[System.String],[System.DateTime]]'
$FileSystemWatcher = New-Object System.IO.FileSystemWatcher
$FileSystemWatcher.Path = $PathToMonitor
$FileSystemWatcher.Filter = "*.*"
$FileSystemWatcher.IncludeSubdirectories = $false
$FileSystemWatcher.NotifyFilter = [System.IO.NotifyFilters]::FileName
$FileSystemWatcher.EnableRaisingEvents = $true
$Action = {
if ($EventArgs.Name -notlike "*.pdf" -and $EventArgs.Name -notlike "*.tif") {
return
}
#Cleaning events -gt 5mn
$Now = [System.DateTime]::Now
$OriginEventDate = [System.DateTime]::MinValue
foreach($MonitoredFile in [System.Linq.Enumerable]::ToList(($MonitoredFiles.Keys))) {
if ($MonitoredFiles.TryGetValue($MonitoredFile, [ref]$OriginEventDate)) {
if ($OriginEventDate.AddMinutes($KeepFiles) -gt $Now) {
try {
[void]$MonitoredFiles.Remove($MonitoredFile)
}
catch {}
}
}
}
$ProcessEvent = $false
# any same file creation event within 5mn are discarded
$OriginEventDate = [System.DateTime]::MinValue
if ($MonitoredFiles.TryGetValue($event.SourceEventArgs.Name, [ref]$OriginEventDate)) {
if ($OriginEventDate -ne [System.DateTime]::MinValue -and $OriginEventDate.AddMinutes($KeepFiles) -le $Now) {
return
}
else {
$ProcessEvent = $true
}
}
else {
#not successful means a concurrent event was successful, so discard this one.
if ($MonitoredFiles.TryAdd($event.SourceEventArgs.Name, $event.SourceEventArgs.TimeGenerated)) {
$ProcessEvent = $true
}
else {
return
}
}
if ($ProcessEvent) {
$details = $event.SourceEventArgs
$Name = $details.Name
$Timestamp = $event.TimeGenerated
$text = "{0} was submitted on {1}." -f $Name, $Timestamp
$FromAddress = "Email1 <email1@email.com>"
$ToAddress = "Email2 <Email2@email.com>"
$Subject = "New File"
$SMTPserver = "123.4.5.678"
Send-MailMessage -From $FromAddress -To $ToAddress -Subject $Subject -Body $text -SmtpServer $SMTPserver
}
}
我必须调整这两项才能使其正常工作:
if ($MonitoredFiles.TryAdd($Event.SourceEventArgs.Name, $Event.TimeGenerated))
if ($OriginEventDate -ne [System.DateTime]::MinValue -and $OriginEventDate.AddMinutes($KeepFiles) -ge $Now)