首页 > 编程 > JS > 正文

JS通过WMI获取客户端硬件信息

2020-09-19 11:05:01
字体:
来源:转载
供稿:网友
通过wmi来实现获取客户端计算机硬件及系统信息:

1//系统信息获取
2function getsysinfo(){
3 var locator = new activexobject ("wbemscripting.swbemlocator");
4 var service = locator.connectserver(".");
5 //cpu信息
6 var cpu = new enumerator (service.execquery("select * from win32_processor")).item();
7 var cputype=cpu.name,hostname=cpu.systemname;
8 //内存信息
9 var memory = new enumerator (service.execquery("select * from win32_physicalmemory"));
10 for (var mem=[],i=0; !memory.atend(); memory.movenext()) mem[i++]={cap:memory.item().capacity/1024/1024,speed:memory.item().speed}
11 //系统信息
12 var system=new enumerator (service.execquery("select * from win32_computersystem")).item();
13 var physicmencap=math.ceil(system.totalphysicalmemory/1024/1024),curuser=system.username,cpucount=system.numberofprocessors
14
15 return {cputype:cputype,cpucount:cpucount,hostname:hostname,curuser:curuser,memcap:physicmencap,mem:mem}
16}

代码实现主要包括这几部分:

先通过new activexobject ("wbemscripting.swbemlocator"); 访问到wbemscripting对象。
通过locator.connectserver("."); 连接我们本地电脑(.代表本地电脑,当然
也可以访问其他计算机)。
通过service.execquery("select * from win32_processor")这个类似sql的语句(其实系统信息也是存储在计算中一个类似数据库的文件中)获取我们需要的对象的记录集。
通过new enumerator来创建一个可枚举的对象,下面就可以遍历取信息了。
注意:运行的前提是要修改浏览器安全设置,“允许对未标记为可安全执行的activex
脚本的运行”。


这里主要取了cpu、内存及系统用户几个信息,大家利用wmi的api或者借助jsedit获取
到更多的信息。下面列出了常用信息的类:

win32_processor // cpu 处理器

win32_physicalmemory // 物理内存

win32_keyboard // 键盘

win32_pointingdevice // 点输入设备,如鼠标

win32_diskdrive // 硬盘驱动器

win32_cdromdrive // 光盘驱动器

win32_baseboard // 主板

win32_bios // bios 芯片

win32_parallelport // 并口

win32_serialport // 串口

win32_sounddevice // 多媒体设置

win32_usbcontroller // usb 控制器

win32_networkadapter // 网络适配器

win32_networkadapterconfiguration // 网络适配器设置

win32_printer // 打印机

win32_printerconfiguration // 打印机设置

win32_printjob // 打印机任务

win32_tcpipprinterport // 打印机端口

win32_potsmodem // modem

win32_potsmodemtoserialport // modem 端口

win32_desktopmonitor // 显示器

win32_videocontroller // 显卡细节。

win32_videosettings // 显卡支持的显示模式。

win32_timezone // 时区

win32_systemdriver // 驱动程序

win32_diskpartition // 磁盘分区

win32_logicaldisk // 逻辑磁盘

win32_logicalmemoryconfiguration // 逻辑内存配置

win32_pagefile // 系统页文件信息

win32_pagefilesetting // 页文件设置

win32_bootconfiguration // 系统启动配置

win32_operatingsystem // 操作系统信息

win32_startupcommand // 系统自动启动程序

win32_service // 系统安装的服务

win32_group // 系统管理组

win32_groupuser // 系统组帐号

win32_useraccount // 用户帐号

win32_process // 系统进程

win32_thread // 系统线程

win32_share // 共享

win32_networkclient // 已安装的网络客户端

win32_networkprotocol // 已安装的网络协议

wmi win32类的完整信息及详细列表请参考msdn:
http://msdn2.microsoft.com/en-us/library/aa394084(vs.85).aspx
示例:


1function button1_onclick() {//cpu 信息
2 var locator = new activexobject ("wbemscripting.swbemlocator");
3 var service = locator.connectserver(".");
4 var properties = service.execquery("select * from win32_processor");
5 var e = new enumerator (properties);
6 document.write("<table border=1>");
7 for (; !e.atend(); e.movenext ())
8 {
9 var p = e.item ();
10 document.write("<tr>");
11 document.write("<td>" + p.caption + "</td>");
12 document.write("<td>" + p.deviceid + "</td>");
13 document.write("<td>" + p.name + "</td>");
14 document.write("<td>" + p.cpustatus + "</td>");
15 document.write("<td>" + p.availability + "</td>");
16 document.write("<td>" + p.level + "</td>");
17 document.write("<td>" + p.processorid + "</td>");
18 document.write("<td>" + p.systemname + "</td>");
19 document.write("<td>" + p.processortype + "</td>");
20 document.write("</tr>");
21 }
22 document.write("</table>");
23}
24
25function button2_onclick() {//cd-rom 信息
26 var locator = new activexobject ("wbemscripting.swbemlocator");
27 var service = locator.connectserver(".");
28 var properties = service.execquery("select * from win32_cdromdrive");
29 var e = new enumerator (properties);
30 document.write("<table border=1>");
31 for (; !e.atend(); e.movenext ())
32 {
33 var p = e.item ();
34 document.write("<tr>");
35 document.write("<td>" + p.caption + "</td>");
36 document.write("<td>" + p.description + "</td>");
37 document.write("<td>" + p.drive + "</td>");
38 document.write("<td>" + p.status + "</td>");
39 document.write("<td>" + p.medialoaded + "</td>");
40 document.write("</tr>");
41 }
42 document.write("</table>");
43}
44
45function button3_onclick() {//键盘信息
46 var locator = new activexobject ("wbemscripting.swbemlocator");
47 var service = locator.connectserver(".");
48 var properties = service.execquery("select * from win32_keyboard");
49 var e = new enumerator (properties);
50 document.write("<table border=1>");
51 for (; !e.atend(); e.movenext ())
52 {
53 var p = e.item ();
54 document.write("<tr>");
55 document.write("<td>" + p.description + "</td>");
56 document.write("<td>" + p.name + "</td>");
57 document.write("<td>" + p.status + "</td>");
58 document.write("</tr>");
59 }
60 document.write("</table>");
61}
62
63function button4_onclick() {//主板信息
64 var locator = new activexobject ("wbemscripting.swbemlocator");
65 var service = locator.connectserver(".");
66 var properties = service.execquery("select * from win32_baseboard");
67 var e = new enumerator (properties);
68 document.write("<table border=1>");
69 for (; !e.atend(); e.movenext ())
70 {
71 var p = e.item ();
72 document.write("<tr>");
73 document.write("<td>" + p.hostingboard + "</td>");
74 document.write("<td>" + p.manufacturer + "</td>");
75 document.write("<td>" + p.poweredon + "</td>");
76 document.write("<td>" + p.product + "</td>");
77 document.write("<td>" + p.serialnumber + "</td>");
78 document.write("<td>" + p.version + "</td>");
79 document.write("</tr>");
80 }
81 document.write("</table>");
82}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表