问题
I have a PowerShell snapin, and I would like to run a bit of initialization code (hooking some AppDomain events) each time my snapin is loaded (i.e. once for each powershell.exe process that is started). How can this be accomplished?
回答1:
One approach, and this is probably a bit rube-goldberg in nature, is to hook into a provider's startup code. This assumes that in your snapin configuration you have declared a default drive to initialize. It just so happens that in PSCX we use a provider to store all of our settings and global variables to minimize impact on the user's global session space. The initialization of this settings drive provided a convenient place for us to hook in other initialization code.
Now if we weren't creating a provider, I assume there is some other way to initialize code early but I don't know off the top of my head.
来源:https://stackoverflow.com/questions/3551771/how-can-i-run-initialization-code-each-time-my-snap-in-is-loaded