Solved

VBS script to kill process after x amount of seconds

Posted on 2009-03-30
5
620 Views
Last Modified: 2013-12-26
I have a program that opens up microsoft word.  Sometimes that program hangs, which means that Microsoft word is still open.  I would like to get a script that will run lets say every 5 minutes and check to see how long Microsoft word has been open.   If it has been open for lets say 15 minutes or more, I want it to email me.  I have figured out how to make things email me but I have not figured out how to have it calculate how long a program has been open and then perform an action based on the result.  

Any help would be greatly appreciated.
0
Comment
Question by:djturizmo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 8

Expert Comment

by:jwarnken
ID: 24023032
You can use wmi to get the performance data that should get you on your way
strComputer = "." 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_PerfFormattedData_PerfProc_Process",,48) 
For Each objItem in colItems 
    Wscript.Echo "-----------------------------------"
    Wscript.Echo "Win32_PerfFormattedData_PerfProc_Process instance"
    Wscript.Echo "-----------------------------------"
    Wscript.Echo "ElapsedTime: " & objItem.ElapsedTime
    Wscript.Echo "Name: " & objItem.Name
Next

Open in new window

0
 

Author Comment

by:djturizmo
ID: 24023132
What is the 48 for?
0
 

Author Comment

by:djturizmo
ID: 24024349
OK this is what I have so far.  Can you help with it?  This script runs with no errors but It seems like it doesn't return and instance of winword.exe

strComputer = "."
strWaitTime = 15
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
    "Select * from Win32_PerfFormattedData_PerfProc_Process Where Name = 'WINWORD.EXE'")

      If ElapsedTime > strWaitTime Then
            Set objEmail = CreateObject("CDO.Message")
      
            objEmail.From = "TEST@TESTcom"
            objEmail.To = "TEST@TEST.com"
            objEmail.Subject = "Status Hung"
            objEmail.Textbody = "Status Hung"
            objEmail.Configuration.Fields.Item _
                ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
            objEmail.Configuration.Fields.Item _
                ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
                    "blah.blah.local"
            objEmail.Configuration.Fields.Item _
                ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
            objEmail.Configuration.Fields.Update
            objEmail.Send

      End If
0
 
LVL 8

Accepted Solution

by:
jwarnken earned 50 total points
ID: 24030169
This will work. I added an echo so you can be sure to see it process the results of the query.

Also the ElapsedTime value is in seconds so I changed your wait time to 900 (15 min.)


strComputer = "."
strWaitTime = 900
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
Set colItems = objWMIService.ExecQuery("Select * from Win32_PerfFormattedData_PerfProc_Process Where Name = 'WINWORD'")
For Each objItem in colItems 
	Wscript.Echo "Name: " & objItem.Name
	Wscript.Echo "ElapsedTime: " & objItem.ElapsedTime
	If objItem.ElapsedTime > strWaitTime Then
            Set objEmail = CreateObject("CDO.Message")
            objEmail.From = "TEST@TESTcom"
            objEmail.To = "TEST@TEST.com"
            objEmail.Subject = "Status Hung"
            objEmail.Textbody = "Status Hung"
            objEmail.Configuration.Fields.Item _
                ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
            objEmail.Configuration.Fields.Item _
                ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
                    "blah.blah.local" 
            objEmail.Configuration.Fields.Item _
                ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
            objEmail.Configuration.Fields.Update
            objEmail.Send
 
      End If
NEXT

Open in new window

0
 

Author Closing Comment

by:djturizmo
ID: 31564516
Thanks for the help, that did the trick.
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

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…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

691 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