-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathGet-CommandSkeleton.ps1
More file actions
131 lines (111 loc) · 3.49 KB
/
Get-CommandSkeleton.ps1
File metadata and controls
131 lines (111 loc) · 3.49 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
[CmdletBinding()]
PARAM(
[Parameter(Mandatory=$True)]
[string]
$CommandName,
[Parameter(Mandatory=$False)]
[string]
[ValidateSet("Alias","Cmdlet","Function","Script")]
$CommandType
)
BEGIN {
# This section just gets a list of automatic variables created by [CmdletBinding()] so they are not added to the PARAM block
Function Test-InternalFunction {
[CmdletBinding()]
PARAM()
PROCESS{}
}
$IgnoreParameters = (Get-Command Test-InternalFunction).Parameters.Keys
Remove-Item -Path Function:\Test-InternalFunction
}
PROCESS {
$ParamHash = @{
Name = $CommandName
}
if ( $PSBoundParameters.ContainsKey("CommandType")) {
$ParamHash.Add("CommandType",$CommandType)
}
$Command = Get-Command @ParamHash
if ( [string]::IsNullOrEmpty($Command) ) {
exit
}
$ModuleName = $Command.ModuleName
$Parameters = $Command.Parameters
$DefaultParameterSet = $Command.DefaultParameterSet
$ParameterSets = $Command.ParameterSets.Name
$i=0
# Write out the function declaration up to the PARAM block
[array]$Output = "Function $CommandName {"
if (-NOT([string]::IsNullOrEmpty($DefaultParameterSet))) {
$Output += "[CmdletBinding(DefaultParameterSetName='$DefaultParameterSet')]"
}
else {
$Output += "[CmdletBinding()]"
}
$Output += "PARAM("
# This loop will write out each of the parameters for the command
$OutputParameterKeys = $Parameters.Keys | Where-Object { $IgnoreParameters -notcontains $_ }
ForEach ( $Key in $OutputParameterKeys) {
if ( $Parameters[$Key].SwitchParameter -eq $True ) {
$Type = "Switch"
}
else {
$Type = $Parameters[$Key].ParameterType.Name
if ( $Type -eq 'FlagsExpression`1') {
$Type = 'System.Management.Automation.FlagsExpression`1[System.IO.FileAttributes]'
}
}
if ( $i -eq ($OutputParameterKeys.Count - 1) ) {
$Comma = ""
}
else {
$Comma = ","
}
$ParamParameterSets = $Parameters[$Key].ParameterSets.Keys
# Write Out the [Parameter] information
if ( $ParamParameterSets -eq "__AllParameterSets" ) {
ForEach ($ParameterSet in $ParameterSets) {
$Mandatory = $Parameters[$Key].ParameterSets["__AllParameterSets"].IsMandatory
$Position = $Parameters[$Key].ParameterSets["__AllParameterSets"].Position
if ($Position -ge 0) {
$Position = ", Position={0}" -f $Position
}
else {
$Position = ""
}
$Output += ' [Parameter(ParameterSetName="{0}", Mandatory=${1}{2})]' -f $ParameterSet, $Mandatory, $Position
}
}
else {
ForEach ($ParameterSet in $ParamParameterSets) {
$Mandatory = $Parameters[$Key].ParameterSets[$ParameterSet].IsMandatory
$Position = $Parameters[$Key].ParameterSets[$ParameterSet].Position
if ($Position -ge 0) {
$Position = ", Position={0}" -f $Position
} else {$Position = ""}
$Output += ' [Parameter(ParameterSetName="{0}", Mandatory=${1}{2})]' -f $ParameterSet, $Mandatory, $Position
}
}
# Parameter Type
$Output += ' [{0}]' -f $Type
# Parameter Name, and comma if not the last
$Output += ' ${0}{1}' -f $Key, $Comma
$Output += ''
$i++
}
# Finish up the skeleton code
$Output += ")"
$Output += "BEGIN {}"
$Output += "PROCESS {"
# Call the program from its absolute location using the same parameters passed to the current command
if ( -NOT([string]::IsNullOrEmpty($ModuleName))) {
$Output += ' {0}\{1} @PSBoundParameters' -f $ModuleName, $CommandName
}
else {
$Output += '{0} @PSBoundParameters' -f $CommandName
}
$Output += "}"
$Output += "END {}"
$Output += "}"
Write-Output $Output
}