WMI/PowerShell Eventing

by jay007blak at 2012-08-14 20:09:55

I’m not sure if I am in this right category but I would assume this is more of an advanced topic. Here’s the scenario:
[list]Monitor a directory for a creation of a file[/list]
[list]After that program is done writing to the file, read in the file[/list]
[list]process the data from the file and send an html email.[/list]

The problem that I’m experiencing is that the powershell eventing is fast. I have had several instances in testing where the event is registered and my script goes to read from the file but the application writing the file isn’t done writing yet. There’s a few ways I can handle for this but would like your advice as to what is the best practice for this?

Also, I’m trying to make this run as fast as possible, so I’m using runspaces to run these asynchronously. I’m experiencing two issues, 1) i’m consistently running into issues with the number of items in the queue. For example, if i have 10 items in my queue, i’ll only process 9 of them. 2) i am leveraging log4net to handle my logging but i’m having trouble using that in run space. Do you have any samples or recommendations on what the best practice is for leveraging run spaces especially when it comes to loading dlls.
by poshoholic at 2012-08-14 20:48:08
Great questions, and this definitely sounds like an appropriate topic for this forum.

Regarding monitoring for the creation of a file, it sounds like PowerShell Eventing is picking up on the file creation and then your script tries to access the file before the application has closed the file handle. My immediate question there is, is there a way you can tell when the application has finished with the file? For example, if the file is written using a specific executable and you can tell it’s done because that exe is closed, or perhaps some content in the file indicates it is closed, something like that. If not, and if you can only watch for the file and then try to grab it, hoping it’s done, you might want to put in a loop with a short delay and have it retry a specific number of times before it gives up. See this Stack Overflow Q&A for some related discussion: http://stackoverflow.com/questions/3576 … -is-in-use.

Regarding the second question I need more details. It sounds like you’re processing the file data and to speed that up you’re using multiple runspaces with a queue for the items that you are processing. How are you spinning up your runspaces? Is my description accurate for what you’re doing? What sort of trouble are you having with log4net? I think more details would be helpful in tackling the second part of your post.