欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Hardware_Info.vbs 获取硬件信息的VBS代码

程序员文章站 2022-08-27 15:59:32
复制代码 代码如下:'hardware_info.vbs v1.1 by: fastslzon error resume nextdim wmi,ws,fsoset wmi...

复制代码 代码如下:

'hardware_info.vbs v1.1 by: fastslz
on error resume next
dim wmi,ws,fso
set wmi = getobject("winmgmts:{impersonationlevel=impersonate}!\\.\root\cimv2")
set coss = wmi.execquery("select * from win32_operatingsystem")
for each oos in coss
        osx = oos.caption &" " & oos.csdversion  & vbcrlf
next
bbx= "————————————主板信息————————————"  & vbcrlf
set board = wmi.instancesof("win32_baseboard")
set bios = wmi.instancesof("win32_bios")
for each oboard in board
bbx = bbx _
    & "主板名称: " & oboard.product &vbtab & oboard.version & vbcrlf _
    & "制造厂商: " & oboard.manufacturer                    & vbcrlf
next
for each obios in bios
bbx = bbx _
    & "bios厂商: " & obios.manufacturer                     & vbcrlf _
    & "bios日期: " & formatdatetime(wdate(trim(obios.releasedate)),1)  & vbcrlf _
    & "bios版本: " & obios.smbiosbiosversion & vbcrlf _
    & "oem 版本: " & obios.version           & vbcrlf
next
set bios = nothing:set board  = nothing

cpux = "———————————处理器信息———————————"  & vbcrlf
set cpus = wmi.instancesof("win32_processor")
set caches = wmi.instancesof("win32_cachememory")
for each objcpu in cpus
mcs = objcpu.maxclockspeed :ccs = objcpu.currentclockspeed
if objcpu.maxclockspeed mod 2 = 1 then mcs = ccs + 1
if objcpu.currentclockspeed mod 2 = 1 then ccs = ccs + 1
oc = qoc(ccs,mcs)
if (oc-mcs) >  10 and (oc-mcs) > 0 then oclc = "  超频比率: " & formatpercent((oc-mcs)/mcs)
if (oc-mcs) < -10 and (oc-mcs) < 0 then oclc = "  降频比率: " & formatpercent((oc-mcs)/mcs)
cpux = cpux _
     & "cpu 名称: " & trim(objcpu.name)                & vbcrlf _
     & "cpu 构架: " & objcpu.description               & vbcrlf _
     & "制造厂商: " & objcpu.manufacturer              & vbcrlf _
     & "接口规格: " & objcpu.socketdesignation         & vbcrlf _
     & "cpu 数量: " & objcpu.cpustatus                 & vbcrlf _
     & "核心数量: " & objcpu.numberofcores             & vbcrlf _
     & "线程数量: " & objcpu.numberoflogicalprocessors & vbcrlf _
     & "地址位宽: " & objcpu.addresswidth & " bit"     & vbcrlf _
     & "数据位宽: " & objcpu.datawidth    & " bit"     & vbcrlf _
     & "cpu 电压: " & objcpu.currentvoltage / 10 & "v" & vbcrlf _
     & "外部频率: " & objcpu.extclock  & " mhz"        & vbcrlf _
     & "当前频率: " & oc               & " mhz" & oclc & vbcrlf _
     & "原始频率: " & mcs              & " mhz"        & vbcrlf _
     & "cpu 使用: " & objcpu.loadpercentage  & "%"     & vbcrlf
next
function qoc(ccs,mcs)
    if ccs = mcs then
        set wreg = getobject("winmgmts:{impersonationlevel=impersonate}!\\.\root\default:stdregprov")
        wreg.getdwordvalue &h80000002,"hardware\description\system\centralprocessor\0","~mhz",strvalue
        oc = strvalue :set wreg = nothing
        else
        oc = ccs
    end if
    qoc = oc :if qoc mod 2 = 1 then qoc = qoc + 1
end function
for each objcache in caches
    if objcache.maxcachesize > 0  then
        select case objcache.purpose
        case "l1-cache"
        cpux = cpux & "一级缓存: " & objcache.maxcachesize & "kb    (l1数据+l1缓存)" & vbcrlf
        case "l2-cache"
        cpux = cpux & "二级缓存: " & objcache.maxcachesize & " kb" & vbcrlf
        case "l3-cache"
        cpux = cpux & "三级缓存: " & objcache.maxcachesize & " kb" & vbcrlf
        end select
    end if
next
set caches = nothing:set cpus = nothing

memx = "————————————内存信息————————————" & vbcrlf
set memorys = wmi.instancesof("win32_physicalmemory")
set memky = wmi.instancesof("win32_operatingsystem")
for each aky in memky
    zl = aky.totalvisiblememorysize
    ky = aky.freephysicalmemory
next
mems = 0:memc = 0
types = array("unknown","other","dram","synchronous dram","cache dram","edo","edram","vram","sram", "ram", _
               "rom","flash","eeprom","feprom","eprom","cdram","3dram","sdram","sgram","rdram","ddr","ddr2")
for each mem in memorys
    for i = 0 to ubound(types)
        if mem.memorytype = i then memtype = types(i) :end if
    next
    for j = 0 to 6
        select case mem.tag
            case "physical memory " & j
            mems = mems+(mem.capacity)
            memx = memx  &"插槽" & mem.devicelocator & ": " & round(mem.capacity/1048576) &" mb  " _
                         & memtype & "-" & mem.speed & "mhz" & " 数据带宽" & mem.datawidth _
                         & "bit" & " 总带宽" & mem.totalwidth &"bit" & vbcrlf
        end select
    next
next
memx = memx _
     & "内存安装: "& round(mems/1048576)&" mb " & vbcrlf _
     & "内存总量: "& round(zl/1024)& " mb" & vbcrlf _
     & "内存可用: "& round(ky/1024)& " mb" & vbcrlf _ 
     & "内存使用率: " &formatpercent((zl-ky)/zl)& vbcrlf
set memorys = nothing:set memky = nothing

vx= "————————————显卡信息————————————"  & vbcrlf
set cvid = wmi.execquery("select deviceid from win32_videocontroller")
for each ovid in cvid
    set video = wmi.execquery("select * from win32_videocontroller where deviceid='"& ovid.deviceid &"'")
    for each ovideo in video
        vx = vx _
        & "显卡名称: " & ovideo.name                                    & vbcrlf _
        & "制造厂商: " & ovideo.adaptercompatibility                    & vbcrlf _
        & "物理显存: " & round(ovideo.adapterram/1048576)&" mb "        & vbcrlf _
        & "显示模式: " & ovideo.currenthorizontalresolution &" x " _
                       & ovideo.currentverticalresolution   &" "_
                       & ovideo.currentbitsperpixel         &"bit " _
                       & ovideo.currentrefreshrate          &"hz"     & vbcrlf
    next
next
set video = nothing:set cvid = nothing

dx= "————————————硬盘信息————————————"  & vbcrlf
set ide = wmi.execquery("select * from win32_diskdrive where interfacetype='ide'")
set cppp = wmi.execquery("select * from win32_perfrawdata_perfdisk_physicaldisk")
for each oide in ide
    for i = 0 to ide.count
        select case oide.index
            case i
               for each oppp in cppp
                   if instr(oppp.name, i) then vname = oppp.name
               next
               dx = dx & "硬盘" & i &"型号:" & oide.caption                   & vbcrlf _
               & vbtab & "标称容量: "    & round(oide.size/1000000000) &" gb" & vbcrlf _
               & vbtab & "实际容量: "    & round(oide.size/1073741824) &" gb" & vbcrlf _
               & vbtab & "柱面数: "      & oide.totalcylinders                & vbcrlf _
               & vbtab & "磁头数: "      & oide.totalheads                    & vbcrlf _
               & vbtab & "每道扇区数: "  & oide.sectorspertrack               & vbcrlf _
               & vbtab & "扇区大小: "    & oide.bytespersector                & vbcrlf _
               & vbtab & "总扇区数: "    & oide.totalsectors                  & vbcrlf _
               & vbtab & "分区状态: "    & vname  & vbcrlf
               devid = replace(oide.deviceid, "\", "\\")
               set cdp = wmi.execquery("associators of {win32_diskdrive.deviceid="""& devid &"""}" _
               & "where assocclass = win32_diskdrivetodiskpartition")
               for each odp in cdp
                 set cld = wmi.execquery("associators of {win32_diskpartition.deviceid="""& odp.deviceid &"""}" _
                 & "where assocclass = win32_logicaldisktopartition")
                   for each old in cld
                     dx = dx _
                        & vbtab & old.deviceid &" " & left(old.volumename&"         " ,11) & left(old.filesystem&"   " ,6) & "共:" _
                        & right("    "&round(old.size/1073741824,1),6)                &" gb    可用:" _
                        & right("    "&round(old.freespace/1073741824,1),6)           &" gb    已用:" _
                        & right("    "&round((old.size-old.freespace)/1073741824,1),6)&" gb" & vbcrlf
                   next
              next
        end select
    next
next
set cld = nothing:set cdp = nothing:set ide = nothing:set cdp = nothing:set cppp =  nothing

sx= "————————————声卡信息————————————"  & vbcrlf
set csd = wmi.execquery("select * from win32_sounddevice")
for each osd in csd
    sx = sx & "声卡名称: " & osd.productname  & vbcrlf
next
set csd = nothing
nx= "————————————网卡信息————————————"  & vbcrlf
set cnet = wmi.execquery("select * from win32_networkadapter where physicaladapter ='true'")
if cstr(cnet.count) < 0 then
    set cnet = wmi.execquery("select * from win32_networkadapter where pnpdeviceid like 'pci%%' or pnpdeviceid like 'usb%%'")
    for each onet in cnet
        if onet.netconnectionstatus > 0 then
           nx = nx & "网卡名称: "& onet.name & vbcrlf
           else
           nx = nx & "网卡名称: "& onet.name & vbcrlf
        end if
    next
    else
    for each onet in cnet
        if onet.netenabled = true then
           nx = nx & "网卡名称: "& onet.name & vbtab & "活跃状态" & vbcrlf
           else
           nx = nx & "网卡名称: "& onet.name & vbtab & "空闲状态" & vbcrlf
        end if
    next
end if
set cnet = nothing

if (lcase(right(wscript.fullname,11)) = "wscript.exe") then
    msgbox osx & bbx & cpux & memx ,,"hardware_info.vbs v1.0 by: fastslz"
    msgbox vx & sx & nx  ,,"hardware_info.vbs v1.0 by: fastslz"
    msgbox dx ,, "hardware_info.vbs v1.0 by: fastslz"
    msgn = msgbox ("是否保存到文件?" , 32+4 , "hardware_info.vbs v1.0 by: fastslz")
    if msgn = 6 then jzcsx = jzcs :winfo
    else
    wscript.echo osx & bbx & cpux & memx & vx & sx & nx & dx
end if
set wmi = nothing

sub winfo()
    set ws = createobject("wscript.shell")
    set fso = createobject("scripting.filesystemobject")
    afile = ws.currentdirectory &"\"& ws.expandenvironmentstrings("%computername%")&"_hardware_info.txt"
    set hinfo = fso.createtextfile(afile , true)
    hinfo.writeline "hardware_info.vbs by: fastslz"
    hinfo.writeline osx & bbx & cpux & memx & vx & sx & nx & dx & jzcsx
    hinfo.close
    ws.run chr(34) & afile & chr(34)
set ws = nothing:set fso = nothing:set hinfo = nothing
end sub

function wdate(nd)
    if not isnull(nd) then
    wdate = cdate(mid(nd,5,2)&"/"&mid(nd,7,2)&"/"&left(nd,4)&" "&mid(nd,9,2)&":"&mid(nd,11,2)&":"&mid(nd,13,2))
    end if
end function

function jzcs()
    nummsg = vbcrlf & "———————————简易cpu基准测试———————————" & vbcrlf
    dim i,t1,t2,tempvalue,aruntime,bruntime
    t1 = timer()
    for i = 1 to 2000000
        tempvalue= 2^0.5
    next
    t2 = timer()
    aruntime = formatnumber((t2-t1)*1000,2)
    nummsg =  nummsg & "cpu 200万次开方计算所需时间:" &aruntime&" 毫秒" & vbcrlf
    t1 = timer()
    for i = 1 to 6000000
        tempvalue= 1 + 1
    next
    t2 = timer()
    bruntime = formatnumber((t2-t1)*1000,2)
    nummsg =  nummsg & "cpu 600万次加法计算所需时间:"&bruntime&" 毫秒" & vbcrlf
    jzcs = nummsg
end function


cmd调用方法
复制代码 代码如下:

@echo off
for /f "delims=*" %%a in ('cscript //nologo "hardware_info.vbs"^|find ":"') do echo %%a

:在第二个硬盘第一个分区新建backup文件夹
for /f "tokens=2,*" %%a in ('cscript //nologo "hardware_info.vbs"^|findstr  "分区状态"') do (
    if %%a#==1# for /f "tokens=1" %%i in ("%%b") do md "%%i\backup")
pause