forked from jdhitsolutions/PSScriptTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-Parameter.ps1
117 lines (101 loc) · 4.18 KB
/
Get-Parameter.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
Function Get-ParameterInfo {
[cmdletbinding()]
[Outputtype("PSCustomobject")]
[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 {
$data = (Get-Command -Name $command -errorAction "Stop").parameters
}
Catch {
Write-Warning "Failed to find command $command"
}
#keep going if parameters were found
if ($data.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]@{
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