-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSystemHealthManager.ps1
More file actions
418 lines (388 loc) · 20.4 KB
/
SystemHealthManager.ps1
File metadata and controls
418 lines (388 loc) · 20.4 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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# SystemHealthManager.ps1
# Autor: Daniel Vocurca Frade
# Data: 14/04/2025
# Descrição: Ferramenta interativa avançada CoyMenu
# Verifica se está rodando como administrador
function Test-Admin {
$currentUser = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
return $currentUser.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
# Tenta abrir no Windows Terminal como administrador, se disponível, ou no PowerShell padrão
if (-not (Test-Admin)) {
Write-Host "⚠️ Este script requer privilégios administrativos!" -ForegroundColor Yellow
Write-Host "🔧 Tentando reiniciar como administrador..." -ForegroundColor Cyan
Start-Sleep -Seconds 2
$scriptPath = $MyInvocation.MyCommand.Path
# Tenta usar o Windows Terminal (wt.exe), se instalado
if (Get-Command "wt.exe" -ErrorAction SilentlyContinue) {
Start-Process "wt.exe" -Verb RunAs -ArgumentList "powershell -NoProfile -ExecutionPolicy Bypass -File `"$scriptPath`""
} else {
Start-Process powershell -Verb RunAs -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$scriptPath`""
}
exit
}
# Define configurações de console para suportar cores
$Host.UI.RawUI.BackgroundColor = "Black"
$Host.UI.RawUI.ForegroundColor = "White"
Clear-Host
# Função para exibir cabeçalho estilizado
function Show-Header {
Clear-Host
Write-Host "╔════════════════════════════════════════════════════╗" -ForegroundColor Cyan
Write-Host "║ ║" -ForegroundColor Cyan
Write-Host "║ ____ ___ _ _ /\/\ ___ _ __ _ _ ║" -ForegroundColor Yellow
Write-Host "║ / ___/ _ \| | | | / \ / _ \ '_ \| | | | ║" -ForegroundColor Yellow
Write-Host "║ | (_| (_) | |_| |/ /\/\ \ __/ | | | |_| | ║" -ForegroundColor Yellow
Write-Host "║ \___\___/ \__ /\/ \/\___|_| |_|__,_| v2.1 ║" -ForegroundColor Yellow
Write-Host "║ |___/ ║" -ForegroundColor Yellow
Write-Host "║ ║" -ForegroundColor Cyan
Write-Host "╚════════════════════════════════════════════════════╝" -ForegroundColor Cyan
Write-Host " 🚀 Monitoramento e Otimização com Estilo 🚀" -ForegroundColor Green
Write-Host "──────────────────────────────────────────────────────" -ForegroundColor Cyan
Write-Host ""
}
# Função para animação de carregamento
function Show-Loading {
param ($message)
Write-Host "$message " -NoNewline -ForegroundColor Yellow
for ($i = 0; $i -lt 3; $i++) {
Write-Host "." -NoNewline -ForegroundColor Green
Start-Sleep -Milliseconds 300
}
Write-Host ""
}
# Função para obter métricas do sistema
function Get-SystemMetrics {
$cpuUsage = (Get-CimInstance Win32_PerfFormattedData_PerfOS_Processor | Where-Object { $_.Name -eq "_Total" }).PercentProcessorTime
$memory = Get-CimInstance Win32_OperatingSystem
$memoryUsed = [math]::Round(($memory.TotalVisibleMemorySize - $memory.FreePhysicalMemory) / 1024 / 1024, 2)
$memoryTotal = [math]::Round($memory.TotalVisibleMemorySize / 1024 / 1024, 2)
$disk = Get-PSDrive -Name "C" -ErrorAction SilentlyContinue
$diskFree = [math]::Round($disk.Free / 1GB, 2)
$diskTotal = [math]::Round(($disk.Used + $disk.Free) / 1GB, 2)
$netStats = Get-NetAdapterStatistics -ErrorAction SilentlyContinue
$netSent = [math]::Round(($netStats | Measure-Object -Property SentBytes -Sum).Sum / 1MB, 2)
$netReceived = [math]::Round(($netStats | Measure-Object -Property ReceivedBytes -Sum).Sum / 1MB, 2)
return [PSCustomObject]@{
CPUUsage = $cpuUsage
MemoryUsed = $memoryUsed
MemoryTotal = $memoryTotal
DiskFree = $diskFree
DiskTotal = $diskTotal
NetSent = $netSent
NetReceived = $netReceived
}
}
# Função para exibir métricas com design aprimorado
function Show-Metrics {
param ($metrics)
Write-Host "🌟 Métricas do Sistema 🌟" -ForegroundColor Yellow
Write-Host "────────────────────────" -ForegroundColor Cyan
Write-Host "🖥️ CPU: " -NoNewline -ForegroundColor Magenta
Write-Host "$($metrics.CPUUsage)% " -NoNewline -ForegroundColor White
Write-Host ("█" * [math]::Round($metrics.CPUUsage / 5)) -ForegroundColor Red
Write-Host "💾 Memória: " -NoNewline -ForegroundColor Magenta
Write-Host "$($metrics.MemoryUsed)/$($metrics.MemoryTotal) GB " -NoNewline -ForegroundColor White
Write-Host ("█" * [math]::Round(($metrics.MemoryUsed / $metrics.MemoryTotal) * 20)) -ForegroundColor Blue
Write-Host "📀 Disco: " -NoNewline -ForegroundColor Magenta
Write-Host "$($metrics.DiskFree)/$($metrics.DiskTotal) GB " -NoNewline -ForegroundColor White
Write-Host ("█" * [math]::Round(($metrics.DiskFree / $metrics.DiskTotal) * 20)) -ForegroundColor Green
Write-Host "🌐 Rede (Enviado/Recebido): " -NoNewline -ForegroundColor Magenta
Write-Host "$($metrics.NetSent)/$($metrics.NetReceived) MB" -ForegroundColor White
Write-Host "────────────────────────" -ForegroundColor Cyan
Write-Host ""
}
# Função para detectar processos problemáticos
function Get-ProblematicProcesses {
$processes = Get-Process | Where-Object { $_.CPU -gt 100 -or $_.WorkingSet64 / 1MB -gt 500 } |
Select-Object Name, @{N='CPU';E={[math]::Round($_.CPU, 2)}}, @{N='MemoriaMB';E={[math]::Round($_.WorkingSet64 / 1MB, 2)}}
return $processes
}
# Função para otimizar o sistema
function Optimize-System {
Show-Loading "🔧 Preparando otimização avançada do sistema"
$confirm = Read-Host "⚠️ Isso vai limpar arquivos temporários, otimizar memória, ajustar serviços, reiniciar o Explorer e desfragmentar o disco (se necessário). Continuar? (S/N)"
if ($confirm -eq 'S' -or $confirm -eq 's') {
Show-Loading "🔧 Iniciando otimização avançada"
try {
Write-Host "🗑️ Limpando arquivos temporários..." -ForegroundColor Yellow
Remove-Item "$env:TEMP\*" -Recurse -Force -ErrorAction Stop
Remove-Item "C:\Windows\Temp\*" -Recurse -Force -ErrorAction Stop
Write-Host "✅ Arquivos temporários limpos!" -ForegroundColor Green
} catch {
Write-Host "⚠️ Erro ao limpar arquivos temporários: $($_.Exception.Message)" -ForegroundColor Red
}
try {
Write-Host "📜 Limpando logs de eventos antigos..." -ForegroundColor Yellow
Get-EventLog -LogName * | ForEach-Object { Clear-EventLog -LogName $_.Log -ErrorAction SilentlyContinue }
Write-Host "✅ Logs de eventos limpos!" -ForegroundColor Green
} catch {
Write-Host "⚠️ Erro ao limpar logs: $($_.Exception.Message)" -ForegroundColor Red
}
try {
Write-Host "💾 Liberando memória ociosa..." -ForegroundColor Yellow
$os = Get-CimInstance Win32_OperatingSystem
$freeMemoryBefore = $os.FreePhysicalMemory
$dll = Add-Type -Name "WinAPI" -MemberDefinition '[DllImport("psapi.dll")] public static extern bool EmptyWorkingSet(IntPtr hProcess);' -PassThru
Get-Process | ForEach-Object { if ($_.Handle) { $dll::EmptyWorkingSet($_.Handle) | Out-Null } }
$os = Get-CimInstance Win32_OperatingSystem
$freeMemoryAfter = $os.FreePhysicalMemory
Write-Host "✅ Memória liberada: $([math]::Round(($freeMemoryAfter - $freeMemoryBefore) / 1024, 2)) MB" -ForegroundColor Green
} catch {
Write-Host "⚠️ Erro ao liberar memória: $($_.Exception.Message)" -ForegroundColor Red
}
try {
Write-Host "⚙️ Ajustando serviços desnecessários..." -ForegroundColor Yellow
$spooler = Get-Service -Name "Spooler" -ErrorAction SilentlyContinue
if ($spooler -and $spooler.Status -eq "Running") {
$stopSpooler = Read-Host "🖨️ Deseja parar o serviço de spooler de impressão? (S/N)"
if ($stopSpooler -eq 'S' -or $stopSpooler -eq 's') {
Stop-Service -Name "Spooler" -Force -ErrorAction Stop
Set-Service -Name "Spooler" -StartupType Manual
Write-Host "✅ Serviço de spooler parado e configurado como manual!" -ForegroundColor Green
}
}
} catch {
Write-Host "⚠️ Erro ao ajustar serviços: $($_.Exception.Message)" -ForegroundColor Red
}
try {
Write-Host "🖥️ Reiniciando Explorer..." -ForegroundColor Yellow
Stop-Process -Name "Explorer" -Force -ErrorAction Stop
Start-Sleep -Seconds 1
Start-Process "Explorer" -ErrorAction Stop
Write-Host "✅ Explorer reiniciado!" -ForegroundColor Green
} catch {
Write-Host "⚠️ Erro ao reiniciar Explorer: $($_.Exception.Message)" -ForegroundColor Red
}
try {
Write-Host "📀 Verificando necessidade de desfragmentação..." -ForegroundColor Yellow
# Check and start required services
$services = @("vds", "StorSvc")
foreach ($service in $services) {
$svc = Get-Service -Name $service -ErrorAction SilentlyContinue
if ($svc) {
if ($svc.Status -ne "Running") {
Write-Host "🔧 Iniciando serviço $service..." -ForegroundColor Yellow
Start-Service -Name $service -ErrorAction Stop
}
} else {
Write-Host "⚠️ Serviço $service não encontrado!" -ForegroundColor Red
}
}
# Validate the volume
$volume = Get-Volume -DriveLetter "C" -ErrorAction SilentlyContinue
if (-not $volume) {
Write-Host "❌ Volume C: não encontrado!" -ForegroundColor Red
return
}
# Check if the drive is an SSD
$disk = Get-Disk | Where-Object { $_.Number -eq (Get-Partition -DriveLetter "C").DiskNumber } -ErrorAction SilentlyContinue
if ($disk -and $disk.Model -match "SSD") {
Write-Host "ℹ️ Disco SSD detectado, desfragmentação ignorada." -ForegroundColor Cyan
return
}
# Proceed with defragmentation analysis
if ($volume.DriveType -eq "Fixed") {
try {
$defrag = (Optimize-Volume -DriveLetter "C" -Analyze -Verbose) | Out-String
if ($defrag -match "Fragmentation\s*:\s*(\d+)%") {
$fragmentation = [int]$Matches[1]
if ($fragmentation -gt 10) {
Write-Host "🔧 Desfragmentando disco (fragmentação: $fragmentation%)..." -ForegroundColor Yellow
Optimize-Volume -DriveLetter "C" -Defrag -Verbose
Write-Host "✅ Disco desfragmentado!" -ForegroundColor Green
} else {
Write-Host "✅ Desfragmentação não necessária (fragmentação: $fragmentation%)" -ForegroundColor Green
}
} else {
Write-Host "⚠️ Não foi possível determinar o nível de fragmentação." -ForegroundColor Yellow
}
} catch {
Write-Host "⚠️ Erro ao analisar/desfragmentar disco: $($_.Exception.Message)" -ForegroundColor Red
}
} else {
Write-Host "ℹ️ Volume não suportado para desfragmentação." -ForegroundColor Cyan
}
} catch {
Write-Host "⚠️ Erro geral ao verificar desfragmentação: $($_.Exception.Message)" -ForegroundColor Red
}
Write-Host "✅ Otimização concluída com sucesso!" -ForegroundColor Green
} else {
Write-Host "❌ Otimização cancelada!" -ForegroundColor Yellow
}
}
# Função de monitoramento contínuo
function Start-ContinuousMonitoring {
Show-Header
Write-Host "🚨 Monitoramento contínuo iniciado (Ctrl+C para sair)" -ForegroundColor Yellow
Write-Host "──────────────────────────────────────────────────────" -ForegroundColor Cyan
while ($true) {
$metrics = Get-SystemMetrics
Show-Metrics -metrics $metrics
if ($metrics.CPUUsage -gt 80) {
Write-Host "⚠️ ALERTA: CPU > 80%!" -ForegroundColor Red
[Console]::Beep(1000, 500)
}
if ($metrics.MemoryUsed / $metrics.MemoryTotal -gt 0.9) {
Write-Host "⚠️ ALERTA: Memória quase esgotada!" -ForegroundColor Red
[Console]::Beep(1000, 500)
}
if ($metrics.DiskFree / $metrics.DiskTotal -lt 0.1) {
Write-Host "⚠️ ALERTA: Disco quase cheio!" -ForegroundColor Red
[Console]::Beep(1000, 500)
}
Start-Sleep -Seconds 5
Write-Host "🔄 Atualizando..." -ForegroundColor Cyan
}
}
# Função de backup
function Backup-Config {
Show-Loading "💾 Criando backup de configurações"
$backupPath = "$env:USERPROFILE\Desktop\SystemHealthBackup_$(Get-Date -Format 'yyyyMMdd_HHmmss')"
New-Item -Path $backupPath -ItemType Directory -Force | Out-Null
Get-ChildItem Env: | Export-Csv "$backupPath\EnvVars.csv" -NoTypeInformation
reg export HKCU\Software\Microsoft\Windows\CurrentVersion\Run "$backupPath\Run.reg" /y 2>$null
Write-Host "✅ Backup salvo em: $backupPath" -ForegroundColor Green
Pause
}
# Função de exportação de relatório
function Export-Report {
Show-Loading "📑 Gerando relatório"
$metrics = Get-SystemMetrics
$processes = Get-ProblematicProcesses
$reportPath = "$env:USERPROFILE\Desktop\SystemHealthReport_$(Get-Date -Format 'yyyyMMdd_HHmmss').csv"
$report = [PSCustomObject]@{
Timestamp = Get-Date
CPUUsage = $metrics.CPUUsage
MemoryUsedGB = $metrics.MemoryUsed
MemoryTotalGB= $metrics.MemoryTotal
DiskFreeGB = $metrics.DiskFree
DiskTotalGB = $metrics.DiskTotal
NetSentMB = $metrics.NetSent
NetReceivedMB= $metrics.NetReceived
}
$report | Export-Csv $reportPath -NoTypeInformation
if ($processes) {
$processes | Export-Csv "$reportPath.append.csv" -NoTypeInformation
Write-Host "✅ Relatório com processos salvo em: $reportPath e $reportPath.append.csv" -ForegroundColor Green
} else {
Write-Host "✅ Relatório salvo em: $reportPath" -ForegroundColor Green
}
Pause
}
# Função de teste de rede
function Test-Network {
Show-Loading "🌐 Testando conexão de rede"
$pingGoogle = Test-Connection -ComputerName "google.com" -Count 4 -ErrorAction SilentlyContinue
$pingCloudflare = Test-Connection -ComputerName "1.1.1.1" -Count 4 -ErrorAction SilentlyContinue
Write-Host "────────────────────────" -ForegroundColor Cyan
if ($pingGoogle) {
$avgLatencyGoogle = [math]::Round(($pingGoogle | Measure-Object -Property ResponseTime -Average).Average, 2)
Write-Host "🌍 Google: $avgLatencyGoogle ms" -ForegroundColor Green
} else {
Write-Host "❌ Falha ao pingar Google" -ForegroundColor Red
}
if ($pingCloudflare) {
$avgLatencyCloudflare = [math]::Round(($pingCloudflare | Measure-Object -Property ResponseTime -Average).Average, 2)
Write-Host "☁️ Cloudflare: $avgLatencyCloudflare ms" -ForegroundColor Green
} else {
Write-Host "❌ Falha ao pingar Cloudflare" -ForegroundColor Red
}
Write-Host "────────────────────────" -ForegroundColor Cyan
Pause
}
# Função de saída estilizada
function Exit-Program {
Show-Header
Write-Host "👋 Saindo com estilo..." -ForegroundColor Yellow
$animation = @("🚀", "✨", "🌟", "💫")
for ($i = 0; $i -lt 5; $i++) {
Write-Host "`r$($animation[$i % 4]) Encerrando" -NoNewline -ForegroundColor Green
Start-Sleep -Milliseconds 200
}
Write-Host "`r✅ Programa encerrado! " -ForegroundColor Green
return $true
}
# Menu interativo com design incrível
function Show-Menu {
Show-Header
Write-Host "🎯 Escolha uma opção:" -ForegroundColor Yellow
Write-Host "──────────────────────────────────────────────────────" -ForegroundColor Cyan
Write-Host " [1] 🌟 Exibir métricas do sistema" -ForegroundColor Magenta
Write-Host " [2] ⚠️ Ver processos problemáticos" -ForegroundColor Magenta
Write-Host " [3] 🔧 Otimizar sistema" -ForegroundColor Magenta
Write-Host " [4] 🔍 Verificar atualizações pendentes" -ForegroundColor Magenta
Write-Host " [5] 🚨 Iniciar monitoramento contínuo" -ForegroundColor Magenta
Write-Host " [6] 💾 Fazer backup de configurações" -ForegroundColor Magenta
Write-Host " [7] 📑 Exportar relatório" -ForegroundColor Magenta
Write-Host " [8] 🌐 Testar conexão de rede" -ForegroundColor Magenta
Write-Host " [9] 👋 Sair" -ForegroundColor Magenta
Write-Host "──────────────────────────────────────────────────────" -ForegroundColor Cyan
Write-Host ""
}
# Loop principal
$exitFlag = $false
do {
Show-Menu
$choice = Read-Host "Digite sua escolha (1-9)"
switch ($choice) {
"1" {
Show-Header
$metrics = Get-SystemMetrics
Show-Metrics -metrics $metrics
Pause
}
"2" {
Show-Header
$processes = Get-ProblematicProcesses
if ($processes) {
Write-Host "⚠️ Processos com alto consumo:" -ForegroundColor Red
Write-Host "────────────────────────" -ForegroundColor Cyan
$processes | Format-Table -AutoSize
$kill = Read-Host "🔪 Deseja encerrar algum processo? (Nome ou 'N')"
if ($kill -ne 'N' -and $kill -ne 'n') {
Stop-Process -Name $kill -Force -ErrorAction SilentlyContinue
Write-Host "✅ Processo encerrado!" -ForegroundColor Green
}
} else {
Write-Host "✅ Nenhum processo problemático detectado!" -ForegroundColor Green
}
Pause
}
"3" {
Show-Header
Optimize-System
Pause
}
"4" {
Show-Header
Show-Loading "🔍 Verificando atualizações"
$updates = (New-Object -ComObject Microsoft.Update.Session).CreateUpdateSearcher().Search("IsInstalled=0").Updates
if ($updates.Count -gt 0) {
Write-Host "⚠️ $($updates.Count) atualizações pendentes!" -ForegroundColor Red
} else {
Write-Host "✅ Sistema atualizado!" -ForegroundColor Green
}
Pause
}
"5" {
Start-ContinuousMonitoring
}
"6" {
Backup-Config
}
"7" {
Export-Report
}
"8" {
Test-Network
}
"9" {
$exitFlag = Exit-Program
}
default {
Write-Host "❌ Opção inválida, tente novamente!" -ForegroundColor Red
Pause
}
}
} while (-not $exitFlag)