I have the below code to detect the creation of a new file and grab a copy of it.
$folder = \'\\\\server\\share\\monitor_me\\\'
$filter = \'*.*\'
$copyToFolder
Resolved by changing the monitored event to CHANGED. It seems 2 change events occur after the initial create. For simplicity I just monitor for any change event (thus ensuring I'll always have the latest copy) and include the -Force
parameter to allow the destination file to be overwritten.
Register-ObjectEvent $fsw Changed -SourceIdentifier FileUpdated -Action {
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
Write-Host "The file '$name' was $changeType at $timeStamp" -fore green
Copy-Item -Path $Event.SourceEventArgs.FullPath -Destination $copyToFolder -Force
}
NB: I'm not 100% sure whether this will now capture all new files; as potentially there's some way to create a file without also triggering a change event (i.e. I've not found any documentation to say this scenario doesn't exist); but seems good enough for my current purposes.