I have a script that monitors a folder and it’s subfolders. It is used by the ftp server as a landing location for when we receive ftp files. The script sends an email to the recepients that a new file has been created in the folder. The script works, but It sends an email for every file that was copied to the location. I only want it to send 1 email to the recepients if a file was created in the last 6 hours. This way if they ftp more than 1 file in the last 6 hours, then only 1 email goes out. How can i modify my script to do this?
# make sure you adjust this to point to the folder you want to monitor
$PathToMonitor = “U:\FTPFolder\Test"
explorer $PathToMonitor
$FileSystemWatcher = New-Object System.IO.FileSystemWatcher
$FileSystemWatcher.Path = $PathToMonitor
$FileSystemWatcher.IncludeSubdirectories = $true
# make sure the watcher emits events
$FileSystemWatcher.EnableRaisingEvents = $true
# define the code that should execute when a file change is detected
$Action = {
$details = $event.SourceEventArgs
$Name = $details.Name
$FullPath = $details.FullPath
$OldFullPath = $details.OldFullPath
$OldName = $details.OldName
$ChangeType = $details.ChangeType
$Timestamp = $event.TimeGenerated
$text = "{0} was {1} at {2}" -f $FullPath, $ChangeType, $Timestamp
Write-Host ""
Write-Host $text -ForegroundColor Green
# you can also execute code based on change type here
switch ($ChangeType)
{
'Changed' {
#$EmailFrom = “EmailAddress2@myemail.com”
#$EmailTo = “EmailAddress2@myemail.com, Person2@hotmail.com”
#$Subject = “New File FTP””
#$Body = $text
#$SMTPServer = “smtp.office365.com”
#$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
#$SMTPClient.EnableSsl = $true
#$SMTPClient.Credentials = New-Object System.Net.NetworkCredential(“EmailAddress1@myemail.com”, “Password”);
#$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
#Start-Sleep -Seconds 4
#$SMTPClient.Dispose()
# this executes only when a file was renamed
$text = "File {0} was Changed" -f $FullPath
Write-Host $text -ForegroundColor Yellow
}
'Created' {
$EmailFrom = “EmailAddress2@ myemail.com”
$EmailTo = “EmailAddress2@myemail.com, Person2@hotmail.com”
$Subject = “New File FTP”
$Body = $text
$SMTPServer = “smtp.office365.com”
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential(“EmailAddress1@myemail.com”, “Password”);
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
Start-Sleep -Seconds 4
$SMTPClient.Dispose()
# this executes only when a file was renamed
$text = "File {0} was Created" -f $FullPath
Write-Host $text -ForegroundColor Yellow
}
'Deleted' {
#$EmailFrom = “EmailAddress2@ myemail.com”
#$EmailTo = “EmailAddress2@myemail.com, Person2@hotmail.com”
#$Subject = “New File FTP”
#$Body = $text
#$SMTPServer = “smtp.office365.com”
#$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
#$SMTPClient.EnableSsl = $true
#$SMTPClient.Credentials = New-Object System.Net.NetworkCredential(“EmailAddress1@myemail.com”, “Password”);
#$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
#Start-Sleep -Seconds 4
#$SMTPClient.Dispose()
# this executes only when a file was renamed
$text = "File {0} was deleted" -f $FullPath
Write-Host $text -ForegroundColor Yellow
# uncomment the below to mimick a time intensive handler
<#
Write-Host "Deletion Handler Start" -ForegroundColor Gray
Start-Sleep -Seconds 4
Write-Host "Deletion Handler End" -ForegroundColor Gray
#>
}
'Renamed' {
#$EmailFrom = “EmailAddress2@ myemail.com”
#$EmailTo = “EmailAddress2@myemail.com, Person2@hotmail.com”
#$Subject = “New File FTP”
#$Body = $text
#$SMTPServer = “smtp.office365.com”
#$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
#$SMTPClient.EnableSsl = $true
#$SMTPClient.Credentials = New-Object System.Net.NetworkCredential(“EmailAddress1@myemail.com”, “Password”);
#$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
#Start-Sleep -Seconds 4
#$SMTPClient.Dispose()
# this executes only when a file was renamed
$text = "File {0} was renamed to {1}" -f $OldName, $Name
Write-Host $text -ForegroundColor Yellow
}
default { Write-Host $_ -ForegroundColor Red -BackgroundColor White }
}
}
# add event handlers
$handlers = . {
Register-ObjectEvent -InputObject $FileSystemWatcher -EventName Changed -Action $Action -SourceIdentifier FSChange
Register-ObjectEvent -InputObject $FileSystemWatcher -EventName Created -Action $Action -SourceIdentifier FSCreate
Register-ObjectEvent -InputObject $FileSystemWatcher -EventName Deleted -Action $Action -SourceIdentifier FSDelete
Register-ObjectEvent -InputObject $FileSystemWatcher -EventName Renamed -Action $Action -SourceIdentifier FSRename
}
Write-Host "Watching for changes to $PathToMonitor"
try
{
do
{
Wait-Event -Timeout 1
Write-Host "." -NoNewline
} while ($true)
}
finally
{
# this gets executed when user presses CTRL+C
# remove the event handlers
Unregister-Event -SourceIdentifier FSChange
Unregister-Event -SourceIdentifier FSCreate
Unregister-Event -SourceIdentifier FSDelete
Unregister-Event -SourceIdentifier FSRename
# remove background jobs
$handlers | Remove-Job
# remove filesystemwatcher
$FileSystemWatcher.EnableRaisingEvents = $false
$FileSystemWatcher.Dispose()
"Event Handler disabled."
}