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!
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.
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.
I donât think that code will meet the needs of OP, may not even work.
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.
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.