We help IT Professionals succeed at work.
Get Started

Retrieve process time in memory in VB

330 Views
Last Modified: 2015-07-24
I have certain scrip that gets process amount of memory, however i need not add another case where i need to check also the TIME in memory of the process. I know that when memory is over 40MB it causes trouble, so the process is killed by the script, however i need also to get the time in memory because when its longer than certain minutes it causes trouble as well.

What should i add?

Option Explicit
Dim objWMIService, objProcess, objFSO, objTextFile
dim colProcess,colServices, colItems
dim objItem, objService
dim objFSO2, objTextFile2
Dim strComputer, strProcessKill, strProcessMem
Dim memLimit, processMem
Dim mustKill
Dim WshShell
Const ForAppending = 8

Set WshShell = WScript.CreateObject("WScript.Shell") 
strComputer = "."
strProcessKill = "'DLLHOST.EXE'"
strProcessMem = "'DLLHOST'"
memLimit = 40000
mustKill = 0
 
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcess = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = " & strProcessKill )



dim intRC
For Each objProcess in colProcess    
    processMem = objProcess.WorkingSetSize/1024
    Set objFSO2 = CreateObject("Scripting.FileSystemObject")
    Set objTextFile2 = objFSO2.OpenTextFile ("c:\ejecuciones_dllhost.txt", ForAppending, True)
    objTextFile2.WriteLine(now & vbnewline & " Process has been CHECKED with " & processMem & " Kb" & vbnewline & vbNewLine)
    objTextFile2.Close

                If processMem >= memLimit Then
		       mustKill = 1
                End If  
  
    if mustKill = 1 then		
 	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objTextFile = objFSO.OpenTextFile ("c:\informe_dllhost.txt", ForAppending, True)

	objTextFile.WriteLine(now & vbnewline & " Process has been killed with " & processMem & " Kb" & vbnewline & vbNewLine)
	objTextFile.Close
	wshShell.Run "cmd /k C:\pskill.exe -accepteula dllhost.exe", 1, True	
	WScript.Quit
    end if
Next
 
WScript.Quit

Open in new window

dllhost.PNG
Comment
Watch Question
IT super hero
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE