Monday, January 21, 2013

Viewing PowerShell Function Definitions in the ISE–Part 2

Last Friday, I published an article about a neat script I’d come across. At the end, I mentioned it would be nice to extend this to enable me to highlight a function name I’m calling in code and either use a menu item or short-cut keys to get the definition up.

Well, I spend several hours today playing and have done just that. Yesterday, I polished up the code a bit and published it over on my PowerShell Scripts blog. At home, I’ve incorporated it into my personal ISE module and it now loads every time I start up the ISE.

The script, as extended, defines a function (Open-FunctionInISE), then adds it to an ISE menu. The function takes input in three ways: you can specify the function name to open, you can highlight the function name in an open edit window and use either short-cut keys or the ISE menu. And if those fail, the function prompts for a function name. I’ve also taken the liberty of renaming the function to be, hopefully, closer to best practice naming!

In converting the function, it was interesting working with a single function that could take input from the pipeline, from the command line of via selected ISE text (and which prompts for a function name if all those fail). In doing the conversion, changed the parameter from a string array to string, which felt nearer to the way I see the script being used. I might play around a bit with the script and add that back in. To simplify such changes, I’ve left the function to operate in the Process block, but I have pulled out the enumeration of objects within each process block.

Another thing the extended script did was to add the function to a menu item in the ISE. That turns out to be easy. Before the script adds the function to the add-on menu, it checks to see if it’s already been added (as might happen if you decide to edit the function and start running it!). Of course, if you already have the menu item created, to make changes in the function itself, just comment out the menu addition code. Or use the function to bring the function definition into the ISE, make your changes and re-run just that script (remembering to persist any changes you decide to make!).

Thanks to the original author of this script (Cookie.Monster) for giving me something fun to do on a Sunday afternoon. Comments most welcome.

Technorati Tags:

1 comment:

Guy said...

Thank you, it worked for me.