Hey Guys -
I'm writing a script which among other things monitors a specific share (including sub-folders) and when a change of any file is detected, it should copy the change to a 2nd share. The issue I'm having during testing is when a change is detected of a file within a subfolder, it copies the changed file to the root of the target share - not the same folder it resided in on the source.
Below is an example followed by my code. I'm sure it's something like a simple parameter I'm leaving out, but been staring at it too long :)
Example
- Monitored Share: \\server1\files\
- Target Share: \\server2\files\
With script running - if the file test.txt changes (or is added) within \\server1\files\folder\, the changed file is copied into \\server2\files\ - not \\server2\files\folder\
Current Script
$sourceshare = "\\server1\files"
$targetshare = "\\server2\files"
$filter = '*.*'
$fsw = New-Object IO.FileSystemWatcher $sourceshare, $filter -Property @{
IncludeSubdirectories = $true
NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}
$onCreated = Register-ObjectEvent $fsw Changed -SourceIdentifier FileChanged -Action {
$path = $Event.SourceEventArgs.FullPath
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
Write-Host "path = $path"
Write-Host "name = $name"
Write-Host "changeType = $changeType"
Copy-Item $path -Destination $targetshare -Force -Verbose
}
Any suggestions? Thanks!
Ben K.