Skip to content

Commit 35cb0d8

Browse files
Adds Get-PSMDArgumentCompleter
1 parent a8353b1 commit 35cb0d8

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
function Get-PSMDArgumentCompleter
2+
{
3+
<#
4+
.SYNOPSIS
5+
Gets the registered argument completers.
6+
7+
.DESCRIPTION
8+
This function can be used to serach the argument completers registered using either the Register-ArgumentCompleter command or created using the ArgumentCompleter attribute.
9+
10+
.PARAMETER CommandName
11+
Filter the results to a specific command. Wildcards are supported.
12+
13+
.PARAMETER ParameterName
14+
Filter results to a specific parameter name. Wildcards are supported.
15+
16+
.EXAMPLE
17+
PS C:\> Get-PSMDArgumentCompleter
18+
19+
Get all argument completers in use in the current PowerShell session.
20+
21+
#>
22+
[CmdletBinding()]
23+
Param (
24+
[Parameter(Position = 1, ValueFromPipeline = $true, ValueFromPipelineByPropertyName)]
25+
[Alias('Name')]
26+
[String]
27+
$CommandName = '*',
28+
29+
[String]
30+
$ParameterName = '*'
31+
)
32+
33+
begin
34+
{
35+
$internalExecutionContext = [PSFramework.Utility.UtilityHost]::GetExecutionContextFromTLS()
36+
$customArgumentCompletersProperty = $internalExecutionContext.GetType().GetProperty(
37+
'CustomArgumentCompleters',
38+
[System.Reflection.BindingFlags]'NonPublic, Instance'
39+
)
40+
$customArgumentCompleters = $customArgumentCompletersProperty.GetGetMethod($true).Invoke(
41+
$internalExecutionContext,
42+
[System.Reflection.BindingFlags]'Instance, NonPublic, GetProperty',
43+
$null,
44+
@(),
45+
$psculture
46+
)
47+
}
48+
process
49+
{
50+
foreach ($argumentCompleter in $customArgumentCompleters.Keys)
51+
{
52+
$name, $parameter = $argumentCompleter -split ':'
53+
54+
if ($name -like $CommandName)
55+
{
56+
if ($parameter -like $ParameterName)
57+
{
58+
New-Object PSObject -Property @{
59+
CommandName = $name
60+
ParameterName = $parameter
61+
Definition = $customArgumentCompleters[$argumentCompleter]
62+
}
63+
}
64+
}
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)