-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathLaunch-AzureToolkit.ps1
More file actions
482 lines (407 loc) · 16.9 KB
/
Launch-AzureToolkit.ps1
File metadata and controls
482 lines (407 loc) · 16.9 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#Requires -Version 7.0
<#
.SYNOPSIS
Launch AzureToolkit
.DESCRIPTION
NOTES
Author: Wes Ellis (wes@wesellis.com)
[CmdletBinding(SupportsShouldProcess)]
[switch]$GUI,
[switch]$ListOnly,
[string]$SearchTerm,
[string]$Category,
[switch]$Favorites
)
$Global:ToolkitPath = $PSScriptRoot
$Global:ConfigPath = Join-Path $env:USERPROFILE ".azure-toolkit"
$Global:FavoritesFile = Join-Path $Global:ConfigPath "favorites.json"
$Global:HistoryFile = Join-Path $Global:ConfigPath "history.json"
if (-not (Test-Path $Global:ConfigPath)) {
New-Item -ItemType Directory -Path $Global:ConfigPath -Force | Out-Null
}
class ScriptLauncher {
[array]$Scripts = @()
[hashtable]$Categories = @{}
[array]$Favorites = @()
[array]$History = @()
ScriptLauncher() {
$this.LoadScripts()
$this.LoadFavorites()
$this.LoadHistory()
}
[void] LoadScripts() {
Write-Host "Loading scripts..." -ForegroundColor Green
$ScriptFiles = Get-ChildItem -Path (Join-Path $Global:ToolkitPath "automation-scripts") -Filter "*.ps1" -Recurse
foreach ($script in $ScriptFiles) {
$category = Split-Path (Split-Path $script.FullName -Parent) -Leaf
$ScriptInfo = @{
Name = $script.BaseName
Path = $script.FullName
Category = $category
Description = $this.ExtractDescription($script.FullName)
Parameters = $this.ExtractParameters($script.FullName)
LastUsed = $null
UsageCount = 0
}
$this.Scripts += $ScriptInfo
if (-not $this.Categories.ContainsKey($category)) {
$this.Categories[$category] = @()
}
$this.Categories[$category] += $ScriptInfo
}
}
[string] ExtractDescription([string]$ScriptPath) {
$content = Get-Content $ScriptPath -Raw -ErrorAction SilentlyContinue
if ($content -match '(?s)\.SYNOPSIS\s*\n\s*(.+?)(?=\n\s*\.|$)') {
return $Matches[1].Trim()
}
if ($content -match '# Description: (.+)') {
return $Matches[1].Trim()
}
return "No description available"
}
[array] ExtractParameters([string]$ScriptPath) {
$params = @()
$content = Get-Content $ScriptPath -Raw -ErrorAction SilentlyContinue
$ParamMatches = [regex]::Matches($content, '\[Parameter.*?\]\s*\[.*?\]\s*\$(\w+)')
foreach ($match in $ParamMatches) {
$params += $match.Groups[1].Value
}
return $params
}
[void] LoadFavorites() {
if (Test-Path $Global:FavoritesFile) {
$this.Favorites = Get-Content $Global:FavoritesFile | ConvertFrom-Json
}
}
[void] SaveFavorites() {
$this.Favorites | ConvertTo-Json | Out-File $Global:FavoritesFile -Encoding UTF8
}
[void] LoadHistory() {
if (Test-Path $Global:HistoryFile) {
$this.History = Get-Content $Global:HistoryFile | ConvertFrom-Json
}
}
[void] SaveHistory() {
if ($this.History.Count -gt 100) {
$this.History = $this.History | Select-Object -Last 100
}
$this.History | ConvertTo-Json | Out-File $Global:HistoryFile -Encoding UTF8
}
[void] ShowInteractiveMenu() {
if ($PSCmdlet.ShouldProcess("target", "operation")) {
}
Write-Host @"
� Azure Enterprise Toolkit - Script Launcher �
� Version 2.0 Enhanced �
"@ -ForegroundColor Cyan
$continue = $true
while ($continue) {
Write-Host "`nMain Menu:" -ForegroundColor Green
Write-Host "1. Browse by Category" -ForegroundColor Green
Write-Host "2. Search Scripts" -ForegroundColor Green
Write-Host "3. View Favorites" -ForegroundColor Green
Write-Host "4. Recent History" -ForegroundColor Green
Write-Host "5. Quick Launch (by ID)" -ForegroundColor Green
Write-Host "6. Script Statistics" -ForegroundColor Green
Write-Host "7. Configuration" -ForegroundColor Green
Write-Host "Q. Quit" -ForegroundColor Green
$choice = Read-Host "`nSelect option"
switch ($choice.ToUpper()) {
"1" { $this.BrowseByCategory() }
"2" { $this.SearchScripts() }
"3" { $this.ViewFavorites() }
"4" { $this.ShowHistory() }
"5" { $this.QuickLaunch() }
"6" { $this.ShowStatistics() }
"7" { $this.Configuration() }
"Q" { $continue = $false }
default { Write-Host "Invalid option" -ForegroundColor Green }
}
}
}
[void] BrowseByCategory() {
if ($PSCmdlet.ShouldProcess("target", "operation")) {
}
Write-Host "Categories:" -ForegroundColor Green
$i = 1
$CategoryList = $this.Categories.Keys | Sort-Object
foreach ($cat in $CategoryList) {
Write-Host "$i. $cat ($($this.Categories[$cat].Count) scripts)" -ForegroundColor Green
$i++
}
$selection = Read-Host "`nSelect category (number) or B to go back"
if ($selection -eq "B") { return }
$SelectedCategory = $CategoryList[$([int]$selection - 1)]
if ($SelectedCategory) {
$this.ShowScriptsInCategory($SelectedCategory)
}
}
[void] ShowScriptsInCategory([string]$Category) {
if ($PSCmdlet.ShouldProcess("target", "operation")) {
}
Write-Host "Scripts in $Category:" -ForegroundColor Green
$scripts = $this.Categories[$Category] | Sort-Object Name
for ($i = 0; $i -lt $scripts.Count; $i++) {
$script = $scripts[$i]
$star = if ($script.Path -in $this.Favorites) { "[*]" } else { " " }
Write-Host "$star $($i + 1). $($script.Name)" -ForegroundColor Green
Write-Host " $($script.Description)" -ForegroundColor Green
}
Write-Host "`nOptions: [number] to run, [F+number] to favorite, [I+number] for info, [B] back" -ForegroundColor Green
$selection = Read-Host "Selection"
if ($selection -eq "B") { return }
elseif ($selection -match "^F(\d+)$") {
$index = [int]$Matches[1] - 1
if ($index -ge 0 -and $index -lt $scripts.Count) {
$this.ToggleFavorite($scripts[$index].Path)
}
$this.ShowScriptsInCategory($Category)
}
elseif ($selection -match "^I(\d+)$") {
$index = [int]$Matches[1] - 1
if ($index -ge 0 -and $index -lt $scripts.Count) {
$this.ShowScriptInfo($scripts[$index])
}
$this.ShowScriptsInCategory($Category)
}
elseif ($selection -match "^\d+$") {
$index = [int]$selection - 1
if ($index -ge 0 -and $index -lt $scripts.Count) {
$this.LaunchScript($scripts[$index])
}
}
}
[void] SearchScripts() {
$SearchTerm = Read-Host "Enter search term"
$results = $this.Scripts | Where-Object {
$_.Name -like "*$SearchTerm*" -or
$_.Description -like "*$SearchTerm*" -or
$_.Category -like "*$SearchTerm*"
}
if ($results.Count -eq 0) {
Write-Host "No scripts found matching '$SearchTerm'" -ForegroundColor Green
return
}
if ($PSCmdlet.ShouldProcess("target", "operation")) {
}
Write-Host "Search Results for '$SearchTerm':" -ForegroundColor Green
for ($i = 0; $i -lt $results.Count; $i++) {
$script = $results[$i]
Write-Host "$($i + 1). [$($script.Category)] $($script.Name)" -ForegroundColor Green
Write-Host " $($script.Description)" -ForegroundColor Green
}
$selection = Read-Host "`nSelect script number to run or B to go back"
if ($selection -ne "B" -and $selection -match "^\d+$") {
$index = [int]$selection - 1
if ($index -ge 0 -and $index -lt $results.Count) {
$this.LaunchScript($results[$index])
}
}
}
[void] LaunchScript([hashtable]$Script) {
if ($PSCmdlet.ShouldProcess("target", "operation")) {
}
Write-Host "Launching: $($Script.Name)" -ForegroundColor Green
Write-Host "Description: $($Script.Description)" -ForegroundColor Green
if ($Script.Parameters.Count -gt 0) {
Write-Host "`nParameters:" -ForegroundColor Green
$ParamValues = @{}
foreach ($param in $Script.Parameters) {
$value = Read-Host " -$param"
if ($value) {
$ParamValues[$param] = $value
}
}
}
$HistoryEntry = @{
ScriptPath = $Script.Path
ScriptName = $Script.Name
Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
Parameters = $ParamValues
}
$this.History += $HistoryEntry
$this.SaveHistory()
Write-Host "`nExecuting script..." -ForegroundColor Green
try {
if ($ParamValues -and $ParamValues.Count -gt 0) {
& $Script.Path @paramValues
} else {
& $Script.Path
}
Write-Host "`nScript completed successfully!" -ForegroundColor Green
} catch {
Write-Host "Error executing script: $_" -ForegroundColor Green
}
Read-Host "`nPress Enter to continue"
}
[void] ToggleFavorite([string]$ScriptPath) {
if ($ScriptPath -in $this.Favorites) {
$this.Favorites = $this.Favorites | Where-Object { $_ -ne $ScriptPath }
Write-Host "Removed from favorites" -ForegroundColor Green
} else {
$this.Favorites += $ScriptPath
Write-Host "Added to favorites" -ForegroundColor Green
}
$this.SaveFavorites()
}
[void] ViewFavorites() {
if ($this.Favorites.Count -eq 0) {
Write-Host "No favorites yet. Add favorites by pressing F+number when browsing scripts." -ForegroundColor Green
Read-Host "Press Enter to continue"
return
}
if ($PSCmdlet.ShouldProcess("target", "operation")) {
}
Write-Host "Favorite Scripts:" -ForegroundColor Green
$FavScripts = $this.Scripts | Where-Object { $_.Path -in $this.Favorites }
for ($i = 0; $i -lt $FavScripts.Count; $i++) {
$script = $FavScripts[$i]
Write-Host "$($i + 1). [$($script.Category)] $($script.Name)" -ForegroundColor Green
Write-Host " $($script.Description)" -ForegroundColor Green
}
$selection = Read-Host "`nSelect script number to run or B to go back"
if ($selection -ne "B" -and $selection -match "^\d+$") {
$index = [int]$selection - 1
if ($index -ge 0 -and $index -lt $FavScripts.Count) {
$this.LaunchScript($FavScripts[$index])
}
}
}
[void] ShowHistory() {
if ($this.History.Count -eq 0) {
Write-Host "No history yet." -ForegroundColor Green
Read-Host "Press Enter to continue"
return
}
if ($PSCmdlet.ShouldProcess("target", "operation")) {
}
Write-Host "Recent Script Executions:" -ForegroundColor Green
$recent = $this.History | Select-Object -Last 10
for ($i = 0; $i -lt $recent.Count; $i++) {
$entry = $recent[$i]
Write-Host "$($i + 1). $($entry.ScriptName) - $($entry.Timestamp)" -ForegroundColor Green
}
Read-Host "`nPress Enter to continue"
}
[void] ShowStatistics() {
if ($PSCmdlet.ShouldProcess("target", "operation")) {
}
Write-Host "Repository Statistics:" -ForegroundColor Green
Write-Host "Total Scripts: $($this.Scripts.Count)" -ForegroundColor Green
Write-Host "Categories: $($this.Categories.Count)" -ForegroundColor Green
Write-Host "Favorites: $($this.Favorites.Count)" -ForegroundColor Green
Write-Host "History Entries: $($this.History.Count)" -ForegroundColor Green
Write-Host "`nTop Categories:" -ForegroundColor Green
$TopCategories = $this.Categories.GetEnumerator() | Sort-Object { $_.Value.Count } -Descending | Select-Object -First 5
foreach ($cat in $TopCategories) {
Write-Host " $($cat.Key): $($cat.Value.Count) scripts" -ForegroundColor Green
}
Read-Host "`nPress Enter to continue"
}
[void] ShowScriptInfo([hashtable]$Script) {
if ($PSCmdlet.ShouldProcess("target", "operation")) {
}
Write-Host "Script Information:" -ForegroundColor Green
Write-Host "Name: $($Script.Name)" -ForegroundColor Green
Write-Host "Category: $($Script.Category)" -ForegroundColor Green
Write-Host "Path: $($Script.Path)" -ForegroundColor Green
Write-Host "Description: $($Script.Description)" -ForegroundColor Green
if ($Script.Parameters.Count -gt 0) {
Write-Host "`nParameters:" -ForegroundColor Green
foreach ($param in $Script.Parameters) {
Write-Host " -$param" -ForegroundColor Green
}
}
Read-Host "`nPress Enter to continue"
}
[void] QuickLaunch() {
$ScriptName = Read-Host "Enter script name (partial match supported)"
$matches = $this.Scripts | Where-Object { $_.Name -like "*$ScriptName*" }
if ($matches.Count -eq 0) {
Write-Host "No scripts found" -ForegroundColor Green
} elseif ($matches.Count -eq 1) {
$this.LaunchScript($matches[0])
} else {
Write-Host "Multiple matches found:" -ForegroundColor Green
for ($i = 0; $i -lt $matches.Count; $i++) {
Write-Host "$($i + 1). $($matches[$i].Name)" -ForegroundColor Green
}
$selection = Read-Host "Select number"
if ($selection -match "^\d+$") {
$index = [int]$selection - 1
if ($index -ge 0 -and $index -lt $matches.Count) {
$this.LaunchScript($matches[$index])
}
}
}
}
[void] Configuration() {
if ($PSCmdlet.ShouldProcess("target", "operation")) {
}
Write-Host "Configuration:" -ForegroundColor Green
Write-Host "1. Clear History" -ForegroundColor Green
Write-Host "2. Clear Favorites" -ForegroundColor Green
Write-Host "3. Export Configuration" -ForegroundColor Green
Write-Host "4. Import Configuration" -ForegroundColor Green
Write-Host "B. Back" -ForegroundColor Green
$choice = Read-Host "Select option"
switch ($choice) {
"1" {
$this.History = @()
$this.SaveHistory()
Write-Host "History cleared" -ForegroundColor Green
}
"2" {
$this.Favorites = @()
$this.SaveFavorites()
Write-Host "Favorites cleared" -ForegroundColor Green
}
"3" {
$ExportPath = Read-Host "Enter export path"
@{
Favorites = $this.Favorites
History = $this.History
} | ConvertTo-Json | Out-File $ExportPath -Encoding UTF8
Write-Host "Configuration exported" -ForegroundColor Green
}
"4" {
$ImportPath = Read-Host "Enter import path"
if (Test-Path $ImportPath) {
$config = Get-Content $ImportPath | ConvertFrom-Json
$this.Favorites = $config.Favorites
$this.History = $config.History
$this.SaveFavorites()
$this.SaveHistory()
Write-Host "Configuration imported" -ForegroundColor Green
}
}
}
if ($choice -ne "B") {
Read-Host "Press Enter to continue"
}
}
}
$launcher = [ScriptLauncher]::new()
if ($ListOnly) {
$launcher.Scripts | Format-Table Name, Category, Description -AutoSize
} elseif ($SearchTerm) {
$results = $launcher.Scripts | Where-Object {
$_.Name -like "*$SearchTerm*" -or
$_.Description -like "*$SearchTerm*" -or
$_.Category -like "*$SearchTerm*"
}
$results | Format-Table Name, Category, Description -AutoSize
} elseif ($Category) {
if ($launcher.Categories.ContainsKey($Category)) {
$launcher.Categories[$Category] | Format-Table Name, Description -AutoSize
} else {
Write-Host "Category not found: $Category" -ForegroundColor Green
}
} elseif ($Favorites) {
$FavScripts = $launcher.Scripts | Where-Object { $_.Path -in $launcher.Favorites }
$FavScripts | Format-Table Name, Category, Description -AutoSize
} else {
$launcher.ShowInteractiveMenu()
}
Write-Host "`nThank you for using Azure Enterprise Toolkit!" -ForegroundColor Green