转载自:https://blog.51cto.com/luweikai/1904427
参考:http://www.51testing.com/html/85/87885-17546.html
https://www.cnblogs.com/wtao/archive/2011/09/16/2178760.html
获取CPU使用率脚本(vbs),另存为cpu.vbs:
1 On Error Resume Next 2 Set objProc = GetObject("winmgmts:\\.\root\cimv2:win32_processor='cpu0'") 3 Wscript.Echo "CPU 使用率: " & objProc.LoadPercentage & "%"
获取内存使用率脚本(vbs),另存为ram.vbs:
1 strComputer = "." 2 set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") 3 set colOS = objWMI.InstancesOf("Win32_OperatingSystem") 4 for each objOS in colOS 5 strReturn = "内存总数: " & round(objOS.TotalVisibleMemorySize / 1024) & " MB" & vbCrLf &"内存可用数: " & round(objOS.FreePhysicalMemory / 1024) & " MB" & vbCrLf &"内存使用率 :" & Round(((objOS.TotalVisibleMemorySize-objOS.FreePhysicalMemory)/objOS.TotalVisibleMemorySize)*100) & "%" 6 Wscript.Echo strReturn 7 next
获取磁盘使用率脚本(vbs),另存为hard.vbs:
1 Set fsoobj = CreateObject("Scripting.FileSystemObject") 2 DriversInfo = GetDriversInfo 3 DriversInfo = Replace(DriversInfo, "|", vbCrLf) 4 sReturn ="硬盘信息:" & vbCrLf & DriversInfo 5 Wscript.Echo sReturn 6 Function GetDriversInfo() 7 8 GetDriversInfo = "" 9 Set drvObj = fsoobj.Drives 10 For Each D In drvObj 11 Err.Clear 12 If D.DriveLetter <> "A" Then 13 If D.isReady Then 14 GetDriversInfo = GetDriversInfo & "分区:" & D.DriveLetter & vbCrLf 15 GetDriversInfo = GetDriversInfo & "可用空间:" & cSize(D.FreeSpace) & vbCrLf 16 GetDriversInfo = GetDriversInfo & "总大小:" & cSize( D.TotalSize) & vbCrLf 17 GetDriversInfo = GetDriversInfo & "使用率 :" & (100*((D.TotalSize-D.FreeSpace)/D.TotalSize)) &"%" & vbCrLf 18 GetDriversInfo = GetDriversInfo & "|" 19 Else 20 End If 21 Else 22 End If 23 Next 24 End Function 25 26 Function cSize(tSize) 27 28 If tSize >= 1073741824 Then 29 cSize = Int((tSize / 1073741824) * 1000) / 1000 & " GB" 30 ElseIf tSize >= 1048576 Then 31 cSize = Int((tSize / 1048576) * 1000) / 1000 & " MB" 32 ElseIf tSize >= 1024 Then 33 cSize = Int((tSize / 1024) * 1000) / 1000 & " KB" 34 Else 35 cSize = tSize & "B" 36 End If 37 38 End Function
最后批处理执行以上脚本,另存为“获取CPU、内存和磁盘使用率.bat”
1 @echo off 2 echo "正在获取CPU、内存和磁盘使用率,请稍候..." 3 echo CPU信息:>info.txt 4 cscript //Nologo cpu.vbs >> info.txt 5 echo.>>info.txt 6 echo 内存信息:>>info.txt 7 cscript //Nologo ram.vbs >> info.txt 8 echo.>>info.txt 9 cscript //Nologo hard.vbs >> info.txt
执行结果如下:
1 CPU信息: 2 CPU 使用率: 22% 3 4 内存信息: 5 内存总数: 12132 MB 6 内存可用数: 7354 MB 7 内存使用率 :39% 8 9 硬盘信息: 10 分区:C 11 可用空间:64.541 GB 12 总大小:102.443 GB 13 使用率 :36.9974772881646% 14 15 分区:D 16 可用空间:134.282 GB 17 总大小:402.132 GB 18 使用率 :66.6073734064705% 19 20 分区:E 21 可用空间:47.56 GB 22 总大小:300.003 GB 23 使用率 :84.1468686136043% 24 25 分区:F 26 可用空间:48.774 GB 27 总大小:220.006 GB 28 使用率 :77.8305072014826%