forked from StartAutomating/PowerShellAI
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcopilot.ps1
More file actions
124 lines (96 loc) · 3.28 KB
/
copilot.ps1
File metadata and controls
124 lines (96 loc) · 3.28 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
function Get-Runnable {
<#
.SYNOPSIS
Gets the runnable code from the result
.DESCRIPTION
Gets the runnable code from the result
.EXAMPLE
Get-Runnable -result $result
#>
[CmdletBinding()]
param(
$result
)
$runnable = for ($idx = 1; $idx -lt $result.Count; $idx++) {
$line = $result[$idx]
if ([string]::IsNullOrEmpty($line)) {
continue
}
$line = $line.Trim()
if ($line.StartsWith('#')) {
continue
}
$line
}
return ($runnable -join "`n")
}
function copilot {
<#
.SYNOPSIS
Use GPT to help you remember PowerShell commands and other command line tools
.DESCRIPTION
Makes the request to GPT, parses the response and displays it in a box and then prompts the user to run the code or not
.EXAMPLE
# via https://twitter.com/ClemMesserli/status/1616312238209376260?s=20&t=KknO2iPk3yrQ7x42ZayS7g
copilot "using PowerShell regex, just code. split user from domain of email address with match: demo.user@google.com"
.EXAMPLE
copilot 'how to get ImportExcel'
.EXAMPLE
copilot 'processes running with more than 700 handles'
.EXAMPLE
copilot 'processes running with more than 700 handles select first 5, company and name, as json'
.EXAMPLE
copilot 'for each file in the current dir list the name and length'
.EXAMPLE
copilot 'Find all enabled users that have a samaccountname similar to Mazi; List SAMAccountName and DisplayName'
#>
param(
[Parameter(Mandatory)]
$inputPrompt,
[ValidateRange(0, 2)]
[decimal]$temperature = 0.0,
# The maximum number of tokens to generate. default 256
$max_tokens = 256,
# Don't show prompt for choice
[Switch]$Raw
)
# $inputPrompt = $args -join ' '
$shell = 'powershell, just code:'
$promptComments = ', include comments'
if (-not $IncludeComments) {
$promptComments = ''
}
$prompt = "using {0} {1}: {2}`n" -f $shell, $promptComments, $inputPrompt
$prompt += '```'
$completion = Get-GPT3Completion -prompt $prompt -max_tokens $max_tokens -temperature $temperature -stop '```'
$completion = $completion -split "`n"
if ($completion[0] -ceq 'powershell') {
$completion = $completion[1..($completion.Count - 1)]
}
if ($Raw) {
return $completion
}
else {
$result = @($inputPrompt)
$result += ''
$result += $completion
$runnable = Get-Runnable -result $result
if (Test-AifbScriptAnalyzerAvailable) {
$runnable = Invoke-Formatter -ScriptDefinition $runnable -Verbose:$false
}
Write-Codeblock -Text $runnable -ShowLineNumbers -SyntaxHighlight
$userInput = CustomReadHost
switch ($userInput) {
0 {
(Get-Runnable -result $result) | Invoke-Expression
}
1 {
explain -Value (Get-Runnable -result $result)
}
2 {
Get-Runnable -result $result | Set-Clipboard
}
default { "Not running" }
}
}
}