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
148 lines (132 loc) · 5.37 KB
/
Copy pathGet-ModuleCommand.ps1
File metadata and controls
148 lines (132 loc) · 5.37 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
Function Get-ModuleCommand {
[cmdletbinding(DefaultParameterSetName = "name")]
[Alias("gmh")]
[OutputType("ModuleCommand")]
Param(
[Parameter(
Position = 0,
HelpMessage = "The name of an installed/available module",
ValueFromPipelineByPropertyName
)]
[SupportsWildcards()]
[string]$Name,
[Parameter(
HelpMessage = "Command name to search for"
)]
[SupportsWildcards()]
[string]$CommandName,
[switch]$ListAvailable
)
Begin {
Write-Verbose "Starting $($MyInvocation.MyCommand)"
$PSBoundParameters.Add("ErrorAction", "stop")
#region local functions
function getModuleInfo {
[cmdletbinding()]
param(
$module,
$CommandName
)
Write-Verbose "Using version $($module.version)"
$cmds = @()
Write-Verbose "Getting exported functions"
$cmds += $module.ExportedFunctions.keys | Where-Object { $_ -like "$CommandName" } | Get-Command
Write-Verbose "Getting exported cmdlets"
$cmds += $module.ExportedCmdlets.keys | Where-Object { $_ -like "$CommandName" } | 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 -ShowWindow:$false).synopsis.trim()
Type = $cmd.CommandType
Version = $cmd.version
Help = $cmd.HelpUri
ModuleName = $module.name
ModulePath = $module.Path
Compatible = $module.CompatiblePSEditions
PSVersion = $module.PowerShellVersion
}
} #foreach cmd
$out
}
#endregion
}
Process {
If ([string]::IsNullOrEmpty($Name) -and [string]::IsNullOrEmpty($CommandName)) {
if ($ListAvailable) {
$out = Get-Module -ListAvailable | ForEach-Object {
[PSCustomObject]@{
PSTypeName = "ModuleCommand"
Name = $_.name
Alias = ""
Verb = ""
Noun = ""
Synopsis = $_.Description
Type = $null
Version = $_.version
Help = $_.HelpInfoUri
ModuleName = "Available Modules"
ModulePath = $_.Path
Compatible = $_.CompatiblePSEditions
PSVersion = $_.PowerShellVersion
}
}
}
else {
$out = Get-InstalledModule | ForEach-Object {
[PSCustomObject]@{
PSTypeName = "ModuleCommand"
Name = $_.name
Alias = ""
Verb = ""
Noun = ""
Synopsis = $_.Description
Type = $null
Version = $_.version
Help = $_.HelpInfoUri
ModuleName = "Installed Modules"
ModulePath = $_.Path
Compatible = $_.CompatiblePSEditions
PSVersion = $_.PowerShellVersion
}
}
}
}
else {
if ([string]::IsNullOrEmpty($CommandName)) { $CommandName = "*" }
if ([string]::IsNullOrEmpty($Name)) { $Name = "*" }
if ($ListAvailable) {
$out = Get-Module -Name $Name -ListAvailable | ForEach-Object {
#We need to rebind to object (reason unknown!!)
getModuleInfo -module $_ -CommandName $CommandName
}
}
else {
$out = Get-Module -Name $Name | ForEach-Object {
getModuleInfo -module $_ -CommandName $CommandName
}
}
}
#display results sorted by name for better formatting
$out | Sort-Object -Property ModuleName, Name
}
End {
Write-Verbose "Ending $($MyInvocation.MyCommand)"
}
} #close function
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', $_)
}
}