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

PowerTab 0.72 PowerShell TabExtension - Part 3 The DataBase and utilities

This is part 3 of a series, about PowerTab an extended PowerShell TabExpansion Function

for more information see former posts : 

PowerTab Overview Page ( Downloads and Links )

  • PowerTab 0.72 PowerShell TabExtension - Part 2 The basics
  • PowerTab 0.72 PowerShell TabExtension - Part 1 Installation
  • this part is about The Tabcompletion DataBase, the Utility functions, and custom additions

    In the former post  PowerTab 0.72 PowerShell TabExtension - Part 2 The basics , we did look at changes to the profile, and did see that the load-TabExtension function was called in the profile, and while finding the content of the function we used the TabCompletion on aliases and did see that it also is a quick way to find out the real name of an alias, just type it and hit tab.

    This is a great way to check if an alias name is allready in use also :

     Type it, hit tab and if it does not expand it is not in use, and you can use it.

    After finding the content of the Load-TabExpansion function, we made a sitestep by implementing a GUI tabcompletion for the about_ help and extended the tabExpansion.ps1 function by adding it.

    In this part, I take of from there and go on exploring the DataBase, we did see that it was called TabExpansion.xml and was stored in the Profile Directory.

    Also we will take a look at the utility functions for that :

    I use another way to get to the content of the Load-Tabcomletion Function here, using the tabcompletion on () blocks we also explored in Part 2.

    Type as :

     ( gcm[tab] lo%[tab] ).de[tab]

    ii[tab] (copy rest from console )

     $ta[tab].ta[tab]

    [arrow up].c[tab]

    try also $TabDB.TabExpansion.[tab][tab][tab]  No beginletter used, hence all properties and methods an object has are shown, very handy as this is a Great shortcut for using get-member.

    also note the use of % in the first example to start the tabcompletion before we are at the - ,  another feature of the extend PowerTab tabcompletion, 

    So first we are using tabcompletion on a function here, and also we do not have to type the full Verb and Dash first.

     

     

    PoSH> ( Get-Command Load-tabExpansion ).Definition                                                                      
    $global:dsTabExpansion = new-object data.dataset                                                                        
      [void]$global:dsTabExpansion.ReadXml("$(Split-Path $profile)\tabExpansion.xml")                                       
                                                                                                                            
                                                                                                                            
    PoSH> "$(Split-Path $profile)\tabExpansion.xml"                                                                         
    C:\Users\MvanOrsouw\Documents\WindowsPowerShell\tabExpansion.xml                                                        
    PoSH> Invoke-Item "$(Split-Path $profile)\tabExpansion.xml"                                                             
    PoSH> [xml] ( Get-Content "$(Split-Path $profile)\tabExpansion.xml" )                                                   
                                                                                                                            
    xml                                                         TabExpansion                                                
    ---                                                         ------------                                                
                                                                TabExpansion                                                
                                                                                                                            
                                                                                                                            
    PoSH> $TabDB = [xml] ( Get-Content "$(Split-Path $profile)\tabExpansion.xml" )                                          
    PoSH> $TabDB.TabExpansion                                                                                               
                                                                                                                            
    Custom                                  Types                                   Wmi                                     
    ------                                  -----                                   ---                                     
    {Custom, Custom, Custom, Custom...}     {System.Object, System.ICloneable, S... {__NotifyStatus, __ExtendedStatus, W... 
                                                                                                                            
                                                                                                                            
    PoSH> $TabDB.TabExpansion.Custom[0]                                                                                     
                                                                                                                            
    Filter                                  Text                                    Type                                    
    ------                                  ----                                    ----                                    
    A:                                      A:                                      Function                                
                                                                                                                            
                                                                                                                            
    PoSH> $TabDB.TabExpansion.Custom[-1]                                                                                    
                                                                                                                            
    Filter                                  Text                                    Type                                    
    ------                                  ----                                    ----                                    
    sps                                     Stop-Process                            Alias                                   
                                                                                                                            
                                                                                                                            
    PoSH> $tdb.TabExpansion.Wmi | Where {$_.name -eq 'win32_share'}                                                         
    PoSH> $tdb.TabExpansion.Wmi                                                                                             
    PoSH> $TabDB.TabExpansion.Wmi | Where {$_.name -eq 'win32_share'}                                                       
                                                                                                                            
    name                                                        Description                                                 
    ----                                                        -----------                                                 
    Win32_Share                                                 The Win32_Share class represents a shared resource on a ... 
                                                                                                                            
                                                                                                                            
    PoSH>                                               
    

     

     Note that as it is XML we can get it as an object in PowerShell and then use tabcompletion on that again, also the tabcompletion on the XML object is multilevel tabcompleted (another addition to the TabExtention function ), So we can "Browse" into the XML file using TAB .

     But as you could see in the Load-TabExpansion Function this XML file also gets loaded into a DataSet, so we can use that also the same way as you can see in the next example , and also some utility functions for this are provided, you see here some of the other Tab functions, they are all very short and simple, be sure to take a look at the other functions also, you can easy tweak them to for example to support more databases and switch them on the fly (just create more different XML files etc. :

    Use Tabcompletion like this (to find out about all Tab functions :

    $ds[Tab].ta[Tab]

     add-t[Tab]

    ref%[Tab]

    c_ref%[Tab]

    c_%tab[Tab]

    Note the use of % here for partial completion and the C_ for Custom GUI completion (another % to also wildcard the beginning (but I did find a bug here the result selected gets not completed I do not know why yet )

    (* This Posts readers question !! *, why does the selected command does not gets expanded on the console (it does on c_ref%[tab] ) , if you know please leave a comment

    and here the examples (Tabcompletion examples to try (only work after excersise below)  :

    tqb[tab]

    |%[tab]

    |?[tab]

    cc[tab]

     

    PoSH> $dsTabExpansion.Tables['Custom'].select("Type ='Custom'")                                                         
                                                                                                                            
    Filter                                  Text                                    Type                                    
    ------                                  ----                                    ----                                    
    tqb                                     "The Quick Brown Fox Jumps over the ... Custom                                  
    |%                                      | Foreach-Object {$_}                   Custom                                  
    |?                                      | Where {$_}                            Custom                                  
                                                                                                                            
                                                                                                                            
    PoSH> add-tabExpansion 'cc' '@{Name='';Expression={$_}}'                                                                
                                                                                                                            
    Filter                                  Text                                    Type                                    
    ------                                  ----                                    ----                                    
    cc                                      @{Name=';Expression={$_}}               Custom                                  
                                                                                                                            
                                                                                                                            
    PoSH> pwd | select drive                                                                                                
                                                                                                                            
    Drive                                                                                                                   
    -----                                                                                                                   
    C                                                                                                                       
                                                                                                                            
                                                                                                                            
    PoSH> pwd | select @{Name='Drive';Expression={"$($_.drive):"}}                                                          
                                                                                                                            
    Drive                                                                                                                   
    -----                                                                                                                   
    C:                                                                                                                      
                                                                                                                            
                                                                                                                            
    PoSH> ( Get-Command add-tabExpansion ).Definition                                                                       
    param([string]$filter,[string]$Text,[string]$type = 'Custom') $global:dsTabExpansion.Tables['Custom'].Rows.Add($filter, 
    $text,$type)                                                                                                            
                                                                                                                            
                                                                                                                            
    PoSH> ( Get-Command Save-tabExpansion ).Definition                                                                      
    $global:dsTabExpansion.WriteXml("$(Split-Path $profile)\TabExpansion.xml")                                              
                                                                                                                            
                                                                                                                            
    PoSH> Save-tabExpansion                                                                                                 
    PoSH> ( Get-Command Refresh-tabExpansion ).Definition                                                                   
    Refresh-TabExpansionAlias                                                                                               
      Refresh-TabExpansionCmdlet                                                                                            
      Refresh-TabExpansionFunction                                                                                          
      Refresh-TabExpansionTypes                                                                                             
      Refresh-TabExpansionWmi                                                                                               
                                                                                                                            
                                                                                                                            
    PoSH> ( Get-Command Refresh-tabExpansionFunction ).Definition                                                           
    $global:dsTabExpansion.Tables['Custom'].select("Type = 'Function'") |% {                                                
            $global:dsTabExpansion.Tables['Custom'].rows.remove($_)                                                         
        }                                                                                                                   
        Get-Command -commandType "Function"|% {add-tabExpansion $_.name $_.name $_.CommandType}                             
                                                                                                                            
      # You can also do this only for the session, just do not save it                                                                                                                       
                               
    PoSH> add-TabExpansion 'gb' 'get-buffer | Out-File C:\PowerShell\PowerTab\Examples\'                                    
                                                                                                                            
    Filter                                  Text                                    Type                                    
    ------                                  ----                                    ----                                    
    gb                                      get-buffer | Out-File C:\PowerShell\... Custom                                  
                                                                                                                            
                                                                                                                            
    PoSH> get-buffer | Out-File C:\PowerShell\PowerTab\Examples\tabCustom.txt                                               
    

    You can see that add-Tabcompletion is very handy for keeping things you type much handy, (for example the get-buffer that I use a lot for creating the consoledumps I use in this series, as you can see in the examples above,  and you can choose if you save them for later use or not (you can add more then one completion for one alias !!). 

    Note that the refresh functions leave the Custom additions intact.

     also that you need to refresh functions and aliases after adding new ones and that you need to save the database to make the changes permanent, but as said the utility functions are basic and easy to adapt.

    more in next post

    Enjoy,

    Greetings /\/\o\/\/

    Published Friday, January 19, 2007 5:59 PM by admin

    Comments

    # The PowerShell Guy : PowerTab

    Sunday, January 21, 2007 12:54 PM by The PowerShell Guy : PowerTab

    # re: PowerTab 0.72 PowerShell TabExtension - Part 3 The DataBase and utilities

    I added tab expansion and now powershellAnalyzer will not load ... any thoughts greatly appreciated.  i am a novice, trying to learn by example

    Saturday, January 27, 2007 9:42 PM by EdT

    # PowerShell TabExtension - Part 6 Customizing PowerTab 0.9

    I mentioned in last post about the new version of PowerTab PowerShell TabExtension - Part 5 PowerTab

    Wednesday, April 18, 2007 5:50 PM by The PowerShell Guy

    # PowerTab Tip : Invoke Custom functions for Tabexpansion

    PowerTab uses a database that gives you also the ability to add your own custom aditions to the TabExpansion

    Tuesday, August 21, 2007 7:16 PM by The PowerShell Guy
    Anonymous comments are disabled