forked from jdhitsolutions/PSScriptTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopy-HelpExample.ps1
More file actions
145 lines (121 loc) · 5.03 KB
/
Copy-HelpExample.ps1
File metadata and controls
145 lines (121 loc) · 5.03 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
Function Copy-HelpExample {
[cmdletbinding()]
[alias("che")]
Param(
[Parameter(
Position = 0,
Mandatory,
HelpMessage = "Enter the name of the PowerShell command"
)]
[ValidateScript({
If (Get-Command $_) {
$True
}
else {
Throw "Can't find a command called $_"
}
})]
[string]$Name,
[Parameter(
HelpMessage = "Gets help that explains how the cmdlet works in the specified provider path. Enter a PowerShell provider path."
)]
[ValidateScript({Test-Path $_})]
[string]$Path
)
DynamicParam {
if ($IsWindows -OR ($PSEdition -eq "Desktop")) {
#The dynamic parameter name
$paramName = "UseGridView"
#define a parameter attribute object
$attributes = New-Object System.Management.Automation.ParameterAttribute
$attributes.HelpMessage = "Select help examples using Out-Gridview."
#define a collection for attributes
$attributeCollection = New-Object -Type System.Collections.ObjectModel.Collection[System.Attribute]
$attributeCollection.Add($attributes)
#define an alias
$alias = [System.Management.Automation.AliasAttribute]::new("ogv")
$attributeCollection.Add($alias)
#define the dynamic param
$dynParam1 = New-Object -Type System.Management.Automation.RuntimeDefinedParameter($paramName, [Switch], $attributeCollection)
#create array of dynamic parameters
$paramDictionary = New-Object -Type System.Management.Automation.RuntimeDefinedParameterDictionary
$paramDictionary.Add($paramName, $dynParam1)
#use the array
return $paramDictionary
} #if
} #dynamic param
Begin {
Write-Verbose "Starting $($myinvocation.mycommand)"
#A regex pattern to strip off prompts, comments and empty lines from the code sample
$rx = [System.Text.RegularExpressions.Regex]::new("(PS.*>)|(#.*)|(^\s$)", "Multiline")
#adjust PSBoundParameters
$PSBoundParameters.Add("Examples", $True)
if ($PSBoundParameters.ContainsKey("UseGridView")) {
#remove this parameter from PSBoundParameters because it doesn't belong to Get-Help
[void]($PSBoundParameters.Remove("UseGridView"))
#set a flag
$ogv = $True
}
} #begin
Process {
Write-Verbose "Getting help examples for $Name"
$help = Get-Help @PSBoundParameters
if ($help.examples.example.count -gt 0) {
$choices = $help.examples.example | Select-Object -Property Title,
@{Name = "CodeSample"; Expression = {($rx.replace($_.code, "")).trim()}}
#force the ISE to use Out-Gridview
if ($ogv -OR ($host.name -match "PowerShell ISE")) {
Write-Verbose "Launching Out-Gridview"
$choices | Out-GridView -Title "Select one or more code samples to copy" -PassThru |
ForEach-Object {$_.codeSample} | Set-Clipboard
} #if gridview
else {
#Use console menu
$hash = @{}
$head = @"
$([char]0x1b)[1;4mCode Samples$([char]0x1b)[0m
Each help example is numbered to the left. At the prompt below,
select the code samples you want to copy to the clipboard. Separate
multiple values with a comma.
Some example code includes the output.
"@
$head
for ($i= 0;$i -lt $choices.count;$i++) {
"`r"
"$([char]0x1b)[96m[$($i+1)]$([char]0x1b)[0m $($choices[$i].title.trim())"
"`r"
" $($choices[$i].codesample)"
#add the sample to the temporary hashtable
$hash.add(($i+1).tostring(),$choices[$i].codesample)
}
#prompt the user for a choice
$r = Read-Host "`n$([char]0x1b)[38;5;46mPlease select items to copy to the clipboard by number. Separate multiple entries with a comma. Press Enter alone to cancel$([char]0x1b)[0m"
if ($r -match ",") {
$items = $r.split(",")
}
elseif ($r -match "\d") {
$items = $r
}
else {
Write-Verbose "You must have cancelled."
}
#initialize a list to hold the choices
$select = [System.Collections.Generic.List[string]]::new()
if ($items) {
foreach ($item in $items) {
Write-Verbose "getting item $item"
$select.add($hash.item($item))
}
}
#copy all the selections to the clipboard
$select | Set-Clipboard
} #use console
} #if help examples found
else {
Write-Warning "No help or examples found for $Name"
}
} #process
End {
Write-Verbose "Ending $($myinvocation.mycommand)"
} #end
} #end function