Last active
October 22, 2024 20:32
-
-
Save PanosGreg/389524c92a92075c645dd7388078fd94 to your computer and use it in GitHub Desktop.
Get the argument completers that are registered in the current session (if any)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-ArgumentCompleter { | |
<# | |
.SYNOPSIS | |
Get the argument completers that are registered in the current session (if any) | |
.EXAMPLE | |
Import-Module PowerShellGet | |
Get-ArgumentCompleter | |
shows the argument completers that are registered from the PowerShellGet module | |
.EXAMPLE | |
Import-Module AWS.Tools.Common | |
Get-ArgumentCompleter -Name Region | |
show the argument completer for the Region parameter that is enforced by default by the AWS module | |
#> | |
[OutputType([System.Collections.Generic.KeyValuePair[string,scriptblock]])] | |
[CmdletBinding()] | |
param ( | |
[string]$Name = '*' | |
) | |
# the goal here is to get this: | |
# $ExecutionContext._invokeCommand._context.CustomArgumentCompleters | |
# But the problem is that the _invokeCommand and _context are Private fields | |
# which are not accessible through Get-Member, so we need to use Reflection. | |
# get the total number for all binding flags, we'll need this to get the fields through reflection | |
$AllFlags = [System.Enum]::GetValues([System.Reflection.BindingFlags]) -join ',' | |
$BindFlags = ([System.Reflection.BindingFlags]$AllFlags).value__ | |
# first we'll get the private field _invokeCommand and its value | |
$IcmField = $ExecutionContext.GetType().GetField('_invokeCommand',$BindFlags) | |
$IcmValue = $IcmField.GetValue($ExecutionContext) | |
# then we'll get the field _context from the _invokeCommand | |
$CtxField = $IcmValue.GetType().GetField('_context',$BindFlags) | |
$CtxValue = $CtxField.GetValue($IcmValue) | |
# now we want the CustomArgumentCompleters property from _context | |
$CAPProp = $CtxValue.GetType().GetProperty('CustomArgumentCompleters',$BindFlags) | |
$CAPValue = $CAPProp.GetValue($CtxValue) | |
# finally show the output | |
$CAPValue.GetEnumerator() | where Key -like $Name | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment