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}
It looks like you are missing a parameter on the Register-ObjectEvent
CmdLet, -SourceIdentifier 'FileCreated'
.
$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}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.