11Function Get-ModuleCommand {
22 [cmdletbinding (DefaultParameterSetName = " name" )]
3- [Alias (" gmc " )]
3+ [Alias (" gmh " )]
44 [OutputType (" ModuleCommand" )]
55
66 Param (
77 [Parameter (
88 Position = 0 ,
9- Mandatory ,
10- HelpMessage = " The name of an installed module" ,
11- ParameterSetName = " name" ,
9+ HelpMessage = " The name of an installed/available module" ,
1210 ValueFromPipelineByPropertyName
1311 )]
14- [ValidateNotNullOrEmpty ()]
12+ [SupportsWildcards ()]
1513 [string ]$Name ,
1614
1715 [Parameter (
18- Mandatory ,
19- HelpMessage = " The fully qualified name of an installed module" ,
20- ParameterSetName = " fqdn"
16+ HelpMessage = " Command name to search for"
2117 )]
22- [ValidateNotNullOrEmpty ()]
23- [Microsoft.PowerShell.Commands.ModuleSpecification ] $FullyQualifiedName ,
18+ [SupportsWildcards ()]
19+ [string ] $CommandName ,
2420
2521 [switch ]$ListAvailable
2622 )
2723
2824 Begin {
2925 Write-Verbose " Starting $ ( $MyInvocation.MyCommand ) "
3026 $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
3171 }
3272
3373 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+ }
5293 }
5394 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+ }
55112 }
56- } # try
57- Catch {
58- Write-Verbose " This is weird. There was an exception!"
59- Throw $_
60- # Bail out
61- return
62113 }
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+ }
99123 }
100- } # foreach cmd
124+ else {
125+ $out = Get-Module - Name $Name | ForEach-Object {
126+ getModuleInfo - module $_ - CommandName $CommandName
127+ }
128+ }
129+ }
101130
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+ }
108137
109138} # close function
110139
111140Register-ArgumentCompleter - CommandName Get-ModuleCommand - ParameterName Name - ScriptBlock {
112141 param ($commandName , $parameterName , $wordToComplete , $commandAst , $fakeBoundParameter )
113142
114- (Get-Module - Name " $WordToComplete *" ).name |
143+ (Get-Module - Name " $wordtoComplete *" ).name |
115144 ForEach-Object {
145+ # completion text,listitem text,result type,Tooltip
116146 [System.Management.Automation.CompletionResult ]::new($_ , $_ , ' ParameterValue' , $_ )
117147 }
118148}
0 commit comments