本文目录导读:

这里提供几种常见脚本语言的实现方法,分别适用于 Windows 和 Linux/Mac 系统。
Windows (PowerShell)
# 方法1:使用Win32_Processor类 $processor = Get-WmiObject Win32_Processor Write-Host "处理器版本: $($processor.Name) - $($processor.Version)" # 方法2:使用CIM获取详细信息 $cpuInfo = Get-CimInstance -ClassName CIM_Processor Write-Host "完整信息:" Write-Host "名称: $($cpuInfo.Name)" Write-Host "版本: $($cpuInfo.Version)" Write-Host "描述: $($cpuInfo.Description)" Write-Host "架构: $($cpuInfo.Architecture)"
保存为 get-cpu.ps1,右键选择“使用 PowerShell 运行”。
Windows (CMD/Batch)
@echo off wmic cpu get name, version /format:csv
或者更详细的:
@echo off echo 处理器信息: wmic cpu get name, version, description, architecture pause
Linux
#!/bin/bash # 方法1:从/proc/cpuinfo获取 cat /proc/cpuinfo | grep "model name" | head -1 | cut -d: -f2 # 方法2:使用lscpu lscpu | grep "Model name" | cut -d: -f2 # 方法3:直接显示完整信息 echo "=== 处理器信息 ===" cat /proc/cpuinfo | grep -E "model name|cpu MHz|cache size" | head -3
保存为 get-cpu.sh,运行 chmod +x get-cpu.sh && ./get-cpu.sh
macOS
#!/bin/bash # 方法1:使用sysctl sysctl -n machdep.cpu.brand_string # 方法2:获取详细信息 echo "=== macOS 处理器信息 ===" echo "品牌型号: $(sysctl -n machdep.cpu.brand_string)" echo "核心数: $(sysctl -n hw.physicalcpu)" echo "线程数: $(sysctl -n hw.logicalcpu)"
跨平台 Python 脚本
import platform
import subprocess
import sys
def get_cpu_info():
system = platform.system()
if system == "Windows":
try:
import wmi
c = wmi.WMI()
for cpu in c.Win32_Processor():
return {
'name': cpu.Name.strip(),
'version': cpu.Version,
'architecture': cpu.Architecture
}
except ImportError:
# 如果没有wmi库,使用Powershell
result = subprocess.run(
['powershell', '-Command',
'Get-CimInstance -ClassName CIM_Processor | Select-Object Name, Version'],
capture_output=True, text=True
)
return {'output': result.stdout}
elif system == "Linux":
result = subprocess.run(['lscpu'], capture_output=True, text=True)
for line in result.stdout.split('\n'):
if 'Model name' in line:
return {'name': line.split(':')[1].strip()}
elif system == "Darwin": # macOS
result = subprocess.run(
['sysctl', '-n', 'machdep.cpu.brand_string'],
capture_output=True, text=True
)
return {'name': result.stdout.strip()}
return {'error': f'Unsupported system: {system}'}
if __name__ == "__main__":
info = get_cpu_info()
print(f"处理器信息: {info}")
安装依赖:
# Windows需要 pip install wmi
跨平台 Node.js 脚本
const os = require('os');
const { execSync } = require('child_process');
function getCPUInfo() {
const platform = os.platform();
if (platform === 'win32') {
try {
const output = execSync('wmic cpu get name /format:csv', { encoding: 'utf8' });
const lines = output.trim().split('\n');
return lines[1]?.split(',')[1] || 'Unknown';
} catch (e) {
return 'Error: ' + e.message;
}
} else if (platform === 'linux') {
try {
const output = execSync('cat /proc/cpuinfo | grep "model name" | head -1',
{ encoding: 'utf8' });
return output.split(':')[1].trim();
} catch (e) {
return 'Error: ' + e.message;
}
} else if (platform === 'darwin') {
try {
const output = execSync('sysctl -n machdep.cpu.brand_string',
{ encoding: 'utf8' });
return output.trim();
} catch (e) {
return 'Error: ' + e.message;
}
} else {
return 'Unsupported platform';
}
}
console.log('处理器版本:', getCPUInfo());
使用建议
- Windows用户:直接用 PowerShell 方法最简单,无需安装额外工具
- Linux/Mac用户:直接使用终端命令,或者用 Python 脚本
- 跨平台需求:使用 Python 或 Node.js 脚本
- 管理员权限:某些系统可能需要管理员/root权限才能获取完整信息
选择适合你环境的方法即可,如果需要获取更多细节(如核心数、频率等),可以在现有脚本基础上扩展。