strComputer2 = "." If WScript.Arguments.Count > 1 Then Proc_file = WScript.Arguments.Item(0) flag_underscore = WScript.Arguments.Item(1) Set colNamedArguments = WScript.Arguments.Named 'This flag is used in case the process to be checked contains a blank character between flag_underscore = colNamedArguments.Item("Replace") ' These flags are not mandatory and define the maximum amount of CPU and Memory to be used by the process before raise an alert flag_cpu = int(colNamedArguments.Item("Cpu")) flag_memory = int(colNamedArguments.Item("Memory")) if flag_underscore="YES" OR flag_underscore="NO" then if (flag_underscore="YES") then Proc_file = Replace(Proc_file,"_"," ") end if Set objWMISProcess = GetObject("winmgmts:\\" & strComputer2 & "\root\cimv2") Set colProcess = objWMISProcess.ExecQuery("Select * from Win32_Process where Name = """ & Proc_file & """") CheckProcess="" For Each objProcess in colProcess CheckProcess=objProcess.ProcessId CheckCPU=CPUUSage(objProcess.Handle) CheckMemory=round(objProcess.WorkingSetSize / 1024 / 1024,0) Next If (CheckProcess = "") then Wscript.echo Proc_file & " not found" Wscript.quit(2) else if ((flag_cpu <> "0") and (CheckCPU > flag_cpu)) then output_message = output_message + " Too much CPU consuming!" end if if ((flag_memory <> "0") and (CheckMemory > flag_memory)) then output_message = output_message + " Too much Memory consuming!" end if Wscript.echo "Process " & Proc_file & " - CPU:" & CheckCPU & "% - Memory:" & CheckMemory & "Mb " & output_message if (output_message ="") then Wscript.quit(0) else Wscript.quit(1) end if end if else Wscript.Echo "Usage: Check_Proc.vbs Service_Name /Replace:Attribute /Cpu:value" & vbcrlf & " Attribute value can be YES or NO. In case yes, it will replace _ by blank space" Wscript.quit(1) End If Else Wscript.Echo "Usage: Check_Proc.vbs Service_Name /Replace:Attribute /Cpu:value" & vbcrlf & " Attribute value can be YES or NO. In case yes, it will replace _ by blank space" Wscript.quit(1) End If 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 WScript.Sleep(2000) 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