自动获取系统性能数据的脚本

 

 每隔1秒获取一次CPU、内存和磁盘的性能数据并存储到文件中。

  Const ForReading = 1, ForWriting = 2, ForAppending = 8 '实例化对象 Set WS = CreateObject("Wscript.Shell") Set fso = CreateObject("Scripting.FileSystemObject") 'Set LogFile = fso.CreateTextFile("CPU.txt",True) '写开始时间 'LogFile.WriteLine("程序启动时间: " & Now()) '获取VBS脚本当前工作目录 'apppath=left(Wscript.ScriptFullName,len(Wscript.ScriptFullName)-len(Wscript.ScriptName)) strComputer = "." Set objWMIService = GetObject("winmgmts://" & strComputer & "/root/CIMV2") '实例化WMI的CIMV2命名空间 '总计物理内存 Set TotalMems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem") dwTotalMem = 0 For Each TotalMem in TotalMems 'Wscript.Echo "" & TotalMem.TotalPhysicalMemory & "" dwTotalMem = dwTotalMem + (TotalMem.TotalPhysicalMemory / 1024 / 1024) FileName = TotalMem.Name Next FileName=FileName & "-" 'FileName=FileName & cstr(Now) 'FileName=Replace(FileName,":","") 'FileName=Replace(FileName,"/","") 'FileName=Replace(FileName," ","-") DateStr=cstr(Date) Y=Year(DateStr) M=Right("0" & Month(DateStr),2) D=Right("0" & Day(DateStr),2) FileName = FileName & Y & M & D FileName=FileName & ".txt" Set LogFile = fso.OpenTextFile(FileName, ForAppending, True) 'Wscript.Echo FileName 'Wscript.Exit 'Wscript.Echo "" & dwTotalMem & "" Do '统计CPU使用率 Set Processors = objWMIService.ExecQuery("SELECT * FROM Win32_Processor", , 48) '执行WMI查询语句,”Win32_Processor”是WMI的一个类 CPU = 0 i = 0 For Each Processor In Processors CPU = CPU + Processor.LoadPercentage '将从”Win32_Processor“的对象中获取的值赋值给”cupstr”,即实际CPU使用率 i = i + 1 Next CPU = CPU / i '统计使用的内存 Set Memorys = objWMIService.ExecQuery("Select * from Win32_PerfRawData_PerfOS_Memory",,48) UsedMem = 0 For Each Memory in Memorys UsedMem = UsedMem + CInt(dwTotalMem - Memory.AvailableMBytes) 'Wscript.Echo Memory.AvailableMBytes Next '统计磁盘性能 AvgDiskBytesPerRead = 0 AvgDiskBytesPerWrite = 0 DiskReadBytesPersec = 0 DiskWriteBytesPersec = 0 IOPerSec = 0 Set PhysicalDisks = objWMIService.InstancesOf("Win32_PerfFormattedData_PerfDisk_PhysicalDisk",48) For Each PhyDisk in PhysicalDisks AvgDiskBytesPerRead = AvgDiskBytesPerRead + PhyDisk.AvgDiskBytesPerRead AvgDiskBytesPerWrite = AvgDiskBytesPerWrite + PhyDisk.AvgDiskBytesPerWrite DiskReadBytesPersec = DiskReadBytesPersec + PhyDisk.DiskReadBytesPersec DiskWriteBytesPersec = DiskWriteBytesPersec + PhyDisk.DiskWriteBytesPersec IOPerSec = IOPerSec + CInt(PhyDisk.DiskReadsPersec + PhyDisk.DiskWritesPersec) Next 'Wscript.Echo "" & dwAvailableMem & "" LogFile.WriteLine ""& CPU &" " & UsedMem &" " & IOPerSec & " " & AvgDiskBytesPerRead & " " & _ AvgDiskBytesPerWrite & " " & DiskReadBytesPersec & " " & DiskWriteBytesPersec & "" 'Wscript.Echo ""& cpustr &"," & memstr &"" wscript.sleep (1000) Loop  

你可能感兴趣的:(自动获取系统性能数据的脚本)