-
Notifications
You must be signed in to change notification settings - Fork 29
/
Get-CommandParameters.ps1
78 lines (70 loc) · 1.94 KB
/
Get-CommandParameters.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
<#
.SYNOPSIS
Returns the parameters of the specified cmdlet.
.FUNCTIONALITY
Command
.LINK
Stop-ThrowError.ps1
.LINK
Get-Command
.EXAMPLE
Get-CommandParameters.ps1 Write-Verbose
Name : Message
ParameterType : System.String
ParameterSets : {[__AllParameterSets, System.Management.Automation.ParameterSetMetadata]}
IsDynamic : False
Aliases : {Msg}
Attributes : {, System.Management.Automation.AllowEmptyStringAttribute, System.Management.Automation.AliasAttribute}
SwitchParameter : False
.EXAMPLE
Get-CommandParameters.ps1 Out-Default -NamesOnly
Transcript
InputObject
#>
#Requires -Version 3
[CmdletBinding()]
[OutputType([string])]
Param(
# The name of a cmdlet.
[Parameter(Position=0,Mandatory=$true)][string] $CommandName,
# The name of a parameter set defined by the cmdlet.
[string] $ParameterSet,
# Return only the parameter names (otherwise)
[switch] $NamesOnly,
# Includes common parameters such as -Verbose and -WhatIf.
[switch] $IncludeCommon
)
Begin
{
[string[]] $excludeParams =
if($IncludeCommon) {@()}
else
{
[string[]][System.Management.Automation.PSCmdlet]::CommonParameters +
[string[]][System.Management.Automation.PSCmdlet]::OptionalCommonParameters
}
}
Process
{
$cmd = Get-Command -Name $CommandName -ErrorAction Ignore
if(!$cmd) {Stop-ThrowError.ps1 "Cmdlet not found: $CommandName" -ParameterName CommandName}
if($ParameterSet)
{
$params = $cmd.ParameterSets |
Where-Object Name -eq $ParameterSet |
Select-Object -ExpandProperty Parameters |
Where-Object Name -notin $excludeParams
if($NamesOnly) {return $params |Select-Object -ExpandProperty Name}
else {return $params}
}
else
{
if($NamesOnly) {return $cmd.Parameters.Keys |Where-Object {$_ -notin $excludeParams}}
else
{
return $cmd.Parameters.Keys |
Where-Object {$_ -notin $excludeParams} |
ForEach-Object {$cmd.Parameters[$_]}
}
}
}