Quantcast
Channel: Common Language Runtime Internals and Architecture forum
Viewing all articles
Browse latest Browse all 1710

FileSystemWatcher is not triggering on Program Event

$
0
0

Hello,

I have setup a file system watcher to monitor a folder. When I create a file in the folder, the event triggers and all is well. However, the reason I created this was to manipulate files being created in that folder by another program. When the program creates a file in that folder, the event does not trigger the file system watcher. Any idea how to resolve this?

Here is my script:

#region Filesystem Watcher

$fileWatcher = New-Object System.IO.FileSystemWatcher
$fileWatcher.Path = "C:\CIP Reports"
Register-ObjectEvent -InputObject $fileWatcher -EventName Created -SourceIdentifier File.Created -Action {
 Get-ChildItem 'c:\CIP Reports\*.pdf' -File |
 Sort-Object -Property CreationTime -Descending |
 Select-Object -First 1 |
 Copy-Item -Destination 'c:\CIP Reports\test'

    $Global:t = $event
    Write-Host ("File Created: {0} on {1}" -f $event.SourceEventArgs.Name,
    (Split-Path $event.SourceEventArgs.FullPath))

$report = Get-ChildItem 'c:\CIP Reports' -File |
 Sort-Object -Property CreationTime -Descending |
 Select-Object -First 1

filter timestamp {"$(Get-Date -Format G): $_"}

$fromaddress =
$toaddress =
$bccaddress =
$CCaddress =
$Subject = "Historian::CIP Report"
$body = "A report from Historian has been generated. Please see attached file." | timestamp
$attachment = "c:\CIP Reports\$report"
$smtpserver =

####################################

$message = new-object System.Net.Mail.MailMessage
$message.From = $fromaddress
$message.To.Add($toaddress)
$message.CC.Add($CCaddress)
$message.Bcc.Add($bccaddress)
$message.IsBodyHtml = $True
$message.Subject = $Subject
$attach = new-object Net.Mail.Attachment($attachment)
$message.Attachments.Add($attach)
$message.body = $body
$smtp = new-object Net.Mail.SmtpClient($smtpserver)
$smtp.Send($message)

 Get-ChildItem 'c:\CIP Reports\*.pdf' -File |
 Sort-Object -Property CreationTime -Descending |
 Select-Object -First 1 |
 Move-Item -Destination 'c:\CIP Reports\Archive'

} | Out-Null

If I save a file to the CIP Reports folder, the event triggers and PowerShell outputs the notification. The file is copied and moved to the Archive location without issue. When the program drops its file into the folder, nothing is trigger. Any help is greatly appreciated.

IT Support Administrator


Viewing all articles
Browse latest Browse all 1710

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>