通过WMI来实现获取客户端计算机硬件及系统信息:
复制代码代码如下:
function getSysInfo(){
var locator = new ActiveXObject ("WbemScripting.SWbemLocator")
var service = locator.ConnectServer(".")
//CPU信息
var cpu = new Enumerator (service.ExecQuery("SELECT * FROM Win32_Processor")).item()
var cpuType=cpu.Name,hostName=cpu.SystemName
//内存信息
var memory = new Enumerator (service.ExecQuery("SELECT * FROM Win32_PhysicalMemory"))
for (var mem=[],i=0!memory.atEnd()memory.moveNext()) mem[i++]={cap:memory.item().Capacity/1024/1024,speed:memory.item().Speed}
//系统信息
var system=new Enumerator (service.ExecQuery("SELECT * FROM Win32_ComputerSystem")).item()
var physicMenCap=Math.ceil(system.TotalPhysicalMemory/1024/1024),curUser=system.UserName,cpuCount=system.NumberOfProcessors
return {cpuType:cpuType,cpuCount:cpuCount,hostName:hostName,curUser:curUser,memCap:physicMenCap,mem:mem}
}
代码实现主要包括这几部分:
先通过new ActiveXObject ("WbemScripting.SWbemLocator")访问到WbemScripting对象。
通过locator.ConnectServer(".")连接我们本地电脑(.代表本地电脑,当然
也可以访问其他计算机)。
通过service.ExecQuery("SELECT * FROM Win32_Processor")这个类似sql的语句(其实系统信息也是存储在计算中一个类似数据库的文件中)获取我们需要的对象的记录集。
通过new Enumerator来创建一个可枚举的对象,下面就可以遍历取信息了。
注意:运行的前提是要修改浏览器安全设置,“允许对未标记为可安全执行的ActiveX
脚本的运行”。
//CPU 信息var locator = new ActiveXObject ("WbemScripting.SWbemLocator")
var service = locator.ConnectServer(".")
var properties = service.ExecQuery("SELECT * FROM Win32_Processor")
var e = new Enumerator (properties)
document.write("<table border=1>")
for (!e.atEnd()e.moveNext ())
{
var p = e.item ()
document.write("<tr>")
document.write("<td>" + p.Caption + "</td>")
document.write("<td>" + p.DeviceID + "</td>")
document.write("<td>" + p.Name + "</td>")
document.write("<td>" + p.CpuStatus + "</td>")
document.write("<td>" + p.Availability + "</td>")
document.write("<td>" + p.Level + "</td>")
document.write("<td>" + p.ProcessorID + "</td>")
document.write("<td>" + p.SystemName + "</td>")
document.write("<td>" + p.ProcessorType + "</td>")
document.write("</tr>")
}
document.write("</table>")
//主板信息
var locator = new ActiveXObject ("WbemScripting.SWbemLocator")
var service = locator.ConnectServer(".")
var properties = service.ExecQuery("SELECT * FROM Win32_BaseBoard")
var e = new Enumerator (properties)
document.write("<table border=1>")
for (!e.atEnd()e.moveNext ())
{
var p = e.item ()
document.write("<tr>")
document.write("<td>" + p.HostingBoard + "</td>")
document.write("<td>" + p.Manufacturer + "</td>")
document.write("<td>" + p.PoweredOn + "</td>")
document.write("<td>" + p.Product + "</td>")
document.write("<td>" + p.SerialNumber + "</td>")
document.write("<td>" + p.Version + "</td>")
document.write("</tr>")
}
document.write("</table>")
//网络信息
var locator = new ActiveXObject ("WbemScripting.SWbemLocator")
var service = locator.ConnectServer(".")
var properties = service.ExecQuery("SELECT * FROM win32_networkadapterconfiguration")
var e = new Enumerator (properties)
document.write("<table border=1>")
for (!e.atEnd()e.moveNext ())
{
var p = e.item ()
if (p.IPAddress==null) {
continue
}
document.write("<tr>")
document.write("<td>" + p.Caption + "</td>")
document.write("<td>" + p.IPAddress(0) + "</td>")
document.write("<td>" + p.MACAddress + "</td>")
document.write("</tr>")
}
document.write("</table>")