-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-AGCommandletWrapper.ps1
52 lines (52 loc) · 1.31 KB
/
Get-AGCommandletWrapper.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
param(
[switch]$toFile,
[switch]$filterParams,
$filteredParameters = @(),
$filePath,
$cmdletNameModifierString = "Custom",
[Parameter(Mandatory)]
$cmdletName
)
$helpInfo = get-help $cmdletName -Detailed
if($helpInfo.count -gt 1)
{
write-host "Multiple results returned" -BackgroundColor Red
break
}
#else
$paramNames = $helpInfo.parameters.parameter.name
$script = @("param(")
$script += $paramNames.ForEach({"`t$" + $_ + ","})
$script[$script.Count - 1] = $script[$script.Count - 1].Replace(",","")
$script += ")"
if($filterParams)
{
$script += "`$filteredParameters = @("
$script += $filteredParameters.ForEach({"`t'" + $_ + "',"})
$script[$script.Count - 1] = $script[$script.Count - 1].Replace(",","")
$script += ")"
$script += "`$params = @{}
foreach(`$key in `$PSBoundParameters.keys)
{
if(!`$filteredParameters.Contains(`$key))
{
`$params[`$key] = `$PSBoundParameters[`$key]
}
}
`$output = $cmdletName @params
"
}
else
{
$script += "`$output= $cmdletName @PSBoundParameters"
}
if($toFile)
{
$cmdletNameModifierString = "-" + $cmdletNameModifierString
$fileName = $filePath + $cmdletName.Replace("-",$cmdletNameModifierString) + ".ps1"
$script | Out-File $fileName
}
else
{
$script
}