forked from jdhitsolutions/PSScriptTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-ModuleCommand.ps1
More file actions
92 lines (76 loc) · 3.3 KB
/
Copy pathGet-ModuleCommand.ps1
File metadata and controls
92 lines (76 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#display a summary of tools in a specific module
Function Get-ModuleCommand {
[cmdletbinding(DefaultParameterSetName = "name")]
[Alias("gmc")]
[OutputType("ModuleCommand")]
Param(
[Parameter(Position = 0, Mandatory, HelpMessage = "The name of an installed module", ParameterSetName = "name")]
[ValidateNotNullorEmpty()]
[string]$Name,
[Parameter(Mandatory, HelpMessage = "The fully qualified name of an installed module", ParameterSetName = "fqdn")]
[ValidateNotNullorEmpty()]
[Microsoft.PowerShell.Commands.ModuleSpecification]$FullyQualifiedName,
[switch]$ListAvailable
)
Write-Verbose "Starting $($myinvocation.mycommand)"
#getting commands directly from the module because for some unknown reason,
#probably scope related, when using Get-Command alone to list commands in the module,
#it includes private functions
$psboundParameters.Add("ErrorAction", "stop")
Try {
Write-Verbose "Listing all matching modules"
Write-Verbose "Using bound parameters"
$PSBoundParameters | Out-String | Write-Verbose
$mod = Get-Module @PSBoundParameters
Write-Verbose "Found $($mod.count) modules"
if (-not $mod) {
Throw "Failed to find a matching module. Try again using the -ListAvailable parameter."
}
}
Catch {
write-verbose "This is weird. There was an exception!"
Throw $_
#Bail out
return
}
if ($pscmdlet.parameterSetName -eq 'name' -AND $mod.count -gt 1) {
#make sure to get the latest version
Write-Verbose "Getting the latest version of $($mod[0].name)"
$mod = $mod | Sort-Object -property Version -Descending | Select-Object -first 1
}
Write-Verbose "Using version $($mod.version)"
$cmds = @()
Write-Verbose "Getting exported functions"
$cmds += $mod.Exportedfunctions.keys | Get-Command
Write-Verbose "Getting exported cmdlets"
$cmds += $mod.Exportedcmdlets.keys | Get-Command
Write-Verbose "Found $($cmds.count) functions and/or cmdlets"
$out = foreach ($cmd in $cmds) {
Write-Verbose "Processing $($cmd.name)"
#get aliases, ignoring errors for those commands without one
$alias = (Get-Alias -definition $cmd.Name -erroraction silentlycontinue).name
#it is assumed you have updated help
[pscustomobject]@{
PSTypeName = "ModuleCommand"
Name = $cmd.name
Alias = $alias
Verb = $cmd.verb
Noun = $cmd.noun
Synopsis = (Get-Help $cmd.name).synopsis.trim()
Type = $cmd.CommandType
Version = $cmd.version
ModuleName = $mod.name
}
}
#display results sorted by name for better formatting
$out | Sort-Object -Property Name
Write-Verbose "Ending $($myinvocation.mycommand)"
}
Register-ArgumentCompleter -CommandName Get-ModuleCommand -ParameterName Name -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
(Get-Module -Name "$wordtoComplete*").name |
foreach-object {
# completion text,listitem text,result type,Tooltip
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}