0

In the code below action.bat - calls a java process passing the filename as argument and log.txt - logs a line that filename is created with timestamp.

I am testing this script by dropping 10 txt files which works perfectly, I repeated this test couple of times and log.txt gets updated and also action.bat is called correctly.

However when I leave this filewatcher running for 4 hours and drop 10 new text files only log.txt got updated but the action.bat file is not called.

Again when I kill the powershell and restart the script and test again with 10 new files it worked fine.

why wouldn't Start-process get called after waiting for 4 hours? but the log statement is updated in action

my code is similar to net event filewatch.ps1

 $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.Path = "D:\code\Apps\input"
    $watcher.Filter = "*.txt"
    $watcher.IncludeSubdirectories = $false
    $watcher.EnableRaisingEvents = $true 
### DEFINE ACTIONS AFTER A EVENT IS DETECTED
    $action = {
    $path = $Event.SourceEventArgs.FullPath
                $changeType = $Event.SourceEventArgs.ChangeType
                $logline = "$(Get-Date), $changeType, $path"
                Add-content "D:\code\Apps\log.txt" -value $logline   
                Start-process -Filepath D:\code\Apps\action.bat $path -Wait -passthru; 
                }    

### DECIDE WHICH EVENTS SHOULD BE WATCHED + SET CHECK FREQUENCY  
    $created = Register-ObjectEvent $watcher "Created" -Action $action
    while ($true) {sleep 10}
Community
  • 1
  • 1
sunnytech
  • 1
  • 2

1 Answers1

0

It looks like you are missing a parameter on the Register-ObjectEvent CmdLet, -SourceIdentifier 'FileCreated'.

Corrected Code

 $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.Path = "D:\code\Apps\input"
    $watcher.Filter = "*.txt"
    $watcher.IncludeSubdirectories = $false
    $watcher.EnableRaisingEvents = $true 
### DEFINE ACTIONS AFTER A EVENT IS DETECTED
    $action = {
    $path = $Event.SourceEventArgs.FullPath
                $changeType = $Event.SourceEventArgs.ChangeType
                $logline = "$(Get-Date), $changeType, $path"
                Add-content "D:\code\Apps\log.txt" -value $logline   
                Start-process -Filepath D:\code\Apps\action.bat $path -Wait -passthru; 
                }    

### DECIDE WHICH EVENTS SHOULD BE WATCHED + SET CHECK FREQUENCY  
    $created = Register-ObjectEvent $watcher 'Created' -SourceIdentifier 'FileCreated' -Action $action
    #while ($true) {Start-Sleep 10}
TravisEz13
  • 2,263
  • 1
  • 20
  • 28
  • Thanks for your reply Travis, As I mentioned code I posted was working fine before except Start-process not being called. When I replaced with this corrected code, I am not able to run the script. however when I uncommented #while ($true) {Start-Sleep 10} It seems to be ok why would 'while' cause me issue? – sunnytech May 15 '16 at 18:42
  • I ran the code in and interactive session. The while was not needed in an interactive session, and the while loop **may** keep the thread which processes the events busy. I put some code into launch notepad in `action.bat` and notepad was launched. – TravisEz13 May 16 '16 at 19:17