-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGPUStatMng.pw1
More file actions
108 lines (92 loc) · 3.78 KB
/
Copy pathGPUStatMng.pw1
File metadata and controls
108 lines (92 loc) · 3.78 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
# Automated GPU Management Script Using WMI and PowerShell Cmdlets
# Function to log messages with timestamp
function Log-Message {
param (
[string]$Message,
[switch]$Error
)
$timestamp = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss")
if ($Error) {
Write-Error "$timestamp - $Message"
} else {
Write-Host "$timestamp - $Message"
}
}
# Function to list all GPU devices and their statuses
function List-GPUDevices {
try {
$gpuDevices = Get-WmiObject -Query "SELECT * FROM Win32_VideoController" | Sort-Object -Property DeviceID
if ($gpuDevices.Count -gt 0) {
$gpuDevices | Format-Table -Property DeviceID, Name, DriverVersion, AdapterRAM, Status -AutoSize
Log-Message "GPU devices listed successfully."
} else {
Log-Message "No GPU devices found."
}
} catch {
Log-Message "Failed to retrieve GPU devices: $_" -Error
}
}
# Function to update GPU drivers
function Update-GPUDrivers {
try {
Log-Message "Starting GPU driver update."
# Use Device Management to update GPU drivers
$gpuDevices = Get-WmiObject -Query "SELECT * FROM Win32_VideoController"
foreach ($gpu in $gpuDevices) {
$deviceID = $gpu.DeviceID
Log-Message "Attempting to update driver for GPU: $($gpu.Name) ($deviceID)"
# Update driver using Windows Update
Start-Process -FilePath "cmd.exe" -ArgumentList "/c devmgmt.msc /rescan" -Wait -NoNewWindow -PassThru | Out-Null
Log-Message "Driver update attempt for GPU: $($gpu.Name)"
}
Log-Message "GPU driver update process completed."
} catch {
Log-Message "Failed to update GPU drivers: $_" -Error
}
}
# Function to reinstall GPU drivers
function Reinstall-GPUDrivers {
try {
Log-Message "Starting GPU driver reinstallation."
# Find all GPU devices and reinstall drivers
$gpuDevices = Get-WmiObject -Query "SELECT * FROM Win32_VideoController"
foreach ($gpu in $gpuDevices) {
$deviceID = $gpu.DeviceID
Log-Message "Uninstalling driver for GPU: $($gpu.Name) ($deviceID)"
# Remove GPU driver
$uninstallResult = Start-Process -FilePath "cmd.exe" -ArgumentList "/c devmgmt.msc /uninstall $deviceID" -Wait -NoNewWindow -PassThru | Out-Null
if ($uninstallResult.ExitCode -eq 0) {
Log-Message "Driver uninstalled successfully for GPU: $($gpu.Name)"
# Reinstall driver
Log-Message "Reinstalling driver for GPU: $($gpu.Name) ($deviceID)"
Start-Process -FilePath "cmd.exe" -ArgumentList "/c devmgmt.msc /rescan" -Wait -NoNewWindow -PassThru | Out-Null
} else {
Log-Message "Failed to uninstall driver for GPU: $($gpu.Name)" -Error
}
}
Log-Message "GPU driver reinstallation process completed."
} catch {
Log-Message "Failed to reinstall GPU drivers: $_" -Error
}
}
# Function to refresh driver cache
function Refresh-DriverCache {
try {
Log-Message "Starting driver cache refresh."
# Refresh drivers using Device Manager
Start-Process -FilePath "cmd.exe" -ArgumentList "/c devmgmt.msc /rescan" -Wait -NoNewWindow -PassThru | Out-Null
Log-Message "Driver cache refreshed successfully."
} catch {
Log-Message "Failed to refresh driver cache: $_" -Error
}
}
# Run automated GPU management
function Run-Automated-GPU-Management {
List-GPUDevices
Update-GPUDrivers
Reinstall-GPUDrivers
Refresh-DriverCache
Log-Message "Automated GPU management completed."
}
# Execute the automated GPU management
Run-Automated-GPU-Management