Solved

In VBScript how can I trigger an action if a process has been idle for 2 minutes?

Posted on 2011-03-16
7
1,027 Views
Last Modified: 2012-05-11
I need to be able to monitor cpu utilization on a process (setup.exe) so that I can reboot the system if the "sngProcessTime is <0.5" for 2 minutes.  The script below calls a Reboot sub if the "sngProcessTime is <0.5" at the interval in the outer For-Next loop.  

The reason for this is we have a video driver update (setup.exe) that tends to just sit there even after the installation is complete.

How can this be modified to perform the action: Call Reboot if "sngProcessTime is <0.5 for a period of 2 minutes"?

Set colProcesses = objWMIService.ExecQuery("Select * from Win32_Process Where Name = 'Setup.exe'")
For i = 1 to 10
  For Each objProcess In colProcesses
     sngProcessTime = ( CSng(objProcess.KernelModeTime) + _
	                  CSng(objProcess.UserModeTime)) / 10000000
  Next 
wscript.sleep 30000
       If sngProcessTime < 0.5 Then
	     Call Reboot
       End If
Next	

Open in new window


Thank you.
0
Comment
Question by:tneubauertocg
  • 3
  • 3
7 Comments
 
LVL 12

Expert Comment

by:prashanthd
ID: 35154196
Do you want the script to keep monitor continuously, or only once?
0
 

Author Comment

by:tneubauertocg
ID: 35156553
I need it to monitor the Setup.exe process continuously (during script execution) until it has been idle for 2 minues - then call the reboot sub.
0
 
LVL 12

Accepted Solution

by:
prashanthd earned 500 total points
ID: 35157159
Try the following code...

regards
Prashanth
status=False
ststusnew=False

Do While status=False
    
    Set colProcesses = objWMIService.ExecQuery("Select * from Win32_Process Where Name = 'Setup.exe'")
    For Each objProcess In colProcesses
        sngProcessTime = ( CSng(objProcess.KernelModeTime) + _
        CSng(objProcess.UserModeTime)) / 10000000
        'WScript.Echo sngProcessTime & " - 1 "
    Next
    ststusnew=False
    If sngProcessTime < 0.5 Then
        timestr=DateAdd("n",2,Now)
        'WScript.Echo "in next"
        Do While Now <= timestr
            
            Set colProcesses = objWMIService.ExecQuery("Select * from Win32_Process Where Name = 'Setup.exe'")
            For Each objProcess In colProcesses
                sngProcessTime = ( CSng(objProcess.KernelModeTime) + _
                CSng(objProcess.UserModeTime)) / 10000000
                'WScript.Echo sngProcessTime & "in 2"
            Next
            
            If sngProcessTime > 0.5 Then
                WScript.Echo ">" & sngProcessTime
                ststusnew=True
                Exit Do
            End If	
        Loop
        If ststusnew=False Then
            Call Reboot
            status=True
        End If
    End If   
Loop

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Closing Comment

by:tneubauertocg
ID: 35157443
Prashanth - this is brilliant, it works like a charm!  Thank you very much!
0
 
LVL 12

Expert Comment

by:prashanthd
ID: 35157965
Glad it worked!! Thanks for the points..
0
 

Author Comment

by:tneubauertocg
ID: 35198841
I did find one thing missing that makes it bullet proof.  It was retaining the sngProcessTime value so I had to insert "sngProcessTime = 0" right before the "For-Next" loop on line 7. So now it looks like:

Set colProcesses = objWMIService.ExecQuery("Select * from Win32_Process Where Name =" & strProcessName)
sngProcessTime = 0
    For Each objProcess In colProcesses
        sngProcessTime = ( CSng(objProcess.KernelModeTime) + _
        CSng(objProcess.UserModeTime)) / 10000000
        'WScript.Echo sngProcessTime & " - 1 "
    Next
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now