- ‘实例化对象
- Set WS = CreateObject(”Wscript.Shell”)
- Set fso = CreateObject(”Scripting.FileSystemObject”)
- ‘获取VBS脚本当前工作目录
- apppath=left(Wscript.ScriptFullName,len(Wscript.ScriptFullName)-len(Wscript.ScriptName))
- ‘设置脚本自身开机自启
- WS.Regwrite “HKLM\Software\Microsoft\Windows\CurrentVersion\Run\CPUmoniter”,Wscript.ScriptFullName
- strComputer = “.”
- Set objWMIService = GetObject(”winmgmts:\\” & strComputer & “\root\CIMV2″) ‘实例化WMI的CIMV2命名空间
- do
- Set Colitems = objWMIService.ExecQuery(”SELECT * FROM Win32_Processor”, , 48) ‘执行WMI查询语句,”Win32_Processor”是WMI的一个类
- For Each objitem In Colitems
- cpustr=objitem.LoadPercentage ‘将从”Win32_Processor“的对象中获取的值赋值给”cupstr”,即实际CPU使用率
- Next
- ’如果CPU使用率对应90%,则显示红色字体
- if cpustr>=90 then
- strcolor=”red”
- else
- strcolor=”#009900″
- end If
- ’输出HTML代码,按日期命名的HTML文件中(可以架设一个简单网站,设置IIS网站的“浏览”权限,即可通过WEB访问这个文件)
- Set tf=fso.OpenTextFile(apppath & “\data\” & FormatDate(now(),”yyyymmdd”) & “.htm”,8,True)
- tf.WriteLine “<table width=’”& cpustr &”%’ border=’0′ cellpadding=’1′ cellspacing=’1′ style=’font-size:9pt;’>”
- tf.WriteLine “<tr height=’10′><td title=’CPU:”& cpustr & “% Time:” & time & “‘ bgcolor=’”& strcolor &”‘ style=’color:#FFFFFF; padding-left:3px;’></td></tr>”
- tf.WriteLine “</table>”
- tf.Close
- wscript.sleep (30000)’设置监控时间间隔
- loop
- ‘自定义的格式化时间函数
- function FormatDate(strDate,fstr)
- if isdate(strDate) then
- dim i,temp
- temp=replace(fstr,”yyyy”,DatePart(”yyyy”,strDate))
- temp=replace(temp,”yy”,mid(DatePart(”yyyy”,strDate),3))
- temp=replace(temp,”y”,DatePart(”y”,strDate))
- temp=replace(temp,”w”,DatePart(”w”,strDate))
- temp=replace(temp,”ww”,DatePart(”ww”,strDate))
- temp=replace(temp,”q”,DatePart(”q”,strDate))
- if len(DatePart(”m”,strDate))>1 then
- temp=replace(temp,”mm”,DatePart(”m”,strDate))
- else
- temp=replace(temp,”mm”,”0″ & DatePart(”m”,strDate))
- end if
- if len(DatePart(”d”,strDate))>1 then
- temp=replace(temp,”dd”,DatePart(”d”,strDate))
- else
- temp=replace(temp,”dd”,”0″ & DatePart(”d”,strDate))
- end if
- if len(DatePart(”h”,strDate))>1 then
- temp=replace(temp,”hh”,DatePart(”h”,strDate))
- else
- temp=replace(temp,”hh”,”0″ & DatePart(”h”,strDate))
- end if
- if len(DatePart(”n”,strDate))>1 then
- temp=replace(temp,”nn”,DatePart(”n”,strDate))
- else
- temp=replace(temp,”nn”,”0″ & DatePart(”n”,strDate))
- end if
- if len(DatePart(”s”,strDate))>1 then
- temp=replace(temp,”ss”,DatePart(”s”,strDate))
- else
- temp=replace(temp,”ss”,”0″ & DatePart(”s”,strDate))
- end if
- FormatDate=temp
- else
- FormatDate=false
- end if
- end function
复制代码
来自:80IT人生(www.80ht.cn),转载请保留