Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- .SYNOPSIS
- This filewatcher is specifically to watch for new files in a directory
- below where the project root of the project script exists AND logs its creation (for management) in an adjacent force-written subdirectory
- .DESCRIPTION
- for ChangeType choose from option [Created/Deleted/Changed]
- Arguments:
- - watcherDirectory: name a string (will be created if not exists) for a subdirectory to watch
- - directoryChangeType [Created/Deleted/Changed]
- - watcherNameDescription: single unspaced string describing purpose or name of file/subfolder watching context
- - filenameFilter (for watcher): type of file
- .EXAMPLE
- ./scripts/filewatcher-helper.ps1 -WatcherDirectory -DirectoryChangeType Created
- .NOTES
- This script will:
- - watch the given directory (WatcherDirectory) from argument
- - clearing any existing of the same name
- - register a new Powershell FileSystemWatcher
- .LINK
- #>
- [CmdletBinding()]
- Param(
- [Parameter(Mandatory=$True,Position=1)]
- [string]$WatcherDirectory,
- [Parameter(Mandatory=$True,Position=2)]
- [string]$DirectoryChangeType,
- [Parameter(Mandatory=$True,Position=3)]
- [string]$WatcherNameDescription,
- [Parameter(Mandatory=$True,Position=4)]
- [string]$FilenameFilter
- )
- function Write-Log {
- Param(
- $Message,
- $Path
- )
- function TS {Get-Date -Format 'yyyy-MMM-dd hh:mm:ss'}
- "[$(TS)]$Message" | Tee-Object -FilePath $Path -Append | Write-Verbose
- }
- $loggingOutputDirectory = $($(Split-path -Path $PSScriptRoot -Parent) + '\logging')
- New-Item -EA SilentlyContinue -ItemType Directory -Force -Path $loggingOutputDirectory
- $loggingOutput = $loggingOutputDirectory + '\' + $WatcherNameDescription + '_' + $DirectoryChangeType + '_output_log.txt'
- Write-Log -Message $("Logging subscriber creation initialization to:" + $loggingOutputDirectory) -Path $loggingOutput
- $fullWatcherDirectory = $($(Split-path -Path $PSScriptRoot -Parent) + '\' + $WatcherDirectory)
- Write-Log -Message $("Initializing subscriber creation for directory:" + $fullWatcherDirectory) -Path $loggingOutput
- New-Item -EA SilentlyContinue -ItemType Directory -Force -Path $fullWatcherDirectory | Out-Null
- $fileSystemWatcher = New-Object IO.FileSystemWatcher -EA SilentlyContinue $fullWatcherDirectory, $filter -Property @{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}
- $subscriberName = $null
- $subscriberName = $($WatcherDirectory + '_' + $WatcherNameDescription + '_' + $DirectoryChangeType)
- Write-Log -Message $("Unregistering previous version of Event type " + $subscriberName + " on " + $fullWatcherDirectory) -Path $loggingOutput
- Unregister-Event $subscriberName # clear event subscription in case of change/name collision
- if ($null -eq $(Get-EventSubscriber $subscriberName -EA SilentlyContinue | Format-List -Property SourceIdentifier))
- {
- Write-Log -Message $("Registering " + $subscriberName + " on " + $fullWatcherDirectory) -Path $loggingOutput
- Write-Output $("Registering " + $subscriberName + " on " + $fullWatcherDirectory)
- Register-ObjectEvent $fileSystemWatcher $DirectoryChangeType -SourceIdentifier $subscriberName -Action {
- $name = $Event.SourceEventArgs.Name
- $changeType = $Event.SourceEventArgs.ChangeType
- $timeStamp = $Event.TimeGenerated
- Write-Host "The file '$name' was $DirectoryChangeType at $timeStamp" -fore red
- Out-File -FilePath $loggingOutput -Append -InputObject "The file '$name' was $DirectoryChangeType at $timeStamp"}
- }
Add Comment
Please, Sign In to add comment