首页 » ASP代码 » VBS脚本得到CPU使用率,硬盘使用率和内存使用率

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

在不用第三方软件前提下想定时检查服务器的cpu使用率硬盘使用率和内存使用率,并生成报表在服务器巡检过程中相当实用,其实在Windows下,可以用批处理命令写一个脚本,在一定程度上完成这些简单的任务,但是如果想要实现复杂的功能,批处理命令就力不从心了。处理批处理命令,微软还提供了WSH。我们可以使用VBScript来写脚本,访问其内置对象或COM对象,然后让其在WSH中执行,就可以完成较为复杂的功能。下面提供几种实用的代码,只需要将代码复制到文本文档然后保存为vbs格式就可以在windows上直接运行了,如果是win2k的系统那么你需要从xp或win2003的system32下copy “CScript.exe”到vbs脚本目录。

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

, , , ,

转发到新浪微博 转发到新浪微博

目前这篇文章有3条评论(Rss)

  1. alberghi a valmontone | #1
    05/16/2012 at 13:04

    Very nice post. I just stumbled upon your weblog and wished to say that I’ve really enjoyed surfing around your blog posts. In any case I’ll be subscribing to your rss feed and I hope you write again soon!

  2. http://www.gpsmoto.org/ | #2
    05/16/2012 at 14:32

    I like this blog its a master peace ! Glad I found this on google. “Irrationally held truths may be more harmful than reasoned errors.” by Thomas Huxley.

  3. katalog stron | #3
    05/16/2012 at 15:00

    Thanks for your post. I also feel that laptop computers have become more and more popular right now, and now are usually the only sort of computer utilized in a household. The reason is that at the same time potentially they are becoming more and more affordable, their working power keeps growing to the point where these are as potent as personal computers coming from just a few years ago.

我要评论