r/PowerShell•
SMFX

Events with PowerShell Automation

Script Sharing

There was a recent thread here working with FileSystemWatcher and moving files. Event driven coding is a surprisingly little used feature that is actually really powerful tool to use in automation, so I was inspired to post a little bit about it. It's been written about over and over again, but I still commonly see people doing scheduled tasks or just infinite loops to deal with things that could be triggered, so I figured I'd take a stab at it in an overly wordy blog post:

Events with PowerShell Automation | SmallFoxx Tech Blog

Posted Oct 2, 2022, 7:14 AM UTC

8514
cqdx73•

Thanks. Looks like a good read. Def gonna check this out.

5
TheBSGamer•

I actually utilized the Timer event class recently to create my printer automation agent to indicate a "check in" time and it works great. Having those tools at your disposal really beefs up the automation tasks and makes it even more powerful in an actual meaningful way.

5
av6502•

Can you please elaborate further on the problem and how this improved the situation. I'm having trouble understanding actual use cases.

Thank you.

1
TheBSGamer•

This article gives you a general idea of what it can do. This one focuses specifically on timers. It's a somewhat dated article but still applies in pwsh/5.1.

1
ZomaX6•

From my experience SystemFileWatcher is a good solution for local files and you can even have one script for multiple source locations. However for remote source it's not so reliable mainly because it depends on the network that can get some short interruptions that will cause registered object to stop working without notice. Because of that for remote locations I am using standard gci filter in a loop with [System.IO.FileAccess]::ReadWrite to check if file is available for a move job.

5
SMFXOP•

I can see why you would need to do that. The idea behind Events is they're happenings within the OS, but the nature of network files is they're in another OS, so it is likely there may not be a true trigger.

If you could register a watcher on the local system, it would be more reliable, but that is not always possible. However, depending on what type of system it's on, there may be another Event handler or API that system makes available that is more reliable. For instance, Storage Accounts in Azure have the ability to trigger events and alerts off of a blob placed in a container.

The idea is to consider seeing if you can do a trigger rather than always polling. However, in the end, consider separating out the polling from the processing. This can really help with multi-tasking/threading, as you could use a low overhead polling process to then drop something in a queue for a heavier process to be triggered.

It's really hard to do something 'wrong' if you're getting the results you're looking for; always good to have more tools at your disposal and glad you have something working well for you.

1
gohbender•

I wrote a similar post in stack overflow for someone who was trying to monitor a folder. Ended up suggesting events as well. Feel free to take anything from my post for your blog if it's useful.

https://superuser.com/questions/497205/can-robocopy-monitor-files-on-a-time-increment-of-less-than-one-minute/891766#891766

4
Hoggs•

Event driven coding is a surprisingly little used feature that is actually really powerful tool to use in automation

My guess is that most people writing powershell didn't come from a programming background, so never studied to learn common application architectures. Would love to see this kinda thing done more often!

2
SMFXOP•

You and me both, u/hoggs! I'll keep trying to spread the word!!

2
[deleted]•

[deleted]

1
jgmachine•

I’ve wondered about doing something like this with a system I work with running on SQL. I looked into trying to have event driven triggers, but I was worried about what happens when the event is missed for whatever reason. In that situation I would still need to occasionally check all the data and do a “sync”, so I decided to not go down the path of an event driven trigger. It’s something I’m always open to revisiting though.

1
user01401•

Nice write up!

Have you had any memory leak issues?

I remember reading someone having that issue before.

1
Hoggs•

If your script is large and complex and you're worried about memory management - one possible option is to run a smaller script as a "watchdog" script waiting for events, and have it fork a process (run another script) when it needs to do work. This way you're assured the long-running script is lightweight, and your Do-Work script is completely flushed out of memory when it's done.

3
user01401•

Thanks, that is a good idea. I'll definitely have to give this a try and get off of the static task scheduler schedules.

1
AAAdamKK•

So I'd love to replace a scheduled job I have running on a server with this but how do I get around the session being closed either by another user or because of a reboot etc?

Edit: Nevermind it doesn't work with a locally synced sharepoint site anyway, at least when files on-demand is enabled.

1