Monitor when folder action stops?

Hello, I am a super super newbie on powershell and I was wondering if I could ask for your help.

I need to monitor a folder in which a file is written every 20 minutes.
I have adapted this simple one:

 $watcher = New-Object System.IO.FileSystemWatcher
>> $watcher.IncludeSubdirectories = $true
>> $watcher.Path = 'X:\'
>> $watcher.EnableRaisingEvents = $true
>> $action =
>> {
>>     $path = $event.SourceEventArgs.FullPath
>>     $changetype = $event.SourceEventArgs.ChangeType
>>     Write-Host "$path was $changetype at $(get-date)"
>>     New-BurntToastNotification -AppLogo Y:\_ELEMENTS\OVERLAYS\LOGO.png -text "BBOX File written"
>> }
>> Register-ObjectEvent $watcher 'Created' -Action $action

Using the BurntToast extension I get a notification when a new file is written.

This is all very well, but what I actually need is the opposite, namely a script that notices when the software has stopped writing files and send me a notification, so I can check why this is happening. Ideally, the software would run in background at startup.

I thought the script might count at startup how many files there are in the folder and check again after 20 minutes, expecting an increment of 1.
If not, it would send a notification. Do you think that would work?

Would you have any suggestions how to achieve this?
Many many thanks!

Hey there and welcome.

There’s probably a few ways to do that, depending on the requirements. It could be a little weird especially when it does break. I think any solution though is going to be a bit clunky though. Maybe others have dealt with this and have created elegant ways.

Without giving it too much thought, you could probably have a scheduled task that utilizes PowerShell to do this. It can import a config file that keeps track of your count. Setting the task to run every 20 minutes. When it kicks off, it could pull the config file, see what the expected count is and compare it to the current count, if they match, nothing happens, if they don’t match you could maybe do a toast notification. At the end of the run you could increase the count in the config file for the next run. You could probably do even that part a number of ways (AKA treat it more like a log and add an entry each time it runs etc.). I haven’t done this myself but might be worth a try. However, once it breaks you’d have to ‘fix’ the count in the config file after you manually intervened probably, though there’s probably even ways around that.

Welcome to the forum. :wave:t3:

If I did not get this wrong I’d run a scheduled task every 5 or 10 minutes checking the age of the newest file in the desired folder. If this age is greater than 20 minutes (or whatever timespan you may consider a “failure”) the script can trigger an action of your choice. :man_shrugging:t3:

Thanks for the suggestions, I’ll give it a try!

Is that a ChatGPT answer? It doesn’t look like it will work.

This looks wrong for starters:

Register-ScheduledTask -Action { CheckFileCount } 
1 Like

I don’t think that code will meet the needs of OP, may not even work.

  1. The way it’s written as a function is only for that session. The Scheduled Task will create a new runspace and won’t know about ‘CheckFileCount’ so it’ll error, unless that command is part of a module that will be automatically imported as part of the session. Also, the function could be initialized at runtime then ran.
  2. The initial file count check is outside the function and is only being checked once. For one the scheduled task isn’t going to have this var and won’t have the value. Also, because OP expects there to be a file written every 20 minutes, he needs to build whatever he does with that in mind. In theory, every ~20 minutes the ‘expected’ value of the child count should be bumped by one. This means he needs to probably initialize a value and increment the expected count before doing the actual check.