none
System.IO.FileSystemWatcher - Doesn't seem to work past a few changes to the target folder RRS feed

  • Question

  • I have a function I have written to create a new file system watcher on a folder. I can't seem to get it to monitor the folder for longer than about 3-4 actions, or it seems once I have deleted a file from the directory, all my ObjectEvents stop working and the function is basically useless :(

    Can anyone see why I am unable to have this running consistently? I've done my due diligence with a google search, but either I am to stupid, or the info isn't there.

    Thanks in advance

    function New-FileSystemWatcher { param( [parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]$Path, [parameter(Mandatory=$false)][Bool]$Recursive=$false, [parameter(Mandatory=$false)][ValidateSet('Error','Warning','Information','')][String[]]$EventType='Warning' ) # Test to ensure that the event log source - 'FileSystemWatcher' If ([System.Diagnostics.EventLog]::SourceExists('FileSystemWatcher') -eq $true) { # Do nothing } else { New-EventLog -Source 'FileSystemWatcher' } $watcher = New-Object System.IO.FileSystemWatcher $watcher.Path = $Path $watcher.IncludeSubdirectories = $Recursive $watcher.EnableRaisingEvents = $true $watcher.filter = "*.*" $watcher.NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite, CreationTime, Attributes, LastAccess, Size, Security, DirectoryName' $watcher.InternalBufferSize = '32768' $changed = Register-ObjectEvent $watcher "Changed" -SupportEvent -Action { Write-Host "Changed: $($eventArgs.FullPath)" Write-EventLog -LogName 'Application' -Source "FileSystemWatcher" -EntryType $EventType -EventId 0001 -Message "Changed: $($eventArgs.FullPath)" -ComputerName (Get-ChildItem -Path Env:\COMPUTERNAME).value } $created = Register-ObjectEvent $watcher "Created" -SupportEvent -Action { Write-Host "Created: $($eventArgs.FullPath)" Write-EventLog -LogName 'Application' -Source "FileSystemWatcher" -EntryType $EventType -EventId 0002 -Message "Created: $($eventArgs.FullPath)" -ComputerName (Get-ChildItem -Path Env:\COMPUTERNAME).value } $deleted = Register-ObjectEvent $watcher "Deleted" -SupportEvent -Action { Write-Host "Deleted: $($eventArgs.FullPath)" Write-EventLog -LogName 'Application' -Source "FileSystemWatcher" -EntryType $EventType -EventId 0003 -Message "Deleted: $($eventArgs.FullPath)" -ComputerName (Get-ChildItem -Path Env:\COMPUTERNAME).value } $renamed = Register-ObjectEvent $watcher "Renamed" -SupportEvent -Action { Write-Host "Renamed: $($eventArgs.FullPath)" Write-EventLog -LogName 'Application' -Source "FileSystemWatcher" -EntryType $EventType -EventId 0004 -Message "Renamed: $($eventArgs.FullPath)" -ComputerName (Get-ChildItem -Path Env:\COMPUTERNAME).value } }

    # Here is the function being called to do it s magic.
    $path = 'c:\MyPath\'
    New-FileSystemWatcher -Path $path



    • Edited by SeanVucich Monday, December 18, 2017 12:50 AM
    • Moved by Bill_Stewart Friday, January 26, 2018 3:38 PM This is not "teach me PowerShell basics step by step" forum
    Monday, December 18, 2017 12:37 AM

All replies

  • You are never calling your function.


    \_(ツ)_/

    Monday, December 18, 2017 12:47 AM
  • You are never calling your function.


    \_(ツ)_/

    All I need to do to replicate the fault is load the function and run the following:
    $path = 'c:\MyPath\'
    New-FileSystemWatcher -Path $path

    When the function is loaded into memory and file system changes are made, I get about 3-4 events and then after a delete, the events no longer print to the powershell console or the event logs... :(

    Monday, December 18, 2017 12:50 AM
  • How are you loading the function?


    \_(ツ)_/

    Monday, December 18, 2017 12:56 AM
  • How are you loading the function?


    \_(ツ)_/

    I use the powershell_ise for the initial testing. This scripts loads A-OK, and initially runs, it just doesn't keep running :(

    Monday, December 18, 2017 1:35 AM
  • Further this, I also attempted to load this via a Powershell console to see if it was an ISE issue and I get the same result. The file system watcher works until I delete a file, then It no longer works :(
    Monday, December 18, 2017 1:46 AM
  • Don't use ISE.  Open PowerShell and paste in the function and run it.  It will work.


    \_(ツ)_/

    Monday, December 18, 2017 1:51 AM
  • Don't use ISE.  Open PowerShell and paste in the function and run it.  It will work.


    \_(ツ)_/

    I've tried this and I get the exact same result.
    Monday, December 18, 2017 2:10 AM
  • I don't.  You must open PS and copy and paste the code.  It cannot be run from a file as you have written it.  I am trying to show you how this actually works.

    Follow my instructions exactly.  Do NOT use ISE!


    \_(ツ)_/


    • Edited by jrv Monday, December 18, 2017 2:35 AM
    Monday, December 18, 2017 2:35 AM
  • I don't.  You must open PS and copy and paste the code.  It cannot be run from a file as you have written it.  I am trying to show you how this actually works.

    Follow my instructions exactly.  Do NOT use ISE!


    \_(ツ)_/


    Ah, I can get get this function to load and run and I also get the same result using both the ISE and a powershell window. I am not having trouble loading this Function, I am having trouble with it continuing to run. It fails after a delete event. I know how to paste a function into a powershell window... See the pic... And I know how to then trigger the funtion... See the pic.

    Hopefully this pic clarify's things for you. 

    Monday, December 18, 2017 2:41 AM
  • I have used this class hundreds of times.  You function works for mem with no issues.  You have an error.  You an error.  First fix the error.

    You cannot create an app log event source or update one without running elevated.  Comment out all event log calls and the code will run correctly.

    Start simple and add complexity until you have understood each element.


    \_(ツ)_/

    Monday, December 18, 2017 2:47 AM
  • I have used this class hundreds of times.  You function works for mem with no issues.  You have an error.  You an error.  First fix the error.

    You cannot create an app log event source or update one without running elevated.  Comment out all event log calls and the code will run correctly.

    Start simple and add complexity until you have understood each element.


    \_(ツ)_/

    WOW - Thanks for the ad hominem... I am on here as I am trying to understand why the delete event stops this from working. And yes, I have been running this elevated.

    Thanks.

    Monday, December 18, 2017 2:58 AM
  • It works fine for me.  The class has never failed in the way you are claiming.  Perhaps you requires a system repair.


    \_(ツ)_/

    Monday, December 18, 2017 3:14 AM
  • It works fine for me.  The class has never failed in the way you are claiming.  Perhaps you requires a system repair.


    \_(ツ)_/

    Well, thanks for this advice. It's really been helpful :|
    Monday, December 18, 2017 6:42 PM