-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.ps1
More file actions
264 lines (246 loc) · 9.45 KB
/
script.ps1
File metadata and controls
264 lines (246 loc) · 9.45 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
#Requires -RunAsAdministrator
# Import required modules
Import-Module -Name Microsoft.PowerShell.Management -ErrorAction SilentlyContinue
Import-Module -Name Microsoft.PowerShell.Utility -ErrorAction SilentlyContinue
# checking if running as admin
function Test-Admin {
# getting the current powershell session privilege using windows builtin administrator module.
$currentUser = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
return $currentUser.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
# write log function to process output and write to log file.
function Write-Log {
param (
[string]$Message,
[string]$LogLevel = "INFO"
)
$logFile = ".\Logs\POWERBASH_$(Get-Date -Format 'yyyyMMdd').log"
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$logMessage = "[$timestamp] [$LogLevel] $Message"
if (-not (Test-Path (Split-Path $logFile))) {
New-Item -ItemType Directory -Path (Split-Path $logFile) -Force | Out-Null
}
Add-Content -Path $logFile -Value $logMessage
switch ($LogLevel) {
"ERROR" { Write-Host $logMessage -ForegroundColor Red }
"WARNING" { Write-Host $logMessage -ForegroundColor Yellow }
default { Write-Host $logMessage -ForegroundColor Green }
}
}
# Function to kill a process by name process name
function Kill-Process {
param (
[string]$ProcessName
)
try {
$processes = Get-Process -Name $ProcessName -ErrorAction Stop
if ($processes) {
foreach ($process in $processes) {
Stop-Process -Id $process.Id -Force -ErrorAction Stop
Write-Log "Terminated process: $($process.Name) (PID: $($process.Id))"
}
} else {
Write-Log "No process found with name: $ProcessName" -LogLevel "ERROR"
}
} catch {
Write-Log "Error terminating process: $_" -LogLevel "ERROR"
}
}
# Function for system health checks for CPU and Memory
function Get-SystemHealth {
try {
# query disk information across different Windows versions.
$cpu = Get-CimInstance Win32_Processor | Select-Object -ExpandProperty LoadPercentage
$memory = Get-CimInstance Win32_OperatingSystem
# Rounding off two decimal places using .NET System.Math Class.
$totalMemory = [math]::Round($memory.TotalVisibleMemorySize / 1MB, 2)
$freeMemory = [math]::Round($memory.FreePhysicalMemory / 1MB, 2)
$usedMemoryPercent = [math]::Round((($totalMemory - $freeMemory) / $totalMemory) * 100, 2)
Write-Log "CPU Usage: $cpu%"
Write-Log "Memory Usage: $usedMemoryPercent% ($freeMemory GB free of $totalMemory GB)"
} catch {
Write-Log "Error retrieving system health: $_" -LogLevel "ERROR"
}
}
#function get free disk space the accroding to the specific partion
function Get-FreeSpace {
param (
[string]$DriveLetter = "C:"
)
try {
$disk = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DeviceID='$DriveLetter'" -ErrorAction Stop
$freeSpaceGB = [math]::Round($disk.FreeSpace / 1GB, 2)
$totalSpaceGB = [math]::Round($disk.Size / 1GB, 2)
$thresholdGB = 30
Write-Log "Drive $DriveLetter - Free Space: $freeSpaceGB GB / Total: $totalSpaceGB GB"
if ($freeSpaceGB -lt $thresholdGB) {
Write-Log "Warning: Free space is below $thresholdGB GB!" -LogLevel "WARNING"
}
} catch {
Write-Log "Error checking disk space: $_" -LogLevel "ERROR"
}
}
# Function for PDF password protection using QPDF
function Protect-PDF {
param (
[string]$FilePath,
[string]$Password
)
try {
if (-not (Test-Path $FilePath)) {
Write-Log "PDF file not found: $FilePath" -LogLevel "ERROR"
return
}
# the below code creates a copy of the password protected and encrypted pdf file based on AES-256.
$outputFile = $FilePath -replace '\.pdf$', '_protected.pdf'
& qpdf --encrypt $Password $Password 256 -- $FilePath $outputFile
if ($LASTEXITCODE -eq 0) {
Write-Log "PDF protected successfully: $outputFile"
} else {
Write-Log "Failed to protect PDF"
}
} catch {
Write-Log "Error protecting PDF: $_" -LogLevel "ERROR"
}
}
# Function to retrieve file hash using default cmdlet.
function Get-FileHashValue {
param (
[string]$FilePath
)
try {
if (-not (Test-Path $FilePath)) {
Write-Log "File not found: $FilePath"
return
}
$hash = Get-FileHash -Path $FilePath -Algorithm SHA256 -ErrorAction Stop
Write-Log "File: $FilePath"
Write-Log "SHA256 Hash: $($hash.Hash)"
} catch {
Write-Log "Error calculating file hash: $_" -LogLevel "ERROR"
}
}
# Function for get IP Info lookup using a free API (ip-api)
function Get-IPInfo {
param (
[string]$IPAddress
)
try {
$response = Invoke-RestMethod -Uri "http://ip-api.com/json/$IPAddress" -ErrorAction Stop
if ($response.status -eq "success") {
Write-Log "IP: $IPAddress"
Write-Log "Status: $($response.status)"
Write-Log "Location: $($response.city), $($response.regionName), $($response.country)"
Write-Log "ISP: $($response.isp)"
Write-Log "Organisation: $($response.org)"
Write-Log "Longitude and Latitude Based on IP: $($response.lon), $($response.lat)"
} else {
Write-Log "Failed to retrieve location for IP: $IPAddress" -LogLevel "ERROR"
}
} catch {
Write-Log "Error fetching IP location: $_" -LogLevel "ERROR"
}
}
# Function to get live network stats
function Get-NetworkStats {
try {
$adapters = Get-NetAdapter -Physical | Where-Object { $_.Status -eq "Up" }
foreach ($adapter in $adapters) {
$stats = Get-NetAdapterStatistics -Name $adapter.Name
Write-Log "Network Adapter: $($adapter.Name)" -LogLevel "INFO"
Write-Log "Status: $($adapter.Status)" -LogLevel "INFO"
Write-Log "Bytes Sent: $([math]::Round($stats.SentBytes / 1MB, 2)) MB" -LogLevel "INFO"
Write-Log "Bytes Received: $([math]::Round($stats.ReceivedBytes / 1MB, 2)) MB" -LogLevel "INFO"
}
} catch {
Write-Log "Error retrieving network stats: $_" -LogLevel "ERROR"
}
}
# Function to backup files
function Backup-Files {
param (
[string]$SourcePath,
[string]$BackupPath = "C:\Backups\Backup_$(Get-Date -Format 'yyyyMMdd_HHmmss').zip"
)
try {
if (-not (Test-Path $SourcePath)) {
Write-Log "Source path not found: $SourcePath" -LogLevel "ERROR"
return
}
if (-not (Test-Path (Split-Path $BackupPath))) {
New-Item -ItemType Directory -Path (Split-Path $BackupPath) -Force | Out-Null
}
Compress-Archive -Path $SourcePath -DestinationPath $BackupPath -Force
Write-Log "Backup created: $BackupPath" -LogLevel "INFO"
} catch {
Write-Log "Error creating backup: $_" -LogLevel "ERROR"
}
}
# Menu
function Show-Menu {
Write-Host "`n== System Utility Script ==" -ForegroundColor Cyan
Write-Host "1. Kill Process"
Write-Host "2. System Health Check"
Write-Host "3. Free Space Monitoring"
Write-Host "4. PDF Password Protection"
Write-Host "5. Retrieve File Hash"
Write-Host "6. IP Location Lookup"
Write-Host "7. Get Network Stats"
Write-Host "8. Backup Files"
Write-Host "9. Exit"
Write-Host "`nEnter your choice (1-9): " -NoNewline
}
#logic to check privilege.
if (-not (Test-Admin)) {
Write-Log "`nThis script requires administrative privileges. Please run as Administrator." -LogLevel "ERROR"
exit
}
while ($true) {
Show-Menu
$choice = Read-Host
switch ($choice) {
"1" {
$processName = Read-Host "Enter process name to kill (e.g., notepad)"
Kill-Process -ProcessName $processName
}
"2" {
Get-SystemHealth
}
"3" {
$drive = Read-Host "Enter drive letter to check (default C:)"
if (-not $drive) { $drive = "C:" }
Get-FreeSpace -DriveLetter $drive
}
"4" {
$filePath = Read-Host "Enter full path to PDF file"
$password = Read-Host "Enter password for PDF"
Protect-PDF -FilePath $filePath -Password $password
}
"5" {
$filePath = Read-Host "Enter full path to file for hash"
Get-FileHashValue -FilePath $filePath
}
"6" {
$ipAddress = Read-Host "Enter IP address for location lookup"
Get-IPInfo -IPAddress $ipAddress
}
"7" {
Get-NetworkStats
}
"8"{
$sourcePath = Read-Host "Enter source path to backup"
$backupPath = Read-Host "Enter backup destination (default: C:\Backups\Backup_<timestamp>.zip)"
if (-not $backupPath) { $backupPath = "C:\Backups\Backup_$(Get-Date -Format 'yyyyMMdd_HHmmss').zip" }
Backup-Files -SourcePath $sourcePath -BackupPath $backupPath
}
"9"{
Write-Host "Exiting script..." -ForegroundColor Cyan
exit
}
default {
Write-Host "Invalid choice. Please select 1-9." -ForegroundColor Red
}
}
Write-Host "`nPress Enter to continue..."
Read-Host
}