Forked from indented-automation/Get-ArgumentCompleter.ps1
Last active
April 6, 2021 05:12
-
-
Save disco0/05729d0db292e292e5c5684896812691 to your computer and use it in GitHub Desktop.
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 custom argument completers registered in the current session. | |
.DESCRIPTION | |
Get custom argument completers registered in the current session. | |
By default Get-ArgumentCompleter lists all of the completers registered in the session. | |
.EXAMPLE | |
Get-ArgumentCompleter | |
Get all of the argument completers for PowerShell commands in the current session. | |
.EXAMPLE | |
Get-ArgumentCompleter -CommandName Invoke-ScriptAnalyzer | |
Get all of the argument completers used by the Invoke-ScriptAnalyzer command. | |
.EXAMPLE | |
Get-ArgumentCompleter -Native | |
Get all of the argument completers for native commands in the current session. | |
#> | |
[CmdletBinding(DefaultParameterSetName = 'PSCommand')] | |
param ( | |
# Filter results by command name. | |
[String]$CommandName = '*', | |
# Filter results by parameter name. | |
[Parameter(ParameterSetName = 'PSCommand')] | |
[String]$ParameterName = '*', | |
# Get argument completers for native commands. | |
[Parameter(ParameterSetName = 'Native')] | |
[Switch]$Native | |
) | |
$getExecutionContextFromTLS = [PowerShell].Assembly.GetType('System.Management.Automation.Runspaces.LocalPipeline').GetMethod( | |
'GetExecutionContextFromTLS', | |
[System.Reflection.BindingFlags]'Static,NonPublic' | |
) | |
$internalExecutionContext = $getExecutionContextFromTLS.Invoke( | |
$null, | |
[System.Reflection.BindingFlags]'Static, NonPublic', | |
$null, | |
$null, | |
$psculture | |
) | |
if ($Native) { | |
$argumentCompletersProperty = $internalExecutionContext.GetType().GetProperty( | |
'NativeArgumentCompleters', | |
[System.Reflection.BindingFlags]'NonPublic, Instance' | |
) | |
} else { | |
$argumentCompletersProperty = $internalExecutionContext.GetType().GetProperty( | |
'CustomArgumentCompleters', | |
[System.Reflection.BindingFlags]'NonPublic, Instance' | |
) | |
} | |
$argumentCompleters = $argumentCompletersProperty.GetGetMethod($true).Invoke( | |
$internalExecutionContext, | |
[System.Reflection.BindingFlags]'Instance, NonPublic, GetProperty', | |
$null, | |
@(), | |
$psculture | |
) | |
foreach ($completer in $argumentCompleters.Keys) { | |
$name, $parameter = $completer -split ':' | |
if ($name -like $CommandName -and $parameter -like $ParameterName) { | |
[PSCustomObject]@{ | |
CommandName = $name | |
ParameterName = $parameter | |
Definition = $argumentCompleters[$completer] | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment