标题: [问题求助] vbs是否可以实现:检测sqlservr.exe这个进程,占用cpu如果超过40%,就执行某个代码。 [打印本页]
作者: ygqiang 时间: 2012-8-31 15:41 标题: vbs是否可以实现:检测sqlservr.exe这个进程,占用cpu如果超过40%,就执行某个代码。
- for each Process in GetObject("winmgmts:").ExecQuery("Select * from Win32_Process")
- WScript.echo Process.name & " " & CPUUSage(Process.Handle) & " %"
- Next
-
- Function CPUUSage( ProcID )
- On Error Resume Next
- Set objService = GetObject("Winmgmts:{impersonationlevel=impersonate}!\Root\Cimv2")
-
- For Each objInstance1 in objService.ExecQuery("Select * from Win32_PerfRawData_PerfProc_Process where IDProcess = '" & ProcID & "'")
- N1 = objInstance1.PercentProcessorTime
- D1 = objInstance1.TimeStamp_Sys100NS
- Exit For
- Next
-
- For Each perf_instance2 in objService.ExecQuery("Select * from Win32_PerfRawData_PerfProc_Process where IDProcess = '" & ProcID & "'")
- N2 = perf_instance2.PercentProcessorTime
- D2 = perf_instance2.TimeStamp_Sys100NS
- Exit For
- Next
-
- ' CounterType - PERF_100NSEC_TIMER_INV
- ' Formula - (1- ((N2 - N1) / (D2 - D1))) x 100
- Nd = (N2 - N1)
- Dd = (D2-D1)
- PercentProcessorTime = ( (Nd/Dd)) * 100
- CPUUSage = Round(PercentProcessorTime ,0)
- End Function
复制代码
上面这个代码,可以实现:检测所有进程中,每个进程的cpu占用百分比,并弹出窗口显示。
能否修改下这个vbs代码,实现:
xp系统,vbs代码放在开始—程序—启动项目下,运行vbs的时候是后台隐藏运行
1、先执行
net start mssqlserver
ping 127.0.0.1 -n 300 >nul 2>nul
2、再执行
间隔10秒,检测sqlservr.exe这个进程,占用cpu如果超过40%,就执行下面的代码。
net stop %Services1%
ping 127.0.0.1 -n 1 >nul 2>nul
net start %Services1%
ping 127.0.0.1 -n 2 >nul 2>nul
net start %Services1%
ping 127.0.0.1 -n 3 >nul 2>nul
net start %Services1%
ping 127.0.0.1 -n 5 >nul 2>nul
net start %Services1%
ping 127.0.0.1 -n 180 >nul 2>nul
net start %Services1%
f:
cd\
cd F:\上机管理\CCSDoor20\
start CCS_Door.exe
作者: jim515 时间: 2013-4-12 21:01
ws.run "F:\上机管理\CCSDoor20\CCS_Door.exe"
作者: jim515 时间: 2013-4-12 21:45
Set ws = CreateObject("Wscript.Shell")
ws.run "cmd /c 批处理路径",vbhide
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |