-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInvoke-AIEventAnalyzer1.ps1
211 lines (169 loc) · 8.14 KB
/
Invoke-AIEventAnalyzer1.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
<#
.SYNOPSIS
This function logs the input string and natural language query.
.DESCRIPTION
The LogData function takes an input string and a natural language query as parameters.
It logs these parameters to a file or other logging mechanism.
.PARAMETER InputString
This parameter accepts the input string that needs to be logged.
.PARAMETER NaturalLanguageQuery
This parameter accepts the natural language query that needs to be logged.
.EXAMPLE
LogData -InputString $inputString -NaturalLanguageQuery "Show only processes using more than 500MB of memory"
#>
function LogData {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]$InputString,
[Parameter(Mandatory = $true)]
[string]$NaturalLanguageQuery
)
# Log the input string and natural language query to a file or other logging mechanism
# This could be a file, a database, a logging service, etc.
# Example: Write-Output "$InputString | $NaturalLanguageQuery" >> log.txt
}
<#
.SYNOPSIS
This function uses Azure OpenAI to interpret the input using a language model and the user's query.
.DESCRIPTION
The Invoke-AICopilot function takes an input object and a natural language query as parameters.
It converts the input object to a string and calls the Invoke-AzureOpenAIChatCompletion function to interpret the input using a language model and the user's query.
.PARAMETER InputObject
This parameter accepts the input object that needs to be interpreted.
.PARAMETER NaturalLanguageQuery
This parameter accepts the natural language query to interpret the input object.
.EXAMPLE
Invoke-AICopilot -InputObject $InputObject -NaturalLanguageQuery "Show only processes using more than 500MB of memory"
#>
function Invoke-AICopilot {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[object]$InputObject,
[Parameter(Mandatory = $true, Position = 0)]
[string]$NaturalLanguageQuery
)
begin {
# Load the Invoke-AzureOpenAIChatCompletion script
. $PSScriptRoot\Invoke-AzureOpenAIChatCompletion.ps1
# This block runs once before any input is processed
# Initialize an array to store the input objects
$inputObjects = @()
}
process {
# This block runs once for each item of input
# Add the current input object to the array
$inputObjects += $InputObject
}
end {
# This block runs once after all input has been processed
# Convert the array of input objects into a single string
$inputString = $inputObjects | Out-String
# Call the Invoke-AzureOpenAIChatCompletion function to interpret the input using a language model and the user's query
$response = Invoke-AzureOpenAIChatCompletion -SystemPrompt $NaturalLanguageQuery -OneTimeUserPrompt $inputString -Precise -simpleresponse
# Return the response from the Azure OpenAI Chat Completion function
return $response
}
}
<#
.SYNOPSIS
This function clears the LLMDataJSON.
.DESCRIPTION
The Clear-LLMDataJSON function takes a string of data as input, finds the first instance of '[' and the last instance of ']', and returns the substring between these two characters. This effectively removes any characters before the first '[' and after the last ']'.
.PARAMETER data
A string of data that needs to be cleared.
.EXAMPLE
$data = "{extra characters}[actual data]{extra characters}"
Clear-LLMDataJSON -data $data
#>
# This function is used to clear the LLMDataJSON
function Clear-LLMDataJSON {
# Define the parameters for the function
param (
# The data parameter is mandatory and should be a string
[Parameter(Mandatory = $true)]
[string]$data
)
# Find the first occurrence of '[' and remove everything before it
$data = $data.Substring($data.IndexOf('['))
# Find the last occurrence of ']' and remove everything after it
$data = $data.Substring(0, $data.LastIndexOf(']') + 1)
# Return the cleaned data
return $data
}
# Define the prompt for the AI model
$prompt_one = @'
###Instruction###
You act as data analyst. Your task is to suggest a list of prompts designed to analyze issues based on the provided data. The prompts MUST be focused on potential root causes, understand its impact on the system's performance and stability, and propose detailed step-by-step solutions or further actions to resolve the problem. Responce MUST be as JSON format only. Audience is IT Proffesional.
Example of JSON with two records:
[
{
"promptNumber": 1,
"prompt": "Analyze the root cause of the W32time service stopping and assess if it is a regular behavior or an indication of an underlying issue.",
"eventType": "W32time Service Event",
"eventID": 258,
"eventLevel": "Information",
"eventMessage": "W32time service is stopping",
"eventTime": "2024-03-22T19:02:09.605Z",
"eventReturnCode": "0x00000000",
"analysisActions": [
"Check if the service is configured to stop at scheduled times.",
"Review system logs for any related errors or warnings.",
"Verify if there was a system shutdown or restart."
]
},
{
"promptNumber": 3,
"prompt": "Examine the security implications of Acrobat's AcroCEF.exe being blocked from making system calls to Win32k.sys and suggest measures to mitigate potential risks.",
"eventType": "Security Mitigation",
"eventID": 10,
"eventLevel": "Warning",
"eventMessage": "The process AcroCEF.exe was prevented from making system calls to the Win32k.sys library.",
"eventTime": "2024-03-16T22:26:05Z",
"eventProcessID": 22432,
"analysisActions": [
"Ensure that Adobe Acrobat is running the latest version.",
"Review Adobe Acrobat's security settings and permissions.",
"Check for any related security advisories from Adobe."
]
}
]
'@
# Clean the system prompt by removing non-ASCII characters
$prompt_one = [System.Text.RegularExpressions.Regex]::Replace($prompt_one, "[^\x00-\x7F]", " ")
# Get a list of all Windows event logs, sort them by record count in descending order, and select the top 25 logs
$logs = Get-WinEvent -ListLog * -ErrorAction SilentlyContinue | Sort-Object RecordCount -Descending| Select-Object LogName, RecordCount -First 25
# Display the name and record count of each log
#$logs | ForEach-Object {Write-Host "$($_.LogName) - $($_.RecordCount) records"}
$logs | FT *
# Ask the user to input the name of the log they want to analyze
$chosenLogName = Read-Host "Please enter the LogName from the list above to analyze events"
$logRecordCount = ($logs | Where-Object {$_.logname -eq "$chosenLogName"}).Recordcount
$chosenLogNameNewest = Read-Host "Please enter newes record count (1-$logRecordCount)"
# Fetch the Windows events to analyze
$data_to_analyze = Get-WinEvent -LogName $chosenLogName -MaxEvents $chosenLogNameNewest | Select-Object Message, Level, ProviderName, ProviderId, LogName, TimeCreated
# Invoke the AI model with the prompt and the data to analyze
$json_data = $data_to_analyze | Invoke-AICopilot -NaturalLanguageQuery $prompt_one
# Clean the returned JSON data
$json_data = Clear-LLMDataJSON -data $json_data
# Convert the cleaned JSON data to a PowerShell object
$object_prompt = ($json_data | ConvertFrom-Json )
while ($true) {
# Display the prompt number, prompt, and analysis actions from the object
$object_prompt | Format-List promptNumber, prompt, analysisActions
Write-Host "Enter 'q' to quit the script at any time."
# Ask the user to choose a prompt number to analyze events
$prompt_count = $object_prompt.Count
$choose_prompt_number = Read-Host "Choose the number of prompt to analyze events (1-$prompt_count)"
if ($choose_prompt_number -eq 'q') {
Write-Host "Ending script..."
break
}
# Construct the chosen prompt
$choose_prompt = $($object_prompt[($choose_prompt_number - 1)].prompt + " " + $object_prompt[($choose_prompt_number - 1)].analysisActions -join " ")
# Display the chosen prompt
Write-Host "Prompt: '$choose_prompt'"
# Invoke the AI model with the chosen prompt and the data to analyze
$data_to_analyze | Invoke-AICopilot -NaturalLanguageQuery $choose_prompt
}