本文目录导读:

是的,实用脚本可以获取显卡信息,具体实现方式取决于你使用的操作系统和脚本语言,以下是几种常见的方法:
Windows 系统
PowerShell 脚本
# 使用 WMI 查询显卡信息 Get-WmiObject Win32_VideoController | Select-Object Name, DriverVersion, AdapterRAM, CurrentRefreshRate # 使用更现代的 CIM 方法 Get-CimInstance Win32_VideoController | Format-List Name, DriverVersion, AdapterRAM, CurrentHorizontalResolution, CurrentVerticalResolution
Python 脚本(需要安装 wmi 库)
import wmi
c = wmi.WMI()
for gpu in c.Win32_VideoController():
print(f"名称: {gpu.Name}")
print(f"驱动版本: {gpu.DriverVersion}")
print(f"显存: {gpu.AdapterRAM / 1024**3:.2f} GB")
print("-" * 50)
Linux 系统
Shell 脚本(使用 lspci)
#!/bin/bash # 获取 NVIDIA 显卡信息 lspci | grep -i vga | grep -i nvidia # 更详细的信息(需要安装 nvidia-smi) nvidia-smi --query-gpu=name,memory.total,driver_version --format=csv,noheader # 通用方法 lspci -v | grep -A 10 "VGA compatible"
Python 脚本(使用 pynvml 库获取 NVIDIA 信息)
import pynvml
pynvml.nvmlInit()
device_count = pynvml.nvmlDeviceGetCount()
for i in range(device_count):
handle = pynvml.nvmlDeviceGetHandleByIndex(i)
name = pynvml.nvmlDeviceGetName(handle)
memory = pynvml.nvmlDeviceGetMemoryInfo(handle)
print(f"GPU {i}: {name}")
print(f"总显存: {memory.total / 1024**3:.2f} GB")
print(f"已用显存: {memory.used / 1024**3:.2f} GB")
pynvml.nvmlShutdown()
macOS 系统
Shell 脚本
#!/bin/bash # 使用 system_profiler system_profiler SPHardwareDataType | grep -i "graphics" system_profiler SPDisplaysDataType # 直接查看显卡详情 system_profiler SPDisplaysDataType | grep -E "Chipset Model|VRAM|Vendor"
跨平台 Python 脚本(使用 pyadl 或 GPUtil)
import GPUtil
gpus = GPUtil.getGPUs()
for gpu in gpus:
print(f"GPU: {gpu.name}")
print(f"显存总量: {gpu.memoryTotal} MB")
print(f"显存使用量: {gpu.memoryUsed} MB")
print(f"GPU 使用率: {gpu.load * 100:.1f}%")
print(f"温度: {gpu.temperature}°C")
print("-" * 30)
完整示例:Windows 批处理脚本
@echo off echo 显卡信息: wmic path win32_videocontroller get name,driverversion,adapterram echo. echo 当前分辨率: wmic path win32_videocontroller get currenthorizontalresolution,currentverticalresolution
常见用途
这些脚本可以用于:
- 服务器监控:定期检查 GPU 状态和温度
- 性能测试:记录显卡在不同负载下的表现
- 系统信息收集:生成硬件报告
- 自动化诊断:检测显卡驱动版本和兼容性问题
选择哪种方法取决于你的具体需求和运行环境,如果需要获取显存占用、温度、风扇转速等更详细的信息,建议使用厂商提供的 SDK(如 NVIDIA 的 NVML 或 AMD 的 ADL)。