VBS脚本得到CPU使用率,硬盘使用率和内存使用率
VBS脚本得到CPU使用率,硬盘使用率和内存使用率
2011年05月03日
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 sReturnFunction 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 NextEnd 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 colOSstrReturn = "内存总数: " & round(objOS.TotalVisibleMemorySize / 1024) & " MB" & vbCrLf &"内存可用数: " & round(objOS.FreePhysicalMemory / 1024) & " MB" & vbCrLf &"内存使用率 :" & Round(((objOS.TotalVisibleMemorySize-objOS.FreePhysicalMemory)/objOS.TotalVisibleMemorySize)*100) & "%"Wscript.Echo strReturnnext
如果想把结果保存在文本里只需要用批处理调用
echo offecho CPU信息:>info.xlscscript //Nologo cpu.vbs >> info.xlsecho.>>info.xlsecho 内存信息:>>info.txtcscript //Nologo ram.vbs >> info.txtecho.>>info.xlscscript //Nologo hard.vbs >> info.txtrem copy info.xls "%date%".xls