[转]VBS脚本得到CPU使用率,硬盘使用率和内存使用率

阅读更多

CPU 使用率代码如下

On
 Error
 Resume
 Next


Set
 objProc  = GetObject("winmgmts:\\.\root\cimv2:win32_processor='cpu0'"

)
Wscript.Echo "CPU 使用率: "
 & objProc.LoadPercentage & "%"




硬盘 使用率代码如下

Set
 fsoobj = CreateObject("Scripting.FileSystemObject"
)
 DriversInfo = GetDriversInfo
 DriversInfo = Replace(DriversInfo, "|"
 , vbCrLf)
 sReturn ="硬盘信息:"

 & vbCrLf & DriversInfo
 Wscript.Echo sReturn
Function

 GetDriversInfo()
   GetDriversInfo = ""

Set

 drvObj = fsoobj.Drives
   For

 Each

 D In

 drvObj
       Err.Clear
       If

 D.DriveLetter <> "A"

 Then


           If

 D.isReady Then


               GetDriversInfo = GetDriversInfo & "分区:"

 & D.DriveLetter & vbCrLf
               GetDriversInfo = GetDriversInfo & "可用空间:"

 & cSize(D.FreeSpace) & vbCrLf
               GetDriversInfo = GetDriversInfo & "总大小:"

 & cSize( D.TotalSize) & vbCrLf
               GetDriversInfo = GetDriversInfo & "使用率 :"

 & (100*((D.TotalSize-D.FreeSpace)/D.TotalSize)) &"%"

 & vbCrLf
               GetDriversInfo = GetDriversInfo & "|"


             Else


           End

 If


         Else


       End

 If


   Next


End

 Function


 
 Function

 cSize(tSize)
 
     If

 tSize >= 1073741824 Then


         cSize = Int((tSize / 1073741824) * 1000) / 1000 & " GB"


       ElseIf

 tSize >= 1048576 Then


         cSize = Int((tSize / 1048576) * 1000) / 1000 & " MB"


       ElseIf

 tSize >= 1024 Then


         cSize = Int((tSize / 1024) * 1000) / 1000 & " KB"


       Else


         cSize = tSize & "B"


     End

 If


 
End

 Function




内存 使用率代码如下

strComputer = "."


set

 objWMI = GetObject("winmgmts:\\"

 & strComputer & "\root\cimv2"

)
set

 colOS = objWMI.InstancesOf("Win32_OperatingSystem"

)
for

 each

 objOS in

 colOS
strReturn = "内存总数: "

 &  round(objOS.TotalVisibleMemorySize / 1024) & " MB"

 & vbCrLf &"内存可用数: "

 & round(objOS.FreePhysicalMemory / 1024) & " MB"

 & vbCrLf &"内存使用率 :"

 & Round(((objOS.TotalVisibleMemorySize-objOS.FreePhysicalMemory)/objOS.TotalVisibleMemorySize)*100) & "%"


Wscript.Echo strReturn
next




如果想把结果保存在文本里只需要用批处理调用

echo off
echo CPU信息:>info.xls
cscript //Nologo cpu.vbs >> info.xls
echo.>>info.xls
echo 内存信息:>>info.txt
cscript //Nologo ram.vbs >> info.txt
echo.>>info.xls
cscript //Nologo hard.vbs >> info.txt

rem copy info.xls "date%"
.xls


你可能感兴趣的:(VBS,CPU,内存,使用率)