Listing registered argument-completers

While it is possible to register argument-completers, PowerShell does not provide a way of listing them. This is somewhat frustrating as it makes exploration and finding examples more difficult.

The following script makes extensive use of reflection in .NET to explore classes that are not made publicly available, eventually getting to a property that holds the argument-completers:

$localPipeline = [PowerShell].Assembly.GetType('System.Management.Automation.Runspaces.LocalPipeline')
$getExecutionContextFromTLS = $localPipeline.GetMethod(
'GetExecutionContextFromTLS',
[System.Reflection.BindingFlags]'Static, NonPublic'
)
$internalExecutionContext = $getExecutionContextFromTLS.Invoke(
$null,
[System.Reflection.BindingFlags]'Static, NonPublic',
$null,
$null,
$psculture
)
$customArgumentCompletersProperty = $internalExecutionContext.GetType().GetProperty(
'CustomArgumentCompleters',
[System.Reflection.BindingFlags]'NonPublic, Instance'
)
$customArgumentCompletersProperty.GetGetMethod($true).Invoke(
$internalExecutionContext,
[System.Reflection.BindingFlags]'Instance, NonPublic, GetProperty',
$null,
@(),
$psculture
)

Native argument-completers are held in a different property and will not be shown by the preceding snippet.

A more refined version of the previous snippet, which also supports the retrieval of native argument-completers, is available as a function at https://gist.github.com/indented-automation/26c637fb530c4b168e62c72582534f5b.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.185.147