Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Clear-Host
- $ErrorActionPreference = [System.Management.Automation.ActionPreference]::Stop
- $fileSystemWatcherDirPath = 'C:\temp'
- $fileSystemWatcherFilter = '*.*'
- $fileSystemWatcher = [System.IO.FileSystemWatcher]::new($fileSystemWatcherDirPath , $fileSystemWatcherFilter)
- $fileSystemWatcher.IncludeSubdirectories = $true
- $fileSystemWatcher.EnableRaisingEvents = $true
- $fileSystemWatcher.NotifyFilter = [System.IO.NotifyFilters]::FileName -bor [System.IO.NotifyFilters]::DirectoryName -bor [System.IO.NotifyFilters]::LastWrite # [System.Linq.Enumerable]::Sum([System.IO.NotifyFilters].GetEnumValues())
- # Create syncronized hashtable
- $syncdFsItemEventHashT = [hashtable]::Synchronized([hashtable]::new())
- $fileSystemWatcherAction = {
- try {
- $fsItemEvent = [pscustomobject]@{
- EventIdentifier = $Event.EventIdentifier
- SourceIdentifier = $Event.SourceIdentifier
- TimeStamp = $Event.TimeGenerated
- FullPath = $Event.SourceEventArgs.FullPath
- ChangeType = $Event.SourceEventArgs.ChangeType
- }
- # Collecting event in synchronized hashtable (overrides existing keys so that only the latest event details are available)
- $syncdFsItemEventHashT[$fsItemEvent.FullPath] = $fsItemEvent
- } catch {
- Write-Host ($_ | Format-List * | Out-String ) -ForegroundColor red
- }
- }
- # Script block which processes collected events and do further actions like copying for backup, etc...
- # That scriptblock was initially used to test "Start-Job". Unfortunately it's not possible to access and modify the synchronized hashtable created within this scope.
- $fSItemEventProcessingJob = {
- $keys = $syncdFsItemEventHashT.psbase.Keys
- foreach ($key in $keys) {
- $fsEvent = $syncdFsItemEventHashT[$key]
- try {
- if ($fsEvent.ChangeType -eq [System.IO.WatcherChangeTypes]::Deleted) {
- $syncdFsItemEventHashT.Remove($key )
- Write-Host ("==> Item '$key' with changetype '$($fsEvent.ChangeType)' removed from hashtable without any further actions!") -ForegroundColor Blue
- continue
- }
- # get filesystem object
- $fsItem = Get-Item -LiteralPath $fsEvent.FullPath
- if ($fsItem -is [System.IO.FileInfo]) {
- # file processing
- try {
- # Check whether the file is still locked / in use by another process
- [System.IO.FileStream]$fileStream = [System.IO.File]::Open( $fsEvent.FullPath, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::Read)
- $fileStream.Close()
- } catch [System.IO.IOException] {
- Write-Host ("==> Item '$key' with changetype '$($fsEvent.ChangeType)' is still in use and can't be read!") -ForegroundColor Yellow
- continue
- }
- }elseIf($fsItem -is [System.IO.DirectoryInfo]) {
- # directory processing
- }
- $syncdFsItemEventHashT.Remove($key )
- Write-Host ("==> Item '$key' with changetype '$($fsEvent.ChangeType)' has been processed and removed from hashtable.") -ForegroundColor Blue
- } catch {
- Write-Host ($_ | Format-List * | Out-String ) -ForegroundColor red
- }
- }
- }
- [void] (Register-ObjectEvent -InputObject $fileSystemWatcher -EventName 'Created' -SourceIdentifier 'FSCreated' -Action $fileSystemWatcherAction)
- [void] (Register-ObjectEvent -InputObject $fileSystemWatcher -EventName 'Changed' -SourceIdentifier 'FSChanged' -Action $fileSystemWatcherAction)
- [void] (Register-ObjectEvent -InputObject $fileSystemWatcher -EventName 'Renamed' -SourceIdentifier 'FSRenamed' -Action $fileSystemWatcherAction)
- [void] (Register-ObjectEvent -InputObject $fileSystemWatcher -EventName 'Deleted' -SourceIdentifier 'FSDeleted' -Action $fileSystemWatcherAction)
- Write-Host "Watching for changes in '$fileSystemWatcherDirPath'.`r`nPress CTRL+C to exit!"
- try {
- do {
- Wait-Event -Timeout 1
- if ($syncdFsItemEventHashT.Count -gt 0) {
- Write-Host "`r`n"
- Write-Host ('-' * 50) -ForegroundColor Green
- Write-Host "Collected events in hashtable queue:" -ForegroundColor Green
- $syncdFsItemEventHashT.Values | Format-Table | Out-String
- }
- # Process hashtable items and do something with them (like copying, ..)
- .$fSItemEventProcessingJob
- } while ($true)
- } finally {
- # unregister
- Unregister-Event -SourceIdentifier 'FSChanged'
- Unregister-Event -SourceIdentifier 'FSCreated'
- Unregister-Event -SourceIdentifier 'FSDeleted'
- Unregister-Event -SourceIdentifier 'FSRenamed'
- # dispose
- $FileSystemWatcher.EnableRaisingEvents = $false
- $FileSystemWatcher.Dispose()
- Write-Host "`r`nEvent Handler removed."
- }
Add Comment
Please, Sign In to add comment