forked from jdhitsolutions/PSScriptTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-Parameter.ps1
More file actions
131 lines (116 loc) · 4.79 KB
/
Get-Parameter.ps1
File metadata and controls
131 lines (116 loc) · 4.79 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
Function Get-ParameterInfo {
[cmdletbinding()]
[Outputtype("PSParameterInfo")]
[alias("gpi")]
Param(
[Parameter(
Position = 0,
Mandatory,
ValueFromPipeline,
ValueFromPipelineByPropertyName,
HelpMessage = "Enter a cmdlet name"
)]
[ValidateNotNullorEmpty()]
[Alias("name")]
[string]$Command,
[string]$Parameter
)
Begin {
Write-Verbose "Starting $($myinvocation.MyCommand)"
#define the set of common parameters to exclude
$common = @("Verbose",
"Debug",
"ErrorAction",
"ErrorVariable",
"WarningAction",
"WarningVariable",
"OutVariable",
"OutBuffer",
"WhatIf",
"Confirm",
"InformationAction",
"InformationVariable",
"PipelineVariable"
)
} #begin
Process {
Write-Verbose "Processing $command for parameter information"
Try {
#need to account that the command might be an alias (Issue #101). 1/21/2021 JDH
$cmd = Get-Command -name $command -ErrorAction Stop
if ($cmd.CommandType -eq 'alias') {
Write-Verbose "Resolving the alias $Command to $($cmd.ResolvedCommand)"
$data = (Get-Command -Name $cmd.ResolvedCommand -ErrorAction "Stop").parameters
}
else {
$data = $cmd.parameters
}
}
Catch {
Write-Warning "Failed to find command $command"
}
# keep going if parameters were found
# Explicitly calling base, to prevent .count from being shadowed
#
if ($data.psbase.Count -gt 0) {
#$data is a hash table
if ($Parameter) {
Write-Verbose "Getting parameter $Parameter"
if ($data.ContainsKey( $Parameter)) {
$params = $Parameter
}
else {
Throw "Can't find a parameter called $Parameter."
}
}
else {
Write-Verbose 'Getting parameter all non-common parameters'
$params = $data.keys | Where-Object { $common -notcontains $_ }
}
$count = ($params | Measure-Object).count
#only keep going if non-common parameters were found
Write-Verbose "Found $count non-common parameters for $command"
if ($count -gt 0) {
#get information from each parameter
$params | ForEach-Object {
$name = $_
Write-Verbose "Analyzing $name"
$type = $data.item($name).ParameterType
$aliases = $data.item($name).Aliases -join ','
$sets = $data.item($name).ParameterSets.Keys
$IsDynamic = $data.item($name).IsDynamic
foreach ($set in $sets) {
#retrieve parameter attribute class
$attributes = $data.item($name).Attributes | Where-Object { $_ -is [system.management.automation.parameterAttribute] -AND $_.ParameterSetName -eq $set }
#a parameter could have different positions in different property sets
if ($attributes.position -ge 0) {
$position = $attributes.position
}
else {
$position = 'Named'
}
#write a custom object to the pipeline
[PSCustomObject]@{
PSTypeName = 'PSParameterInfo'
Name = $name
Aliases = $aliases
Mandatory = $attributes.mandatory
Position = $position
ValueFromPipeline = $attributes.ValueFromPipeline
ValueFromPipelineByPropertyName = $attributes.ValueFromPipelineByPropertyName
Type = $type
IsDynamic = $IsDynamic
ParameterSet = $attributes.ParameterSetName
}
} #foreach set
} #foreach object
} #if $count
} #if $data
else {
Write-Warning "$command has no defined parameters"
}
} #process
End {
Write-Verbose "Ending $($myinvocation.MyCommand)"
} #end
} #end function