Tracking Changes to a Folder¶
Name | Description |
---|---|
Changed | Occurs when a file or directory in the specified Path is changed. |
Created | Occurs when a file or directory in the specified Path is created. |
Deleted | Occurs when a file or directory in the specified Path is deleted. |
Error | Occurs when the instance of FileSystemWatcher is unable to continue monitoring changes or when the internal buffer overflows. |
Renamed | Occurs when a file or directory in the specified Path is renamed. |
Name | Description |
---|---|
Attributes | The attributes of the file or folder. |
CreationTime | The time the file or folder was created. |
DirectoryName | The name of the directory. |
FileName | The name of the file. |
LastAccess | The date the file or folder was last opened. |
LastWrite | The date the file or folder last had anything written to it. |
Security | The security settings of the file or folder. |
Size | The size of the file or folder. |
Função auxiliar¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | Function Start-FileSystemWatcher { [cmdletbinding()] Param ( [parameter()] [string]$Path, [parameter()] [ValidateSet('Changed','Created','Deleted','Renamed')] [string[]]$EventName, [parameter()] [string]$Filter, [parameter()] [System.IO.NotifyFilters]$NotifyFilter, [parameter()] [switch]$Recurse, [parameter()] [scriptblock]$Action ) #region Build FileSystemWatcher $FileSystemWatcher = New-Object -TypeName System.IO.FileSystemWatcher If (-NOT $PSBoundParameters.ContainsKey('Path')) { $Path = $PWD } $FileSystemWatcher.Path = $Path If ($PSBoundParameters.ContainsKey('Filter')) { $FileSystemWatcher.Filter = $Filter } If ($PSBoundParameters.ContainsKey('NotifyFilter')) { $FileSystemWatcher.NotifyFilter = $NotifyFilter } If ($PSBoundParameters.ContainsKey('Recurse')) { $FileSystemWatcher.IncludeSubdirectories = $True } If (-NOT $PSBoundParameters.ContainsKey('EventName')) { $EventName = 'Changed', 'Created', 'Deleted', 'Renamed' } If (-NOT $PSBoundParameters.ContainsKey('Action')) { $Action = { Switch ($Event.SourceEventArgs.ChangeType) { 'Renamed' { $Object = '{0} was {1} to {2} at {3}' -f $Event.SourceArgs[-1].OldFullPath, $Event.SourceEventArgs.ChangeType, $Event.SourceArgs[-1].FullPath, $Event.TimeGenerated } Default { $Object = '{0} was {1} at {2}' -f $Event.SourceEventArgs.FullPath, $Event.SourceEventArgs.ChangeType, $Event.TimeGenerated } } $WriteHostParams = @{ ForegroundColor = 'Green' BackgroundColor = 'Black' Object = $Object } Write-Host @WriteHostParams } } #endregion Build FileSystemWatcher #region Initiate Jobs for FileSystemWatcher $ObjectEventParams = @{ InputObject = $FileSystemWatcher Action = $Action } ForEach ($Item in $EventName) { $ObjectEventParams.EventName = $Item $ObjectEventParams.SourceIdentifier = "File.$($Item)" Write-Verbose "Starting watcher for Event: $($Item)" $Null = Register-ObjectEvent @ObjectEventParams } #endregion Initiate Jobs for FileSystemWatcher } |
Exemplo de utilização¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | $FileSystemWatcherParams = @{ Path = 'C:\Users\Proxb\Desktop\Dropbox' Recurse = $True NotifyFilter = 'FileName' Verbose = $True Action = { $Item = Get-Item -Path $Event.SourceEventArgs.FullPath $WriteHostParams = @{ ForegroundColor = 'Green' BackgroundColor = 'Black' } Switch -regex ($Item.Extension) { '\.(ps1|psm1|psd1)' { $WriteHostParams.Object = "Processing PowerShell file: $($Item.Name)" } '\.(docx|doc)' { $WriteHostParams.Object = "Processing Word document: $($Item.Name)" } '\.(xlsx|xls)' { $WriteHostParams.Object = "Processing Excel spreadsheet: $($Item.Name)" } '\.csv' { $WriteHostParams.Object = "Processing CSV spreadsheet: $($Item.Name)" } '\.xml' { $WriteHostParams.Object = "Processing XML document: $($Item.Name)" } '\.exe' { $WriteHostParams.Object = "Processing Executable: $($Item.Name)" } '\.onepkg' { $WriteHostParams.Object = "Processing OneNote package: $($Item.Name)" } '\.lnk' { $WriteHostParams.Object = "Processing Link: $($Item.Name)" } '\.cer|\.pfx' { $WriteHostParams.Object = "Processing Certificate File: $($Item.Name)" } Default { $WriteHostParams.Object = "Processing File: $($Item.Name)" } } $Item | Remove-Item Write-Host @WriteHostParams } } @('Created') | ForEach-Object { $FileSystemWatcherParams.EventName = $_ Start-FileSystemWatcher @FileSystemWatcherParams } |