Guest User

Untitled

a guest
Jul 16th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. <#
  2. .SYNOPSIS
  3. This filewatcher is specifically to watch for new files in a directory
  4. below where the project root of the project script exists AND logs its creation (for management) in an adjacent force-written subdirectory
  5.  
  6. .DESCRIPTION
  7. for ChangeType choose from option [Created/Deleted/Changed]
  8.  
  9. Arguments:
  10. - watcherDirectory: name a string (will be created if not exists) for a subdirectory to watch
  11. - directoryChangeType [Created/Deleted/Changed]
  12. - watcherNameDescription: single unspaced string describing purpose or name of file/subfolder watching context
  13. - filenameFilter (for watcher): type of file
  14.  
  15. .EXAMPLE
  16. ./scripts/filewatcher-helper.ps1 -WatcherDirectory -DirectoryChangeType Created
  17.  
  18.  
  19. .NOTES
  20. This script will:
  21. - watch the given directory (WatcherDirectory) from argument
  22. - clearing any existing of the same name
  23. - register a new Powershell FileSystemWatcher
  24.  
  25. .LINK
  26.  
  27. #>
  28.  
  29. [CmdletBinding()]
  30. Param(
  31. [Parameter(Mandatory=$True,Position=1)]
  32. [string]$WatcherDirectory,
  33. [Parameter(Mandatory=$True,Position=2)]
  34. [string]$DirectoryChangeType,
  35. [Parameter(Mandatory=$True,Position=3)]
  36. [string]$WatcherNameDescription,
  37. [Parameter(Mandatory=$True,Position=4)]
  38. [string]$FilenameFilter
  39. )
  40.  
  41. function Write-Log {
  42. Param(
  43. $Message,
  44. $Path
  45. )
  46. function TS {Get-Date -Format 'yyyy-MMM-dd hh:mm:ss'}
  47. "[$(TS)]$Message" | Tee-Object -FilePath $Path -Append | Write-Verbose
  48. }
  49.  
  50.  
  51. $loggingOutputDirectory = $($(Split-path -Path $PSScriptRoot -Parent) + '\logging')
  52. New-Item -EA SilentlyContinue -ItemType Directory -Force -Path $loggingOutputDirectory
  53. $loggingOutput = $loggingOutputDirectory + '\' + $WatcherNameDescription + '_' + $DirectoryChangeType + '_output_log.txt'
  54. Write-Log -Message $("Logging subscriber creation initialization to:" + $loggingOutputDirectory) -Path $loggingOutput
  55.  
  56. $fullWatcherDirectory = $($(Split-path -Path $PSScriptRoot -Parent) + '\' + $WatcherDirectory)
  57. Write-Log -Message $("Initializing subscriber creation for directory:" + $fullWatcherDirectory) -Path $loggingOutput
  58. New-Item -EA SilentlyContinue -ItemType Directory -Force -Path $fullWatcherDirectory | Out-Null
  59.  
  60. $fileSystemWatcher = New-Object IO.FileSystemWatcher -EA SilentlyContinue $fullWatcherDirectory, $filter -Property @{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}
  61. $subscriberName = $null
  62. $subscriberName = $($WatcherDirectory + '_' + $WatcherNameDescription + '_' + $DirectoryChangeType)
  63. Write-Log -Message $("Unregistering previous version of Event type " + $subscriberName + " on " + $fullWatcherDirectory) -Path $loggingOutput
  64. Unregister-Event $subscriberName # clear event subscription in case of change/name collision
  65. if ($null -eq $(Get-EventSubscriber $subscriberName -EA SilentlyContinue | Format-List -Property SourceIdentifier))
  66. {
  67. Write-Log -Message $("Registering " + $subscriberName + " on " + $fullWatcherDirectory) -Path $loggingOutput
  68. Write-Output $("Registering " + $subscriberName + " on " + $fullWatcherDirectory)
  69. Register-ObjectEvent $fileSystemWatcher $DirectoryChangeType -SourceIdentifier $subscriberName -Action {
  70. $name = $Event.SourceEventArgs.Name
  71. $changeType = $Event.SourceEventArgs.ChangeType
  72. $timeStamp = $Event.TimeGenerated
  73. Write-Host "The file '$name' was $DirectoryChangeType at $timeStamp" -fore red
  74. Out-File -FilePath $loggingOutput -Append -InputObject "The file '$name' was $DirectoryChangeType at $timeStamp"}
  75. }
Add Comment
Please, Sign In to add comment