@@ -48,29 +48,53 @@ public async Task<List<PSCommandMessage>> Handle(GetCommandParams request, Cance
48
48
PSCommand psCommand = new PSCommand ( ) ;
49
49
50
50
// Executes the following:
51
- // Get-Command -CommandType Function,Cmdlet,ExternalScript | Select-Object -Property Name,ModuleName | Sort-Object -Property Name
51
+ // Get-Command -CommandType Function,Cmdlet,ExternalScript | Sort-Object -Property Name
52
52
psCommand
53
53
. AddCommand ( "Microsoft.PowerShell.Core\\ Get-Command" )
54
54
. AddParameter ( "CommandType" , new [ ] { "Function" , "Cmdlet" , "ExternalScript" } )
55
- . AddCommand ( "Microsoft.PowerShell.Utility\\ Select-Object" )
56
- . AddParameter ( "Property" , new [ ] { "Name" , "ModuleName" } )
57
55
. AddCommand ( "Microsoft.PowerShell.Utility\\ Sort-Object" )
58
56
. AddParameter ( "Property" , "Name" ) ;
59
57
60
- IEnumerable < PSObject > result = await _powerShellContextService . ExecuteCommandAsync < PSObject > ( psCommand ) . ConfigureAwait ( false ) ;
58
+ IEnumerable < CommandInfo > result = await _powerShellContextService . ExecuteCommandAsync < CommandInfo > ( psCommand ) . ConfigureAwait ( false ) ;
61
59
62
60
var commandList = new List < PSCommandMessage > ( ) ;
63
61
if ( result != null )
64
62
{
65
- foreach ( dynamic command in result )
63
+ foreach ( CommandInfo command in result )
66
64
{
65
+ // Some info objects have a quicker way to get the DefaultParameterSet. These
66
+ // are also the most likely to show up so win-win.
67
+ string defaultParameterSet = null ;
68
+ switch ( command )
69
+ {
70
+ case CmdletInfo info :
71
+ defaultParameterSet = info . DefaultParameterSet ;
72
+ break ;
73
+ case FunctionInfo info :
74
+ defaultParameterSet = info . DefaultParameterSet ;
75
+ break ;
76
+ }
77
+
78
+ if ( defaultParameterSet == null )
79
+ {
80
+ // Try to get the default ParameterSet if it isn't streamlined (ExternalScriptInfo for example)
81
+ foreach ( CommandParameterSetInfo parameterSetInfo in command . ParameterSets )
82
+ {
83
+ if ( parameterSetInfo . IsDefault )
84
+ {
85
+ defaultParameterSet = parameterSetInfo . Name ;
86
+ break ;
87
+ }
88
+ }
89
+ }
90
+
67
91
commandList . Add ( new PSCommandMessage
68
92
{
69
93
Name = command . Name ,
70
94
ModuleName = command . ModuleName ,
71
95
Parameters = command . Parameters ,
72
96
ParameterSets = command . ParameterSets ,
73
- DefaultParameterSet = command . DefaultParameterSet
97
+ DefaultParameterSet = defaultParameterSet
74
98
} ) ;
75
99
}
76
100
}
0 commit comments