利用 Windows Management Instrumentation (WMI) 可在 JavaScript 中查詢客戶端詳細硬件與系統信息(CPU、內存、磁盤、顯卡、網卡、BIOS 等)。僅在 IE/HTA/Node.js (wmic) 環境有效,現代瀏覽器(Chrome/Firefox/Edge)因安全策略不支援 ActiveXObject。本文提供完整封裝函數、常用 WMI 類列表與實戰示例。
wmic 或 node-wmi)function getSysInfo() {
try {
var locator = new ActiveXObject("WbemScripting.SWbemLocator");
var service = locator.ConnectServer("."); // "." 表示本機
// 1. CPU 信息
var cpu = new Enumerator(service.ExecQuery("SELECT * FROM Win32_Processor")).item();
var cpuType = cpu.Name;
var hostName = cpu.SystemName;
// 2. 內存信息(可能多條插槽)
var memory = new Enumerator(service.ExecQuery("SELECT * FROM Win32_PhysicalMemory"));
var mem = [];
for (var i = 0; !memory.atEnd(); memory.moveNext()) {
var item = memory.item();
mem[i++] = {
capacity: Math.round(item.Capacity / 1024 / 1024), // MB
speed: item.Speed // MHz
};
}
// 3. 系統信息
var system = new Enumerator(service.ExecQuery("SELECT * FROM Win32_ComputerSystem")).item();
var physicMenCap = Math.ceil(system.TotalPhysicalMemory / 1024 / 1024); // MB
var curUser = system.UserName;
var cpuCount = system.NumberOfProcessors;
return {
cpuType: cpuType,
cpuCount: cpuCount,
hostName: hostName,
curUser: curUser,
memCap: physicMenCap,
mem: mem
};
} catch (e) {
console.error("WMI 查詢失敗:", e.message);
return null;
}
}
// 使用示例
var info = getSysInfo();
if (info) {
console.log("CPU:", info.cpuType, "×", info.cpuCount);
console.log("內存:", info.memCap, "MB");
console.log("用戶:", info.curUser);
info.mem.forEach(function(m, i) {
console.log(" 插槽" + (i+1) + ":", m.capacity + "MB @" + m.speed + "MHz");
});
}
| WMI 類 | 用途 | 關鍵屬性示例 |
|---|---|---|
| Win32_Processor | CPU 處理器 | Name, DeviceID, CpuStatus, ProcessorId, NumberOfCores, MaxClockSpeed |
| Win32_PhysicalMemory | 物理內存條 | Capacity, Speed, Manufacturer, PartNumber, BankLabel |
| Win32_ComputerSystem | 系統整體 | TotalPhysicalMemory, UserName, NumberOfProcessors, Model, Manufacturer |
| Win32_OperatingSystem | 作業系統 | Caption, Version, BuildNumber, OSArchitecture, LastBootUpTime |
| Win32_BaseBoard | 主板 | Product, Manufacturer, SerialNumber, Version |
| Win32_BIOS | BIOS | SMBIOSBIOSVersion, Manufacturer, ReleaseDate |
| Win32_DiskDrive | 硬盤驅動器 | Model, Size, InterfaceType, MediaType, Partitions |
| Win32_LogicalDisk | 邏輯磁盤(分區) | DeviceID, Size, FreeSpace, FileSystem, VolumeName |
| Win32_NetworkAdapter | 網絡適配器 | Name, MACAddress, Speed, NetConnectionStatus |
| Win32_NetworkAdapterConfiguration | 網卡配置 | IPAddress, DefaultIPGateway, DNSServerSearchOrder, DHCPEnabled |
| Win32_VideoController | 顯卡 | Name, AdapterRAM, DriverVersion, VideoModeDescription |
| Win32_SoundDevice | 聲卡 | Name, Manufacturer, Status |
| Win32_USBController | USB 控制器 | Name, DeviceID, Status |
| Win32_Keyboard | 鍵盤 | Name, Description, Status |
| Win32_PointingDevice | 鼠標/指針設備 | Name, HardwareType, Status |
| Win32_Printer | 打印機 | Name, PortName, DriverName, WorkOffline |
| Win32_Service | 系統服務 | Name, DisplayName, State, StartMode, StartName |
| Win32_Process | 運行進程 | Name, ProcessId, ExecutablePath, CommandLine |
| Win32_TimeZone | 時區 | Caption, StandardName, Bias |
| 屬性 | 值 |
|---|---|
| " + prop.Name + " | " + (prop.Value || "") + " |
Node.js 無 ActiveXObject,可用以下方案:
wmic 命令行工具:child_process.exec('wmic cpu get name')systeminformation npm 包:跨平台、豐富 API、npm i systeminformationnode-wmi:Windows 專用 WMI 封裝// systeminformation 示例
const si = require('systeminformation');
async function getInfo() {
const cpu = await si.cpu();
const mem = await si.mem();
const disk = await si.diskLayout();
console.log(cpu, mem, disk);
}
WMI 提供了強大的 Windows 系統信息查詢能力,但受限於 IE/ActiveX 環境。現代 Web 開發中,此類需求通常由原生客戶端、Electron 應用、或後端部署 Agent 完成,純 Web 頁面極少使用。