
Next time I’ll walk you through how I use that data. Register-ScheduledJob -Name "DailyWatcher" -ScriptBlock $action -Trigger $triggerĪt this point I have a PowerShell scheduled job running essentially in the background, monitoring folders for file changes and logging to a CSV file. Get-EventSubscriber | Out-String | Write-Host -ForegroundColor yellow Manages the logs generated by the solution.

Creates a File Watcher and register it for monitoring only or with file copy. with MD5 hash verification, copying only unique files. $params.Action | Out-String | Write-Host -ForegroundColor Cyan FileWatcher.ps1 is composed of the following features: Logging compatible with CMTrace. $params.MessageData | Out-String | Write-Host -ForegroundColor cyan MessageData = "A file was created or changed in $Path" #possible events are Changed,Deleted,Created

$sbtext = "c:\scripts\LogBackupEntry.ps1 -event `$event -CSVPath $log" You remove the event using Remove-Event to prevent it from being fired again by PowerShell Keep going until stopped Start-FlleSystemWatcher.ps1 The result is a PowerShell script that's used as follows: Start-FileSystemWatcher. #the Action scriptblock to be run when an event fires Write-Host "Creating a FileSystemWatcher for $Path" -ForegroundColor green #specify the directory for the CSV log files #get the directory name from the list of paths #trim leading and trailing white spaces in each path Throw "Failed to find c:\scripts\myBackupPaths.txt" if (Test-Path c:\scripts\myBackupPaths.txt) In my scheduled job action I parse this file to get a list of paths. #comment out paths with a # symbol at the beginning of the line To begin, I created a text file with the paths to monitor. Each event needs to be able to log the file changes so that I know what needs to be backed up incrementally. For backup purposes this means creating a FileSystemWatcher to monitor each directory and a corresponding event subscription. My scheduled job needs an action to perform. Using a script file makes this an easy process. While it is possible to edit an existing job, I find it easier to simply remove it and recreate it.

Doing so provides me a documentation trail and makes it easier to recreate the job. While it is simple enough to create a scheduled job from the console, I tend to save the command in a script file. You get the ease of using the task scheduler with the benefit or running PowerShell code.
#Powershell filewatcher trial#
ManageEngine ADManager Plus - Download Free Trial Creating the PowerShell Scheduled JobĪ PowerShell Scheduled Job takes advantage of the Windows Task Scheduler to run a PowerShell scriptblock or script. Initializes a new instance of the FileSystemWatcher class. Provides data for the directory events: Changed, Created, Deleted. Specifies changes to watch for in a file or folder. Manage and Report Active Directory, Exchange and Microsoft 365 with FileSystemWatcher Class (System.IO) Listens to the file system change notifications and raises events when a directory, or file in a directory, changes.
