System Watcher

Hi All

I’m trying to create a script that watches a file for changes. I have this

$FileSystemWatcher = New-Object System.IO.FileSystemWatcher
$FileSystemWatcher.path = "C:\FlowWatcher"
$FileSystemWatcher.Filter = "Flow.txt"
$FileSystemWatcher.EnableRaisingEvents = $true

Register-ObjectEvent $FileSystemWatcher "Changed" -Action {
$content = get-content C:\FlowWatcher\Flow.txt |select-object -last 1
powershell.exe $content
}

But the output I get is as follows

Id              Name  PSJobTypeName    State       HasMoreData    Location    Command 
--              ----  -------------    -----       -----------    --------    ------- 
1 7b28e775-0aa  ...                    NotStarted  False                      ...

I’m just not sure why is hasn’t started

No reason to do this from scratch…
Leverage this and tweak as needed…

Powershell FileSystemWatcher This script uses the .net FileSystemWatcher class to subscribe to 'create', 'change' and 'delete' events on files or folders in the NTFS filesystem.It can be used to monitor a folder or folders, and can be modified to perform any action upon the triggering of these events. https://gallery.technet.microsoft.com/scriptcenter/Powershell-FileSystemWatche-dfd7084b

As for why your is not starting…
You have a few things in what you have, that is not needed.

I agree with Postanote, don’t reinvent the wheel. I personally found that I preferred the resource at MCPMag.com, it has a nice little function and a template to role out the rules. The template needed some modifications for my purposes, but it allowed me to achieve my objective with little additional efforts.

 

https://mcpmag.com/articles/2015/09/24/changes-to-a-folder-using-powershell.aspx