It catches events and then "replays" them to event subscribers. The answer is that PowerShell has its own type of event handling. But how does that even work in PowerShell? Normally, you add a few handlers to a FileSystemWatcher and then keep running until shut down. The first thing I realized was that events in PowerShell aren't as simple as in. Both struck me as more complicated than writing the server itself in PowerShell. I could have used the same approach (using C#) but that would either have required the server to start a PowerShell process for every script to run, or to be a PowerShell host itself. This time I wanted the ability to run PowerShell scripts. So after setting up the FileSystemWatcher, all there's left to do is to implement the event handlers. Each event gets an argument specifying which file was afffected the Renamed event also receives the previous name of the file. (Those are also the names of the events). The heavy lifting is done by the FileSystemWatcher, which lives up to its name: it watches a (single) folder, with or without it subfolders, and raises event when files are Created, Changed, Deleted or Renamed. Once again, it's not rocket science: the main challenge is in error handling and logging. I've written servers like that before, but always in C#, using the System.IO.FileSystemWatcher class. Basically, it monitors a folder and when files appear in it, it takes some action on them, in this case: execute them. The other day I had an idea: a PowerShell Script Server. Using a FileSystemWatcher from PowerShell Using constants for table and column names.Using a FileSystemWatcher from PowerShell.Comparing folder security using PowerShell.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |