-
Notifications
You must be signed in to change notification settings - Fork 0
/
ps.paraminfo.ps1
153 lines (134 loc) · 3.98 KB
/
ps.paraminfo.ps1
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
149
150
151
152
<#
.SYNOPSIS
Displays information of parameters.
.DESCRIPTION
Returns the available parameters and their properties for a specified command or cmdlet.
Result can be limited to a given name of a specific parameter.
.PARAMETER Command
Specifies the name of a command or cmdlet that will have its parameters shown.
.PARAMETER Parameter
Name of a specific parameter that the output will be limited to.
.EXAMPLE
ps.paraminfo Convert-Path
.EXAMPLE
ps.paraminfo -Command 'Get-ChildItem' -Parameter Force
.INPUTS
String (Command Name), (Parameter Name)
Input a string representing the name of a command/cmdlet.
Input a string representing the name of a specific parameter.
.OUTPUTS
Object (Parameter Info Object [PSParameterInfo])
Writes a custom object representing the properties of a parameter to the pipeline.
If the script is the last command in the pipeline, the objects are displayed in the console.
#>
[CmdletBinding()]
[OutputType('PSParameterInfo')]
param(
[Parameter(
Position = 0,
Mandatory,
ValueFromPipeline,
ValueFromPipelineByPropertyName,
HelpMessage = 'Enter the name of a cmdlet'
)]
[ValidateNotNullorEmpty()]
[String] $Command
,
[Parameter(
Position = 1,
HelpMessage = 'Enter the name of a parameter'
)]
[String] $Parameter
)
begin {
function Test-ParamAttribute ($TestObject, $SetName) {
if ($TestObject -is [System.Management.Automation.ParameterAttribute] -and $TestObject.ParameterSetName -eq $SetName) {
return $true
}
else {
return $false
}
}
$CommonParameters = @(
'Verbose',
'Debug',
'ErrorAction',
'ErrorVariable',
'WarningAction',
'WarningVariable',
'OutVariable',
'OutBuffer',
'WhatIf',
'Confirm',
'InformationAction',
'InformationVariable',
'PipelineVariable'
)
}
process {
try {
$GivenCommand = Get-Command -Name $Command -ErrorAction 'Stop'
if ($GivenCommand.CommandType -eq 'Alias') {
$ParamInfo = (Get-Command -Name $GivenCommand.ResolvedCommand -ErrorAction 'Stop').Parameters
}
else {
$ParamInfo = $GivenCommand.Parameters
}
}
catch {
Write-Host "[ps.paraminfo] Error: '$Command' could not be found as a command!" -ForegroundColor 'DarkRed'
exit 1
}
if ($ParamInfo.psbase.Count -gt 0) {
if ($Parameter) {
if ($ParamInfo.ContainsKey($Parameter)) {
$ParamOutp = $Parameter
}
else {
Write-Host "[ps.paraminfo] Error: A parameter called '$Parameter' could not be found!" -ForegroundColor 'DarkRed'
exit 2
}
}
else {
$ParamOutp = $ParamInfo.Keys | Where-Object {$CommonParameters -notcontains $_}
}
$ParamCount = ($ParamOutp | Measure-Object).Count
if ($ParamCount -gt 0) {
$ParamOutp | ForEach-Object {
$Name = $_
$Type = $ParamInfo.Item($Name).ParameterType
$Aliases = $ParamInfo.Item($Name).Aliases -join ', '
$ParamSets = $ParamInfo.Item($Name).ParameterSets.Keys
$IsDynamic = $ParamInfo.Item($Name).IsDynamic
foreach ($Set in $ParamSets) {
$Attributes = $ParamInfo.Item($Name).Attributes | Where-Object {Test-ParamAttribute -TestObject $_ -SetName $Set}
if ($Attributes.Position -ge 0) {
$PositionValue = $Attributes.position
}
else {
$PositionValue = 'None (Named Parameter)'
}
$Outp = [pscustomobject]@{
Name = $Name
Type = $Type
Aliases = $Aliases
Position = $PositionValue
Mandatory = $Attributes.Mandatory
IsDynamic = $IsDynamic
ParameterSet = $Attributes.ParameterSetName -replace '__', ''
ValueFromPipeline = $Attributes.ValueFromPipeline
ValueFromPipelineByPropertyName = $Attributes.ValueFromPipelineByPropertyName
}
Write-Output $Outp
}
}
}
}
else {
Write-Host "[ps.paraminfo] Warning: No defined parameters found for the command '$Command'." -ForegroundColor 'DarkYellow'
exit 3
}
}
end {
# Nothing to do here.
}