VBS windows监控

http://bbs.csdn.net/topics/230078112
'
/************************************************************************/ ' ' FileName: WriteUsage.vbs ' ' Author: Chinafish ' ' Email: china_fish@163.com ' ' Homepage: http://www.csinx.org ' ' Desc: Write CPU & Memory usage ' '/************************************************************************/ On Error Resume Next Dim dwTotalMem, dwAvailMem Const szRootDir = "D:\CPU\" '定时循环记录 Do While True Const ForReading = 1 Const ForWriting = 2 Set oFSO = CreateObject("Scripting.FileSystemObject") '计算当日记录文件名 Today = Date() TargetFile = szRootDir & Year(Today) & Right("00" & Month(Today),2) & Right("00" & Day(Today),2) & ".log" '没有文件先创建文件 Set MyFile = oFSO.CreateTextFile(TargetFile, false) MyFile.Close '打开创建的文件读出原来的内容 Set MyFile = oFSO.OpenTextFile(TargetFile, ForReading) strContent = MyFile.ReadAll MyFile.Close '先写原文件内容 Set MyFile = oFSO.OpenTextFile(TargetFile, ForWriting) '文件无内容时写文件头 if strContent <> "" then MyFile.WriteLine(strContent) else MyFile.WriteLine("/*Usage.log file write by Chinafish.*/") MyFile.WriteBlankLines 1 End if '写开始时间 MyFile.WriteLine("程序启动时间: " & Now()) '本地电脑 strComputer = "." '连接到本地电脑 Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.InstancesOf("Win32_LogicalMemoryConfiguration") '总计物理内存 For Each colItem In colItems dwTotalMem = CLng(colItem.TotalPhysicalMemory / 1024) MyFile.WriteLine("总计物理内存: " & CLng(colItem.TotalPhysicalMemory / 1024) & "MB") Next '剩余内存 Set colItems = objWMIService.ExecQuery("Select * from Win32_PerfRawData_PerfOS_Memory",,48) For Each objItem in colItems dwAvailMem = objItem.AvailableMBytes MyFile.WriteLine("剩余物理内存: " & objItem.AvailableMBytes & "MB") Next '物理内存使用率(第一行精确到小数后2位, 第二行取整数) 'MyFile.WriteLine("物理内存使用: " & CInt((1 - dwAvailMem/dwTotalMem)*100*100)/100 & "%") MyFile.WriteLine("物理内存使用: " & CInt((1 - dwAvailMem/dwTotalMem)*100) & "%") 'CPU数和使用率 Set colItems = objWMIService.ExecQuery("Select * from Win32_Processor",,48) i = 0 For Each objItem in colItems MyFile.WriteLine("CPU(" & i & ")使用率: " & objItem.LoadPercentage & "%") i = i + 1 Next '写结束时间 MyFile.WriteLine("程序结束时间: " & Now()) MyFile.Close Set oFSO = Nothing Set MyFile = Nothing '睡眠达到定时操作目的 参数是ms 1000是1秒 WScript.Sleep 1000*60*1 Loop

 

自行修改版。

On Error Resume Next

Dim dwTotalMem, dwAvailMem

Const szRootDir = "D:\CPU\"

Set oFSO = CreateObject("Scripting.FileSystemObject")

    Const ForReading = 1
    Const ForWriting = 2

    '计算当日记录文件名
    Today = Date()
    TargetFile =  szRootDir & Year(Today) & Right("00" & Month(Today),2) &  Right("00" & Day(Today),2) & Hour(Now) & ".log"


    '没有文件先创建文件
    Set MyFile = oFSO.CreateTextFile(TargetFile, false)
    MyFile.Close


    Set MyFile = oFSO.OpenTextFile(TargetFile, ForWriting)


    '写开始时间
    MyFile.WriteLine("程序启动时间: " & Now())

    '本地电脑
    strComputer = "."

    '连接到本地电脑
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

    Set colItems = objWMIService.InstancesOf("Win32_LogicalMemoryConfiguration")

    '总计物理内存
    For Each colItem In colItems
        dwTotalMem = CLng(colItem.TotalPhysicalMemory / 1024)
        MyFile.WriteLine("总计物理内存: " & CLng(colItem.TotalPhysicalMemory / 1024) & "MB")
    Next

    '剩余内存
    Set colItems = objWMIService.ExecQuery("Select * from Win32_PerfRawData_PerfOS_Memory",,48)

    For Each objItem in colItems
        dwAvailMem = objItem.AvailableMBytes
        MyFile.WriteLine("剩余物理内存: " & objItem.AvailableMBytes & "MB")
    Next


    'CPU数和使用率
    Set colItems = objWMIService.ExecQuery("Select * from Win32_Processor",,48)
    i = 0

    For Each objItem in colItems
        MyFile.WriteLine("CPU(" & i & ")使用率: " & objItem.LoadPercentage & "%")
        i = i + 1
    Next

    MyFile.Close

    Set oFSO = Nothing
    Set MyFile = Nothing

 

posted @ 2013-09-10 23:10  胡.杰  阅读(407)  评论(0编辑  收藏  举报