PoshCode Logo PowerShell Code Repository

Custom Accelerators CTP3 by Joel Bennett 5 years ago
View followups from Joel Bennett | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/762"></script>download | new post

A script module for CTP3 which allows the user to create their own custom type accelerators. Thanks to Oisin Grehan for the discovery.

  1. #requires -version 2.0
  2. ## Custom Accelerators for PowerShell 2 (CTP3)
  3. ####################################################################################################
  4. ## A script module for CTP3 which allows the user to create their own custom type accelerators.
  5. ## Thanks to "Oisin Grehan for the discovery":http://www.nivot.org/2008/12/25/ListOfTypeAcceleratorsForPowerShellCTP3.aspx.
  6. ####################################################################################################
  7. ## Revision History
  8. ## v1.0  - Modularization, by Joel 'Jaykul' Bennett
  9. ####################################################################################################
  10.  
  11. # get a reference to the Type  
  12. $xlr8r = [type]::gettype("System.Management.Automation.TypeAccelerators")  
  13.  
  14. function Add-Accelerator {
  15. <#
  16.    .Synopsis
  17.       Add a type accelerator to the current session
  18.    .Description
  19.       The Add-Accelerator function allows you to add a simple type accelerator (like [regex]) for a longer type (like [System.Text.RegularExpressions.Regex]).
  20.    .Example
  21.       Add-Accelerator list [System.Collections.Generic.List``1]
  22.       $list = New-Object list[string]
  23.      
  24.       Creates an accelerator for the generic List[T] collection type, and then creates a list of strings.
  25.    .Example
  26.       Add-Accelerator list, glist [System.Collections.Generic.List``1]
  27.      
  28.       Creates two accelerators for the generic List[T] collection type.
  29.    .Parameter Accelerator
  30.       The short form accelerator should be just the name you want to use (without square brackets).
  31.    .Parameter Type
  32.       The type you want the accelerator to accelerate.
  33.    .Notes
  34.       When specifying multiple values for a parameter, use commas to separate the values.
  35.       For example, "-Accel string, regex".      
  36.  
  37.       Also see the help for Get-Accelerator and Remove-Accelerator
  38.    .Link
  39.       http://huddledmasses.org/powershell-2-ctp3-custom-accelerators-finally/
  40.      
  41. #>
  42. [CmdletBinding()]
  43. PARAM(
  44.    [Parameter(Position=0)]
  45.    [string[]]$Accelerator
  46. ,
  47.    [Parameter(Position=1)]
  48.    [type]$Type
  49. )
  50. PROCESS {
  51.    # add a user-defined accelerator  
  52.    foreach($a in $Accelerator) { $xlr8r::Add( $a, $Type) }
  53. }
  54. }
  55.  
  56. function Get-Accelerator {
  57. <#
  58.    .Synopsis
  59.       Get one or more type accelerator definitions
  60.    .Description
  61.       The Get-Accelerator function allows you to look up the type accelerators (like [regex]) defined on your system by their short form or by type
  62.    .Example
  63.       Get-Accelerator string
  64.      
  65.       Returns the KeyValue pair for the accelerator definition(s)
  66.    .Example
  67.       Get-Accelerator ps*,wmi*
  68.      
  69.       Returns the KeyValue pair for the matching accelerator definitions
  70.    .Parameter Accelerator
  71.       One or more short form accelerators to search for
  72.       Accepts Wildcards.
  73.    .Parameter Type
  74.       One or more types to search for.
  75.    .Notes
  76.       When specifying multiple values for a parameter, use commas to separate the values.
  77.       For example, "-Accel string, regex".
  78.      
  79.       Also see the help for Add-Accelerator and Remove-Accelerator
  80.    .Link
  81.       http://huddledmasses.org/powershell-2-ctp3-custom-accelerators-finally/
  82. #>
  83. [CmdletBinding(DefaultParameterSetName="ByType")]
  84. PARAM(
  85.    [Parameter(Position=0, ParameterSetName="ByAccelerator", ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
  86.    [Alias("Key")]
  87.    [string[]]$Accelerator
  88. ,
  89.    [Parameter(Position=0, ParameterSetName="ByType", ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
  90.    [Alias("Value")]
  91.    [type[]]$Type
  92. )
  93. PROCESS {
  94.    # add a user-defined accelerator  
  95.    switch($PSCmdlet.ParameterSetName) {
  96.       "ByAccelerator" {
  97.          $xlr8r::get.GetEnumerator() | % {
  98.             foreach($a in $Accelerator) {
  99.                if($_.Key -like $a) { $_ }
  100.             }
  101.          }
  102.          break
  103.       }
  104.       "ByType" {
  105.          if($Type -and $Type.Count) {
  106.             $xlr8r::get.GetEnumerator() | ? { $Type -contains $_.Value }
  107.          }
  108.          else {
  109.             $xlr8r::get.GetEnumerator() | %{ $_ }
  110.          }
  111.          break
  112.       }
  113.    }
  114. }
  115. }
  116.  
  117. function Remove-Accelerator {
  118. <#
  119.    .Synopsis
  120.       Remove a type accelerator from the current session
  121.    .Description
  122.       The Remove-Accelerator function allows you to remove a simple type accelerator (like [regex]) from the current session. You can pass one or more accelerators, and even wildcards, but you should be aware that you can remove even the built-in accelerators.
  123.      
  124.    .Example
  125.       Remove-Accelerator int
  126.       Add-Accelerator int [Int64]
  127.      
  128.       Removes the "int" accelerator for Int32 and adds a new one for Int64. I can't recommend doing this, but it's pretty cool that it works:
  129.      
  130.       So now, "$(([int]3.4).GetType().FullName)" would return "System.Int64"
  131.    .Example
  132.       Get-Accelerator System.Single | Remove-Accelerator
  133.      
  134.       Removes both of the default accelerators for System.Single: [float] and [single]
  135.    .Example
  136.       Get-Accelerator System.Single | Remove-Accelerator -WhatIf
  137.      
  138.       Demonstrates that Remove-Accelerator supports -Confirm and -Whatif. Will Print:
  139.          What if: Removes the alias [float] for type [System.Single]
  140.          What if: Removes the alias [single] for type [System.Single]
  141.    .Parameter Accelerator
  142.       The short form accelerator should be just the name you want to use (without square brackets).
  143.    .Parameter Type
  144.       The type you want the accelerator to accelerate.
  145.    .Notes
  146.       When specifying multiple values for a parameter, use commas to separate the values.
  147.       For example, "-Accel string, regex".
  148.      
  149.       Also see the help for Add-Accelerator and Get-Accelerator
  150.    .Link
  151.       http://huddledmasses.org/powershell-2-ctp3-custom-accelerators-finally/
  152. #>
  153. [CmdletBinding(SupportsShouldProcess=$true)]
  154. PARAM(
  155.    [Parameter(Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
  156.    [Alias("Key")]
  157.    [string[]]$Accelerator
  158. )
  159. PROCESS {
  160.    $xlr8r::get.GetEnumerator() | % {
  161.       foreach($a in $Accelerator) {
  162.          if($_.Key -like $a) {
  163.             if($PSCmdlet.ShouldProcess( "Removes the alias [$($_.Key)] for type [$($_.Value.FullName)]",
  164.                                         "Removing alias [$($_.Key)] for type [$($_.Value.FullName)]?",
  165.                                         "Remove Alias" )) {
  166.                # remove a user-defined accelerator
  167.                $xlr8r::remove($_.Key)  
  168.             }
  169.          }
  170.       }
  171.    }
  172. }
  173. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me