-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpatch.ps1
312 lines (260 loc) · 9.61 KB
/
patch.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName PresentationFramework
$running = $true
$Host.UI.RawUI.BackgroundColor = "DarkBlue"
function displayProcess
{
$saveFolder = New-Object System.Windows.Forms.saveFiledialog -Property @{
InitialDirectory = [Environment]::GetFolderPath('Desktop')
Filter = "Log Files|*.Log|Text File|*.txt| All Files| *.*"
Title = 'Save As'
ShowHelp = $true
}
$main_form = New-Object System.Windows.Forms.Form
$main_form.Text = 'Processor Monitor'
$main_form.Width = 850
$main_form.Height = 600
$main_form.AutoSize = $true
$main_form.StartPosition = 'CenterScreen'
$main_form.BackColor = '#9e9c96'
$btnScheduled = New-Object System.Windows.Forms.Button
$btnScheduled.Enabled = $true
$btnScheduled.Location = New-Object System.Drawing.Size(250,40)
$btnScheduled.Size = New-Object System.Drawing.Size(400, 25)
$btnScheduled.Text = 'Show Scheduled Tasks'
$btnScheduled.Add_Click(
{
$lstTasks.Items.Clear()
$lstTasks.Items.Add("Scheduled Tasks:")
$lstTasks.Items.Add("")
foreach($task in Get-ScheduledTask | Select-Object -Property TaskName | Sort-Object -Property TaskName)
{
$lstTasks.Items.Add($task.TaskName)
}
$lstTasks.Enabled = $true
$btnDelete.Enabled = $true
$btnRemove.Enabled = $true
})
$lstProcess = New-Object System.Windows.Forms.ListBox
$lstProcess.Location = New-Object System.Drawing.Point(30,70)
$lstProcess.Width = 400
$lstProcess.Height = 400
$lstProcess.BackColor = "#000000"
$lstProcess.ForeColor = "#ffffff"
$lstProcess.Items.Add("Top 20 Running Processes:")
$lstProcess.Items.Add("")
foreach($process in Get-Process| Sort-Object -Property CPU -Descending | Select-Object -Property Name -First 20)
{
$lstProcess.Items.Add($process.Name)
}
$lstProcess.Add_Click(
{
if(($lstProcess.SelectedItem.ToString() -ne "") -and ($lstProcess.SelectedItem.ToString() -ne "Top 20 Running Processes:"))
{
Write-Host $lstProcess.SelectedItem.ToString()
$btnStop.Enabled = $true
}
else
{
$btnStop.Enabled = $false
}
})
$lstTasks = New-Object System.Windows.Forms.ListBox
$lstTasks.Location = New-Object System.Drawing.Point(450,70)
$lstTasks.Width = 400
$lstTasks.Height = 400
$lstTasks.Enabled = $false
$lstTasks.Add_Click(
{
if($lstTasks.SelectedItem.ToString() -ne "Scheduled Tasks:" -and $lstProcess.SelectedItem.ToString() -ne "")
{
$btnDelete.Enabled = $true
$btnOpen.Enabled = $true
}
})
$btnStop = New-Object System.Windows.Forms.Button
$btnStop.Enabled = $false
$btnStop.Location = New-Object System.Drawing.Size(40,500)
$btnStop.Size = New-Object System.Drawing.Size(120,25)
$btnStop.Text = 'End Process'
$btnStop.Add_Click(
{
$procName = $lstProcess.SelectedItem.ToString()
$msgBoxInput = [System.Windows.MessageBox]::Show('Are you sure that you want to remove the process ' + $procName + "?",'End Process','YesNoCancel','Question')
switch ($msgBoxInput)
{
'yes'
{
Stop-Process -Name $procName -Force -Confirm:$false
foreach($process in Get-Process| Select-Object -Property Name -First 10 | Sort-Object -Property CPU -Descending)
{
$lstProcess.Items.Add($process.TaskName)
}
}
'no'
{
}
'cancel'
{
}
}
})
$btnRefresh = New-Object System.Windows.Forms.Button
$btnRefresh.Enabled = $true
$btnRefresh.Location = New-Object System.Drawing.Size(170,500)
$btnRefresh.Size = New-Object System.Drawing.Size(120,25)
$btnRefresh.Text = 'Refresh'
$btnRefresh.Add_click({
$lstProcess.Items.Clear()
$lstProcess.Items.Add("Top 20 Running Processes:")
$lstProcess.Items.Add("")
foreach($process in Get-Process| Sort-Object -Property CPU -Descending | Select-Object -Property Name -First 20)
{
$lstProcess.Items.Add($process.Name)
}
})
$btnSave = New-Object System.Windows.Forms.Button
$btnSave.Location = New-Object System.Drawing.Size(300,500)
$btnSave.Size = New-Object System.Drawing.Size(120,25)
$btnSave.Text = 'Save'
$btnSave.Add_Click(
{
$saveFolder.ShowDialog()
$CPUPercent = @{
Name = ‘CPUPercent’
Expression =
{
$TotalSec = (New-TimeSpan -Start $_.StartTime).TotalSeconds
[Math]::Round( ($_.CPU * 100 / $TotalSec), 2)
}
}
do{
Get-Process |
Select-Object -Property Name, CPU, $CPUPercent, Description |
Sort-Object -Property CPUPercent -Descending |
where-object {$_.CPUPercent -gt 5} |
out-file -filepath $saveFolder.FileName -encoding ASCII -width 70 -Append
$i++
Start-Sleep -s 10
} while ($i -lt 6)
[System.Media.SystemSounds]::Beep.Play()
$msgBoxInput = [System.Windows.MessageBox]::Show(“Now wasn’t that fun.. Let’s See Whats in the file. :)”, "Processes Logged",'OK','Information')
Invoke-Item $saveFolder.FileName
})
$btnDelete = New-Object System.Windows.Forms.Button
$btnDelete.Enabled = $false
$btnDelete.Location = New-Object System.Drawing.Size(450,500)
$btnDelete.Size = New-Object System.Drawing.Size(120,25)
$btnDelete.Text = 'Remove Task'
$btnDelete.Add_Click(
{
$taskName = $lstTasks.SelectedItem.toString()
$msgBoxInput = [System.Windows.MessageBox]::Show('Are you sure that you want to remove the task' + $taskName + "? `r`n (You cannot recover the task once removed)",'Remove Task','YesNoCancel','Warning')
switch ($msgBoxInput)
{
'yes'
{
Stop-ScheduledTask -TaskName $taskName
Disable-ScheduledTask -TaskName $taskName
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false
$lstTasks.Items.Clear()
$lstTasks.Items.Add("Scheduled Tasks:")
$lstTasks.Items.Add("")
foreach($task in Get-ScheduledTask | Sort-Object -Property TaskName)
{
$lstTasks.Items.Add($task.TaskName)
}
}
'no'
{
}
'cancel'
{
}
}
})
$btnRemove = New-Object System.Windows.Forms.Button
$btnRemove.Enabled = $false
$btnRemove.Location = New-Object System.Drawing.Size(680,500)
$btnRemove.Size = New-Object System.Drawing.Size(150,25)
$btnRemove.Text = 'Task Was Not Removed?'
$btnRemove.Add_Click(
{
$msgBoxInput = [System.Windows.MessageBox]::Show("Delete Task Manually and try again", "Task Was not removed",'OK','Warning')
$btnDelete.Enabled = $false
Invoke-Item C:\Windows\System32\Tasks
})
$main_form.Controls.Add($lstProcess)
$main_form.Controls.Add($lstTasks)
$main_form.Controls.Add($btnDelete)
$main_form.Controls.Add($btnStop)
$main_form.Controls.Add($btnRemove)
$main_form.Controls.Add($btnScheduled)
$main_form.Controls.Add($btnSave)
$main_form.Controls.Add($btnRefresh)
$main_form.ShowDialog()
}
Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled True
$cpu_threshold = 85
$sleep_interval = 5
$hit = 0
$iloop = 0
While($isRunning -ne $false)
{
$cpu = (gwmi -class Win32_Processor).LoadPercentage
$CPUPercent = @{
Name = ‘CPUPercent’
Expression =
{
$TotalSec = (New-TimeSpan -Start $_.StartTime).TotalSeconds
[Math]::Round( ($_.CPU * 100 / $TotalSec), 2)
}
}
clear-host
write-host “CPU utilization is currently at $cpu%”
If($cpu -ge $cpu_threshold)
{
$hit = $hit+1
}
if($iloop -ge 5)
{
$hit = 0
$iloop = 0
}
start-sleep $sleep_interval
if($hit -eq 3)
{
$msgBoxInput = [System.Windows.MessageBox]::Show("CPU Utilization is more than 85`% `r`n Would you like to view the top processes”,'CPU is over threshold level','YesNoCancel','Warning')
switch ($msgBoxInput)
{
'Yes'
{
displayProcess
}
'No'
{
}
'Cancel'
{
}
}
#Send-MailMessage –From CryptoMonito@mail.com –To 201477488@student.uj.ac.za –Subject “CPU Utilization is more than 85`%” –Body “CPU Utilization is more than $cpu_threshold`%” –SmtpServer smtpserver.domain.com
$hit = 0
$loop = 0
}
else
{
If($cpu -ge $cpu_threshold)
{
$Host.UI.RawUI.BackgroundColor = "DarkRed"
Write-Host "Intense Running Processes:"
Get-Process | Sort-Object -Property CPUPercent -Descending | Select-Object -Property Name, CPU, $CPUPercent, Description -First 20
}else
{
write-host “CPU utilization is below threshold level”
$Host.UI.RawUI.BackgroundColor = "DarkBlue"
}
}
$iloop = $iloop + 1
}