Last active
January 2, 2017 09:09
-
-
Save bymyslf/4c956ff4b7bd4f4f60dd4695d7be63e9 to your computer and use it in GitHub Desktop.
PowerShell module to register a FileSystemWatcher to copy files from source folder to destination folder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Register-Watcher-ToCopy { | |
param ( | |
[string]$sourceFolder, | |
[string]$destFolder, | |
[string]$id | |
) | |
$filter = "*.*" | |
$watcher = New-Object System.IO.FileSystemWatcher $sourceFolder, $filter -Property @{ | |
IncludeSubdirectories = $true | |
EnableRaisingEvents = $true | |
} | |
$messageData = New-Object psobject -Property @{ | |
SourceFolder = $sourceFolder | |
DestFolder = $destFolder | |
} | |
$action = { | |
ROBOCOPY $Event.MessageData.SourceFolder $Event.MessageData.DestFolder /E /PURGE | |
} | |
Register-ObjectEvent -InputObject $watcher -EventName "Changed" -SourceIdentifier $id -MessageData $messageData -Action $action | |
} | |
Export-ModuleMember Register-Watcher-ToCopy |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment