1

I use a powershell script to watch a folder and get notification when a new file is created.

The script is the following

### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
    $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.Path = "C:\Users\Desktop\test\"
    $watcher.Filter = "*.nrrd"
    $watcher.IncludeSubdirectories = $true
    $watcher.EnableRaisingEvents = $true  

### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
    $action = 
    { 
        $path = $Event.SourceEventArgs.FullPath
        $changeType = $Event.SourceEventArgs.ChangeType

        Write-Host "The file '$path' was $changeType at '$(Get-Date)'" -fore green   
    }    
### DECIDE WHICH EVENTS SHOULD BE WATCHED 
    Register-ObjectEvent $watcher "Created" -Action $action
    while ($true) {sleep 5}

It works fine even if I create the file manually or with a .bat, PHP or even python.

The only way it does not work is when another user (or session?) is creating the file.

For example:

I created a folder on my computer, shared it with other computer over LAN. When the file is created by me (or one of my script), the script is well working and the sentence is displayed.

If I use another computer on the LAN to add a file in the same folder, it does not work. Nothing is displayed.

(But if I copy/past the file created by the other user, the script is working, so is not a issue with the file itself).


I found this and this but the posts are outdated.

Do you have any workaround or idea to solve this?

UPDATE

The file created by hand (or copy/pasted) has owner DEBIAN\root but the original one have Unix User\www-data.

Maybe powershell (windows) is not able to "catch" this kind of user that's maybe why the event is not even created?

Atnaize
  • 1,766
  • 5
  • 25
  • 54

1 Answers1

0

Try giving the shared folder's UNC path

$watcher.Path = "\\server\share"
Pang
  • 9,564
  • 146
  • 81
  • 122
Prasoon Karunan V
  • 2,916
  • 2
  • 12
  • 26