Sonntag, 14. März 2010

ISE and PowerShell Modules

The last days searched a bit the current links about PowerShell modules.

There are the two new links by
Bruce Payette

http://dotnetslackers.com/articles/net/Converting-a-PowerShell-Script-into-a-Module.aspx
http://dotnetslackers.com/articles/net/Converting-a-PowerShell-Script-into-a-Module-part2.aspx

and in PowerShellPack there are lots of examples.

When you have a script module, that is one with a .psm1 file, it is easy to create a personal variant of it, be just copying it to a new folder within PSmodulePath and rename its
.psm1 file to correspond to the new foldername.

There might be .psd1 and .ps1xml files which require similar and further adaptions. The modules I'm working on don't have them.

But being modules that extend ISE, they use the addons menu and the current defacto standard to build such menus is the use of the Add-IseMenu from ISEpack.

The original version from James Brundage has some shortcomings, but I succeeded in writing a compatible extension, which you find in ISE-Cream. The main advantage, is that it only warns when shortcut keys are used and just builds the menu ignoring them.

That makes it easy to copy a module, rename the top menu name in one of them and load them side by side.

At first the menus look good, but when you try them, they seem to use the function from the last imported module.
When 2 modules export the same function name, the name is rebound to the function from the last imported module. Usually menus call their functions just by name and do not hard weir the modulename into the call.
In the end both menus call the functions from the last imported module.

Of course you can take the risk and edit the modul into all function calls, but that is a lot of work and seems error phrone.
Ignorant to the possibility to prefix the calls with the module name, I added a module parameter to the ICE-Cream version of Add-IseMenu, so that I have to set it at a single point.

Whenever I see the following code:
import-module IsePack          

I replace it with
if (! (test-path function:Add-IseMenu ) ) {import-module IsePack }          
Did I mention, that in the ISE-Cream version, you can predefine the order of the entries?


Bernd

PS:
I noticed, that undersores in modulnames will not be displayed. Better don't use them





Keine Kommentare:

Kommentar veröffentlichen