-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPowerShell-Function-Annotator.ps1
288 lines (234 loc) · 10.5 KB
/
PowerShell-Function-Annotator.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
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
<#
Created by Aric Galloso
3/26/2025
Description: This PowerShell script, automates the process of documenting PowerShell functions by leveraging Gemini-1.5-pro API to generate descriptions based on the function's code. It provides a structured way to request and integrate function descriptions into the script itself, improving readability and maintainability.
The PowerShell function called `Invoke-PowerShellFunctionAnnotation` processes PowerShell scripts, either individually or in batch, and applies annotation or transformation using a Gemini-1.5-pro API Key. The function sets up the parameter handling and path resolution to prepare for this processing.
#>
function Invoke-PowerShellFunctionAnnotation {
[CmdletBinding(DefaultParameterSetName='Batch')]
param (
[Parameter(Mandatory, ParameterSetName='Single')]
[Parameter(Mandatory, ParameterSetName='Batch')]
[string]$ApiKey,
[Parameter(Mandatory, ParameterSetName='Single')]
[string]$SingleScriptPath,
[Parameter(Mandatory, ParameterSetName='Batch')]
[string]$SourceDirectory,
[Parameter(Mandatory, ParameterSetName='Single')]
[Parameter(Mandatory, ParameterSetName='Batch')]
[string]$DestinationPath
)
# Helper function to resolve paths with quote handling
function Resolve-QuotedPath {
param([string]$Path)
$cleanPath = $Path.Trim().Trim('"''')
try {
$fullPath = Resolve-Path $cleanPath -ErrorAction Stop
return $fullPath.Path
}
catch {
return $cleanPath
}
}
# Helper function to add function descriptions
<#
Description: This PowerShell function Add-FunctionDescriptions takes PowerShell code as input and uses Gemini-1.5-pro API to generate descriptions for each function defined within the code.
Here's a breakdown:
1. **`Add-FunctionDescriptions` Function:**
- Takes two parameters: `$ScriptCode` (the PowerShell code to analyze) and `$ApiKey` (the API key for accessing the external service).
2. **`Get-FunctionDescription` Nested Helper Function:**
- Takes two parameters: `$FunctionCode` (the code of a single function extracted from `$ScriptCode`) and `$ApiKey`.
- Constructs a prompt that asks the API to describe the provided `$FunctionCode`. This prompt is crucial as it structures the request sent to the API. It effectively says, "Please describe what this PowerShell function does:" followed by the function's code.
- Creates a request body in a hashtable format suitable for sending to the API. This request body contains the generated prompt.
#>
function Add-FunctionDescriptions {
param(
[string]$ScriptCode,
[string]$ApiKey
)
# Nested helper function to extract function descriptions
function Get-FunctionDescription {
param(
[string]$FunctionCode,
[string]$ApiKey
)
$prompt = "Please provide a description of what the following PowerShell Script does:
$FunctionCode"
$requestBody = @{
contents = @(
@{
parts = @(
@{
text = $prompt
}
)
}
)
}
$bodyJson = ConvertTo-Json -Depth 10 $requestBody
$headers = @{
"Content-Type" = "application/json"
"x-goog-api-key" = $ApiKey
}
$apiUrl = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro:generateContent"
try {
Write-Host "Sending request to Gemini API for function description..."
$response = Invoke-RestMethod -Uri $apiUrl -Method Post -Headers $headers -Body $bodyJson -ErrorAction Stop
if ($response.candidates -and $response.candidates.Count -gt 0 -and
$response.candidates[0].content -and
$response.candidates[0].content.parts -and
$response.candidates[0].content.parts.Count -gt 0) {
return $response.candidates[0].content.parts[0].text.Trim()
}
Write-Warning "Could not extract description from Gemini API response."
return "# Could not extract description from Gemini API response."
}
catch {
Write-Error "An error occurred during the API call: $($_.Exception.Message)"
return "# Error during API call: $($_.Exception.Message)"
}
}
# Regex to find function definitions
$functionRegex = '(?s)function\s+[\w-]+\s*\{.*?\}'
$functions = [regex]::Matches($ScriptCode, $functionRegex) | ForEach-Object { $_.Value }
# If no functions found, return original code
if ($functions.Count -eq 0) {
return $ScriptCode
}
# Annotate each function
$annotatedCode = $ScriptCode
foreach ($func in $functions) {
$description = Get-FunctionDescription -FunctionCode $func -ApiKey $ApiKey
$annotatedFunction = "<#
Description: $description
#>
$func"
$annotatedCode = $annotatedCode.Replace($func, $annotatedFunction)
}
return $annotatedCode
}
# Resolve and validate paths
$ApiKey = $ApiKey
# Determine if processing single script or batch
$isSingleScript = $PSCmdlet.ParameterSetName -eq 'Single'
# Resolve source and destination paths
$sourcePath = if ($isSingleScript) {
Resolve-QuotedPath -Path $SingleScriptPath
} else {
Resolve-QuotedPath -Path $SourceDirectory
}
$destPath = Resolve-QuotedPath -Path $DestinationPath
# Validate source path
if (-not (Test-Path $sourcePath -PathType ($isSingleScript ? 'Leaf' : 'Container'))) {
throw "Source path does not exist: $sourcePath"
}
# Ensure destination directory exists
$destDir = if ($isSingleScript) { Split-Path $destPath -Parent } else { $destPath }
if (-not (Test-Path $destDir)) {
New-Item -ItemType Directory -Path $destDir | Out-Null
}
# Determine scripts to process
$scriptsToProcess = if ($isSingleScript) {
Get-Item $sourcePath
} else {
Get-ChildItem -Path $sourcePath -Filter "*.ps1" -Recurse
}
# Process scripts
$successCount = 0
$failedScripts = @()
foreach ($script in $scriptsToProcess) {
try {
# Read script content
$originalCode = Get-Content $script.FullName -Raw
# Annotate the script
$annotatedCode = Add-FunctionDescriptions -ScriptCode $originalCode -ApiKey $ApiKey
# Determine destination file path
$destinationFilePath = if ($isSingleScript) {
$destPath
} else {
$relativePath = $script.FullName.Substring($sourcePath.Length).TrimStart('\')
Join-Path -Path $destPath -ChildPath $relativePath
}
# Ensure destination subdirectory exists for batch processing
if (-not $isSingleScript) {
$destinationDir = Split-Path -Path $destinationFilePath -Parent
if (-not (Test-Path -Path $destinationDir)) {
New-Item -ItemType Directory -Path $destinationDir | Out-Null
}
}
# Save annotated script
$annotatedCode | Out-File -FilePath $destinationFilePath -Encoding UTF8
Write-Host "Annotated: $($script.Name)"
$successCount++
}
catch {
Write-Error "Failed to annotate script $($script.Name): $($_.Exception.Message)"
$failedScripts += $script.Name
}
}
# Provide summary
Write-Host "
Annotation Complete
------------------
Total Scripts: $($scriptsToProcess.Count)
Successfully Annotated: $successCount
Failed Scripts: $($failedScripts.Count)"
if ($failedScripts.Count -gt 0) {
Write-Host "Failed Scripts:"
$failedScripts | ForEach-Object { Write-Host "- $_" }
}
}
# Main Script
try {
Write-Host "PowerShell Script Annotator"
Write-Host "==========================="
# Prompt for processing type
$processingType = Read-Host "Do you want to annotate a single PowerShell script or a batch of scripts? (single/batch)"
# Validate processing type
while ($processingType -notin @('single', 'batch')) {
Write-Error "Invalid option. Please enter 'single' or 'batch'."
$processingType = Read-Host "Do you want to annotate a single PowerShell script or a batch of scripts? (single/batch)"
}
# Prepare parameters for the main function
$params = @{}
# Get API key first
$params['ApiKey'] = Read-Host "Enter your Google Cloud Generative AI API Key"
# Source path handling
$sourcePrompt = if ($processingType -eq 'single') {
"Enter the full path of the PowerShell script to annotate"
} else {
"Enter the full path of the source directory containing PowerShell scripts"
}
$sourcePath = Read-Host $sourcePrompt
# Destination path handling
$destPrompt = if ($processingType -eq 'single') {
"Enter the full path for the annotated script (including filename)"
} else {
"Enter the full path of the destination directory for annotated scripts"
}
$destPath = Read-Host $destPrompt
# Add source and destination parameters based on processing type
if ($processingType -eq 'single') {
$params['SingleScriptPath'] = $sourcePath
} else {
$params['SourceDirectory'] = $sourcePath
}
$params['DestinationPath'] = $destPath
# Confirm before processing
$confirmMessage = if ($processingType -eq 'single') {
"Are you sure you want to annotate the script $sourcePath and save it to $destPath? (y/n)"
} else {
"Are you sure you want to annotate all PowerShell scripts in $sourcePath and save them too $destPath? (y/n)"
}
$confirmation = Read-Host $confirmMessage
if ($confirmation -eq 'y') {
# Call the main function with appropriate parameters
Invoke-PowerShellFunctionAnnotation @params
}
else {
Write-Host "Annotation cancelled."
}
}
catch {
Write-Error "An error occurred: $($_.Exception.Message)"
}