-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFreeRAM.ps1
136 lines (128 loc) · 5.48 KB
/
FreeRAM.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
$host.ui.RawUI.BackgroundColor = "Black"
$host.ui.RawUI.ForegroundColor = "White"
Clear-Host
# Vérifier les privilèges administratifs
function Check-Admin {
Write-Host "Checking for Administrative Privileges..."
Start-Sleep -Seconds 3
$currentUser = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object Security.Principal.WindowsPrincipal($currentUser)
if (-not $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Start-Process powershell -Verb runAs -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`""
exit
}
}
Check-Admin
# Script to free up RAM at specified intervals
param(
[ValidateSet("2", "5", "10", "15", "uninstall", "status")]
[string]$option
)
# Function to free up RAM
function Free-RAM {
Write-Host "Freeing up RAM..."
# Exclude critical processes
$excludedProcesses = @("explorer.exe", "csrss.exe", "winlogon.exe", "services.exe", "svchost.exe")
try {
Get-Process | Where-Object { $_.ProcessName -notin $excludedProcesses } | ForEach-Object {
$_.MinWorkingSet = [System.IntPtr]::Zero
$_.MaxWorkingSet = [System.IntPtr]::Zero
}
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
[System.GC]::Collect()
}
catch {
Write-Host "An error occurred while freeing up RAM: $_" -ForegroundColor Red
}
}
# Function to create the scheduled task
function Create-ScheduledTask {
param (
[int]$minutes
)
try {
$taskName = "FreeRAM"
$taskAction = New-ScheduledTaskAction -Execute 'PowerShell.exe' -Argument "-NoProfile -WindowStyle Hidden -File `"$PSScriptRoot\FreeRAM.ps1`" -option $minutes"
$taskTrigger = New-ScheduledTaskTrigger -AtStartup
$taskPrincipal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount
Register-ScheduledTask -TaskName $taskName -Action $taskAction -Trigger $taskTrigger -Principal $taskPrincipal
Write-Host "Scheduled task created to free RAM every $minutes minutes."
}
catch {
Write-Host "An error occurred while creating the scheduled task: $_" -ForegroundColor Red
}
}
# Function to uninstall the scheduled task
function Uninstall-ScheduledTask {
$taskName = "FreeRAM"
try {
if (Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue) {
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false
Write-Host "Scheduled task '$taskName' has been uninstalled."
} else {
Write-Host "No scheduled task named '$taskName' found."
}
}
catch {
Write-Host "An error occurred while uninstalling the scheduled task: $_" -ForegroundColor Red
}
}
# Function to show the status of the scheduled task
function Show-Status {
$taskName = "FreeRAM"
try {
$task = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue
if ($task) {
Write-Host "Scheduled task '$taskName' is installed and configured."
} else {
Write-Host "Scheduled task '$taskName' is not found."
}
}
catch {
Write-Host "An error occurred while checking the status of the scheduled task: $_" -ForegroundColor Red
}
}
# Display the menu
function Show-Menu {
Clear-Host
Write-Host "============================================="
Write-Host "| LexBoosT Free RAM Management |"
Write-Host "============================================="
Write-Host "| 1. Set interval to 2 minutes |" -ForegroundColor Blue
Write-Host "| 2. Set interval to 5 minutes |" -ForegroundColor Blue
Write-Host "| 3. Set interval to 10 minutes |" -ForegroundColor Blue
Write-Host "| 4. Set interval to 15 minutes |" -ForegroundColor Blue
Write-Host "| 5. Uninstall scheduled task |" -ForegroundColor Magenta
Write-Host "| 6. Show status |" -ForegroundColor Yellow
Write-Host "| 0. Exit |" -ForegroundColor Red
Write-Host "============================================="
$choice = Read-Host "Enter your choice"
switch ($choice) {
1 { Create-ScheduledTask -minutes 2; Start-Sleep -Seconds 3; Clear-Host; Show-Menu }
2 { Create-ScheduledTask -minutes 5; Start-Sleep -Seconds 3; Clear-Host; Show-Menu }
3 { Create-ScheduledTask -minutes 10; Start-Sleep -Seconds 3; Clear-Host; Show-Menu }
4 { Create-ScheduledTask -minutes 15; Start-Sleep -Seconds 3; Clear-Host; Show-Menu }
5 { Uninstall-ScheduledTask; Start-Sleep -Seconds 3; Clear-Host; Show-Menu }
6 { Show-Status; Start-Sleep -Seconds 3; Clear-Host; Show-Menu }
0 { Write-Host "Goodbye!"; Start-Sleep -Seconds 3; Exit }
default { Write-Host "Invalid choice. Please select an option from 0 to 6."; Start-Sleep -Seconds 3; Clear-Host; Show-Menu }
}
}
# Run the menu if no option is passed
if (-not $option) {
Show-Menu
} else {
switch ($option) {
"uninstall" { Uninstall-ScheduledTask; exit }
"status" { Show-Status; exit }
default {
$minutes = [int]$option
$seconds = $minutes * 60
while ($true) {
Free-RAM
Start-Sleep -Seconds $seconds
}
}
}
}