none
FileSystemWatcher is not triggering on Program Event RRS feed

  • Question

  • 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

    Thursday, August 23, 2018 4:38 PM

All replies

  • Hi,

    Thank you for posting here.

    Since your question is more related to PowerShell, you could post a new thread in PowerShell forum for suitable support.

    https://social.technet.microsoft.com/Forums/en-US/home?forum=winserverpowershell

    The CLR Forum discuss and ask questions about .NET Framework Base Classes (BCL) such as Collections, I/O, Regigistry, Globalization, Reflection. Also discuss all the other Microsoft libraries that are built on or extend the .NET Framework, including Managed Extensibility Framework (MEF), Charting Controls, CardSpace, Windows Identity Foundation (WIF), Point of Sale (POS), Transactions.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, August 27, 2018 6:52 AM