gpt4 book ai didi

Powershell FileSystemWatcher 脚本在一些新文件上触发两次

转载 作者:行者123 更新时间:2023-12-05 03:34:23 26 4
gpt4 key购买 nike

我正在使用 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 .默认为 LastWriteFileNameDirectoryNameFileName 足以满足您的需要,可能会阻止您的问题。

$FileSystemWatcher.NotifyFilter = [System.IO.NotifyFilters]::FileName

顺便说一下,我不知道你为什么要使用 Wait-Event -Timeout 5。脚本在没有 try{} block 的情况下工作正常。

编辑:添加一个 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
}
}

关于Powershell FileSystemWatcher 脚本在一些新文件上触发两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70159490/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com