Building PowerShell auto-completion using TabExpansionPlusPlus – Part 3 – When?


This is a series of 3 blog posts on one of the best kept secrets in PowerShell: “Argument Completion”:

  • Building PowerShell auto-completion using TabExpansionPlusPlus – Part 1 – What?
  • Building PowerShell auto-completion using TabExpansionPlusPlus – Part 2 – How?
  • Building PowerShell auto-completion using TabExpansionPlusPlus – Part 3 – When?

    Now that we have run through how to create argument completers in Part 2, I wanted to cover when to create argument completers and more specific for which cmdlet parameters?

    Which parameters should I focus on when building “Argument Completers”?

    These are the criteria I have used when writing argument completers:

  • Frequently used parameters example: -Name 
  • Common parameters that are available on many cmdlets – example: VMName, VMSnapshot, VMSwitch (see below)
  • Parameters that deal with “cryptic” data – VMId, GUIDs, PNPIds, LUN paths, etc…

    When you spend some time building argument completers, you really want to find the “hot spot” and provide completion for most used parameters.

    Here’s some piece of code which provides an overview of the most occurring parameters within a specified module.

    function Get-ArgumentCompleterCandidate 
    {
        [Cmdletbinding()]
        param(
            [Parameter(Mandatory=$true)]
            [Alias('ModuleName')]
            [string]$Module,
            [switch]$Descending
        )     $Cmdlets = Get-Command -Module $Module
        Write-Verbose -Message "Module: $Module"
        Write-Verbose -Message "Cmdlets found: $($Cmdlets.Count)"     $Parameters = $Cmdlets.Parameters.Keys
        Write-Verbose -Message "Parameters found: $($Parameters.Count)"
        Write-Verbose -Message "Unique parameters found: $(($Parameters | Sort-Object -Unique).Count)"     $CandidateParameters = $Cmdlets.Parameters.Values |
                Where-Object {$_.ParameterType.Name -ne 'SwitchParameter'} |
                Where-Object {$_.ParameterType.Name -ne 'ActionPreference'} |
                Where-Object {$_.Name -notin ('PipelineVariable','OutVariable','OutBuffer','ErrorVariable','WarningVariable','InformationVariable')} |
                Select-Object @{Name='ParameterName';Expression={$_.Name}},ParameterType
       
        Write-Verbose -Message "Relevant parameters found: $($CandidateParameters.Count)"
        Write-Verbose -Message "Relevant unique parameters found: $(($CandidateParameters | Sort-Object -Property ParameterName -Unique).Count)"
       
        Write-Verbose -Message "Priority parameters for argument completion"
        $CandidateParameters | Group-Object -Property ParameterName -NoElement | Sort-Object -Property Count -Descending:$Descending
    }

    From the CLI

    PS C:\> Get-ArgumentCompleterCandidate -Module FailoverClusters
    PS C:\> Get-ArgumentCompleterCandidate -Module GroupPolicy
    PS C:\> Get-ArgumentCompleterCandidate -Module Hyper-V –Desc –Verbose

    image

    It’s a simple way of identifying the parameters you could/should focus on…

    I will cover in some additional blog posts other advanced topics of “Argument Completers”, but that’s for the near future.

    Hope this helps…

  • Advertisements

    3 thoughts on “Building PowerShell auto-completion using TabExpansionPlusPlus – Part 3 – When?

    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out / Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out / Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out / Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out / Change )

    Connecting to %s