Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

PowerTab Tip : Invoke Custom functions for Tabexpansion

PowerTab ( see PowerTab Overview Page  )uses a database that gives you also the ability to add  your own custom aditions to the TabExpansion functionality of PowerTab

The possibitiy to add custum additions to PowerTab I have shown a couple of times before for example here:  PowerTab 0.72 PowerShell TabExtension - Part 3 The DataBase and utilities  and its usage with for example enums  here : PowerShell : Extract the Possible Enum values from Last Error and add to TabCompletion

But I think there is a great chance not many people did find out about the power of the Invoke functionality allready,

as you see in the screendump below showing the list of shortcut characters the default character for invoke is & , With the custom examples only some datetime shortcuts are show : 

PoSH> get-tabExpansion * custom | where {$_.type -eq 'invoke'}

Filter Text Type
------ ---- ----
date get-date -date invoke
now get-date invoke
time get-date -time invoke

In this post I show some more anvanced possibilities the first example is adding a script to list the snapins registered but only the ones not loaded yet

Listing Snapins not allready loaded to select from

Oisin made an example script for that on his blog : A simple Snap-In picker script , I simplified his function by removing all formating so it only did output the clear snapin names :

Functionget-snapinlist {
  
$snaps = get-pssnapin -r
  
$loaded = get-pssnapin | % {$_.name}
  
$notloaded = @()
  
foreach ($snap in $snaps) {
         
if (-not($loaded -contains $snap.name)) {
               
$snap.name
         }
    }
}
add-tabExpansion 's' 'get-snapinlist' invoke

 As all that was needed for this example was a script to provide the output to pick from as clear text, as we will see that after we add the function to PowerTab its right it your fingertips, only a Tab away.

PoSH> get-snapinlist
InvokeApartmentSample
LerchSnapIn
NetCmdlets
PowerGadgets
Pscx
PSEventing

PoSH> Add-PSSnapin PSEventing

PoSH> get-snapinlist
InvokeApartmentSample
LerchSnapIn
NetCmdlets
PowerGadgets
Pscx
PoSH>

The Invoke custom additions allow you to execute a script when Tabexpansion is called and generates a list from the output.

 So afher that I could thow it into the add it to the PowerTab invoke database and use it like this, where the listing and selection is handled by PowerTab :

Get access to a nested Prompt in the middle of a line

The Second example, I linked to it on my overview page already but I think worth mentioning again here,  is a very creative use of the  invoke functionalty to get a nexted Prompt in the middle of a line by using Tab, so you can do some checking and then on exit complete the line, it is not using PowerTab Invoke functionality to provide a list but to provide a trigger for a nested shell,

you can find this  nice usage example of customizing PowerTab and the use of custom invoke commands on Björn Österman 's blog here : Using PowerTab for NestedPrompt

Very Cool idea , thanks Björn !

 

I think this are 2 good examples of using PowerTab's invoke functionality to the max, And to end with a little teaser, in PowerShell Plus , unfortunate still in private beta,  I have also instant access to this functionality in my editor !!

How PoSH can the world be ;-)

Enjoy,

Greetings /\/\o\/\/

Published Tuesday, August 21, 2007 7:16 PM by MoW

Comments

No Comments
Anonymous comments are disabled