forked from StartAutomating/PowerShellAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Invoke-AIErrorHelper.ps1
31 lines (26 loc) · 1.1 KB
/
Invoke-AIErrorHelper.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
function Invoke-AIErrorHelper {
<#
.SYNOPSIS
Inspect the last error record and offer some suggestions on how to resolve it
.DESCRIPTION
Invoke-AIErrorHelper is a function that uses the OpenAI GPT-3 API to provide insights into errors that occur in a powershell script.
.EXAMPLE
Invoke-AIErrorHelper
#>
[CmdletBinding()]
[alias("ieh")]
param()
$lastError = $global:Error[0]
if ($null -ne $lastError) {
$message = $lastError.Exception.Message
$errorType = $lastError.FullyQualifiedErrorId
$promptPrefix = "Provide a detailed summary of the following powershell error and offer a potential powershell solution (using code if it's a confident solution):"
$errorDetails = "${errorType}`n$message"
$response = (Get-GPT3Completion -prompt "$promptPrefix`n`n$errorDetails" -max_tokens 2048).Trim()
Write-Host -ForegroundColor Cyan "$errorDetails`n"
Write-Host -ForegroundColor DarkGray $response
}
else {
Write-Host "No error has occurred"
}
}