1
1
Function Get-ModuleCommand {
2
2
[cmdletbinding (DefaultParameterSetName = " name" )]
3
- [Alias (" gmc " )]
3
+ [Alias (" gmh " )]
4
4
[OutputType (" ModuleCommand" )]
5
5
6
6
Param (
7
7
[Parameter (
8
8
Position = 0 ,
9
- Mandatory ,
10
- HelpMessage = " The name of an installed module" ,
11
- ParameterSetName = " name" ,
9
+ HelpMessage = " The name of an installed/available module" ,
12
10
ValueFromPipelineByPropertyName
13
11
)]
14
- [ValidateNotNullOrEmpty ()]
12
+ [SupportsWildcards ()]
15
13
[string ]$Name ,
16
14
17
15
[Parameter (
18
- Mandatory ,
19
- HelpMessage = " The fully qualified name of an installed module" ,
20
- ParameterSetName = " fqdn"
16
+ HelpMessage = " Command name to search for"
21
17
)]
22
- [ValidateNotNullOrEmpty ()]
23
- [Microsoft.PowerShell.Commands.ModuleSpecification ] $FullyQualifiedName ,
18
+ [SupportsWildcards ()]
19
+ [string ] $CommandName ,
24
20
25
21
[switch ]$ListAvailable
26
22
)
27
23
28
24
Begin {
29
25
Write-Verbose " Starting $ ( $MyInvocation.MyCommand ) "
30
26
$PSBoundParameters.Add (" ErrorAction" , " stop" )
27
+
28
+ # region local functions
29
+ function getModuleInfo {
30
+ [cmdletbinding ()]
31
+ param (
32
+ $module ,
33
+ $CommandName
34
+ )
35
+ Write-Verbose " Using version $ ( $module.version ) "
36
+
37
+ $cmds = @ ()
38
+ Write-Verbose " Getting exported functions"
39
+ $cmds += $module.Exportedfunctions.keys | Where-Object { $_ -like " $CommandName " } | Get-Command
40
+ Write-Verbose " Getting exported cmdlets"
41
+ $cmds += $module.Exportedcmdlets.keys | Where-Object { $_ -like " $CommandName " } | Get-Command
42
+
43
+ Write-Verbose " Found $ ( $cmds.count ) functions and/or cmdlets"
44
+
45
+ $out = foreach ($cmd in $cmds ) {
46
+ Write-Verbose " Processing $ ( $cmd.name ) "
47
+ # get aliases, ignoring errors for those commands without one
48
+ $alias = (Get-Alias - Definition $cmd.Name - ErrorAction SilentlyContinue).name
49
+
50
+ # it is assumed you have updated help
51
+ [PSCustomObject ]@ {
52
+ PSTypeName = " ModuleCommand"
53
+ Name = $cmd.name
54
+ Alias = $alias
55
+ Verb = $cmd.verb
56
+ Noun = $cmd.noun
57
+ Synopsis = (Get-Help $cmd.name - ShowWindow:$false ).synopsis.trim()
58
+ Type = $cmd.CommandType
59
+ Version = $cmd.version
60
+ Help = $cmd.HelpUri
61
+ ModuleName = $module.name
62
+ ModulePath = $module.Path
63
+ Compatible = $module.CompatiblePSEditions
64
+ PSVersion = $module.PowerShellVersion
65
+ }
66
+ } # foreach cmd
67
+
68
+ $out
69
+ }
70
+ # endregion
31
71
}
32
72
33
73
Process {
34
- # getting commands directly from the module because for some unknown reason,
35
- # probably scope related, when using Get-Command alone to list commands in the module,
36
- # it includes private functions
37
-
38
- Try {
39
- Write-Verbose " Listing all matching modules"
40
- Write-Verbose " Using bound parameters"
41
- $PSBoundParameters | Out-String | Write-Verbose
42
-
43
- # get newest version of the module
44
- $mod = Get-Module @PSBoundParameters | Select-Object - First 1
45
- Write-Verbose " Found $ ( $mod.count ) modules"
46
- if (-not $mod ) {
47
- Throw " Failed to find a matching module. Try again using the -ListAvailable parameter."
48
- }
49
- # get prerelease from private data
50
- if ($mod.PrivateData -and $mod.PrivateData.ContainsKey (' PSData' ) -and $mod.PrivateData.PSData.ContainsKey (' PreRelease' )) {
51
- $prerelease = $mod.PrivateData.PSData.PreRelease
74
+ If ([string ]::IsNullOrEmpty($Name ) -and [string ]::IsNullOrEmpty($CommandName )) {
75
+ if ($ListAvailable ) {
76
+ $out = Get-Module - ListAvailable | ForEach-Object {
77
+ [PSCustomObject ]@ {
78
+ PSTypeName = " ModuleCommand"
79
+ Name = $_.name
80
+ Alias = " "
81
+ Verb = " "
82
+ Noun = " "
83
+ Synopsis = $_.Description
84
+ Type = $null
85
+ Version = $_.version
86
+ Help = $_.HelpInfoUri
87
+ ModuleName = " Available Modules"
88
+ ModulePath = $_.Path
89
+ Compatible = $_.CompatiblePSEditions
90
+ PSVersion = $_.PowerShellVersion
91
+ }
92
+ }
52
93
}
53
94
else {
54
- $prerelease = $null
95
+ $out = Get-InstalledModule | ForEach-Object {
96
+ [PSCustomObject ]@ {
97
+ PSTypeName = " ModuleCommand"
98
+ Name = $_.name
99
+ Alias = " "
100
+ Verb = " "
101
+ Noun = " "
102
+ Synopsis = $_.Description
103
+ Type = $null
104
+ Version = $_.version
105
+ Help = $_.HelpInfoUri
106
+ ModuleName = " Installed Modules"
107
+ ModulePath = $_.Path
108
+ Compatible = $_.CompatiblePSEditions
109
+ PSVersion = $_.PowerShellVersion
110
+ }
111
+ }
55
112
}
56
- } # try
57
- Catch {
58
- Write-Verbose " This is weird. There was an exception!"
59
- Throw $_
60
- # Bail out
61
- return
62
113
}
63
-
64
- if ($PSCmdlet.ParameterSetName -eq ' name' -AND $mod.count -gt 1 ) {
65
- # make sure to get the latest version
66
- Write-Verbose " Getting the latest version of $ ( $mod [0 ].name) "
67
- $mod = $mod | Sort-Object - Property Version - Descending | Select-Object - First 1
68
- }
69
-
70
- Write-Verbose " Using version $ ( $mod.version ) "
71
-
72
- $cmds = @ ()
73
- Write-Verbose " Getting exported functions"
74
- $cmds += $mod.ExportedFunctions.keys | Get-Command
75
- Write-Verbose " Getting exported cmdlets"
76
- $cmds += $mod.ExportedCmdlets.keys | Get-Command
77
-
78
- Write-Verbose " Found $ ( $cmds.count ) functions and/or cmdlets"
79
-
80
- $out = foreach ($cmd in $cmds ) {
81
- Write-Verbose " Processing $ ( $cmd.name ) "
82
- # get aliases, ignoring errors for those commands without one
83
- $alias = (Get-Alias - Definition $cmd.Name - ErrorAction SilentlyContinue).name
84
-
85
- # it is assumed you have updated help
86
- [PSCustomObject ]@ {
87
- PSTypeName = " ModuleCommand"
88
- Name = $cmd.name
89
- Alias = $alias
90
- Verb = $cmd.verb
91
- Noun = $cmd.noun
92
- Synopsis = (Get-Help $cmd.name ).synopsis.trim()
93
- Type = $cmd.CommandType
94
- Version = $cmd.version
95
- Help = $cmd.HelpUri
96
- ModuleName = $mod.name
97
- Compatible = $mod.CompatiblePSEditions
98
- PSVersion = $mod.PowerShellVersion
114
+ else {
115
+ if ([string ]::IsNullOrEmpty($CommandName )) { $CommandName = " *" }
116
+ if ([string ]::IsNullOrEmpty($Name )) { $Name = " *" }
117
+
118
+ if ($ListAvailable ) {
119
+ $out = Get-Module - Name $Name - ListAvailable | ForEach-Object {
120
+ # We need to rebind to object (reason unknown!!)
121
+ getModuleInfo - module $_ - CommandName $CommandName
122
+ }
99
123
}
100
- } # foreach cmd
124
+ else {
125
+ $out = Get-Module - Name $Name | ForEach-Object {
126
+ getModuleInfo - module $_ - CommandName $CommandName
127
+ }
128
+ }
129
+ }
101
130
102
- # display results sorted by name for better formatting
103
- $out | Sort-Object - Property Name
104
- }
105
- End {
106
- Write-Verbose " Ending $ ( $MyInvocation.MyCommand ) "
107
- }
131
+ # display results sorted by name for better formatting
132
+ $out | Sort-Object - Property ModuleName , Name
133
+ }
134
+ End {
135
+ Write-Verbose " Ending $ ( $MyInvocation.MyCommand ) "
136
+ }
108
137
109
138
} # close function
110
139
111
140
Register-ArgumentCompleter - CommandName Get-ModuleCommand - ParameterName Name - ScriptBlock {
112
141
param ($commandName , $parameterName , $wordToComplete , $commandAst , $fakeBoundParameter )
113
142
114
- (Get-Module - Name " $WordToComplete *" ).name |
143
+ (Get-Module - Name " $wordtoComplete *" ).name |
115
144
ForEach-Object {
145
+ # completion text,listitem text,result type,Tooltip
116
146
[System.Management.Automation.CompletionResult ]::new($_ , $_ , ' ParameterValue' , $_ )
117
147
}
118
148
}
0 commit comments