?
Solved

Inactive window process termination script for Windows 2000/XP

Posted on 2007-03-26
6
Medium Priority
?
434 Views
Last Modified: 2011-09-20
We have a network-based application that has a limited number of licenses. Unfortunately, the vendor has no usage management ability within their licensing system, so users can open the application at 8:00 AM on a Monday and leave it "running" in the background for days at a time and thus needlessly consume a limited license. Management has asked us for a system to kick out users who are inactive. We need to create a Windows script that can be started when the system starts (likely as a service) that will monitor for the name of the process and whether it has been inactive (i.e. a minimized or background window) for an hour or more. The script, running on Windows 2000 Pro and XP Pro, would be required to check for this process every ten minutes. If it finds that the window has been inactive for over one hour, it would terminate the process. The following script is from the Microsoft site and we think it would provide the simple basis for this script (using Notepad.exe as an example):


strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colProcessList = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = 'Notepad.exe'")

For Each objProcess in colProcessList
    objProcess.Terminate()
Next


Any assistance would be greatly appreciated.
0
Comment
Question by:cnrlzen
  • 4
  • 2
6 Comments
 
LVL 13

Accepted Solution

by:
rettiseert earned 2000 total points
ID: 18795445
ok, first download and install the wonderful (and free) autoit:
http://www.autoitscript.com/autoit3/ 
(you may download the full package or just donwload and register the autoitx3.dll)

Then use the following VBS, you just need to change  WindowTitle value for the title of the window you wanna close (you may use the first word(s) only):

    Dim ai
    Dim MinutesToWait
    Dim WindowTitle
    Dim WindowText
    Dim MinutesElapsed
    Dim r
   
    MinutesToWait = 1
    WindowTitle = "Programmers"
    WindowText = ""
   
    Set ai = CreateObject("AutoItX3.Control")
   
    Do
   
        If ai.WinExists(WindowTitle) Then
       
            r = ai.WinWaitActive(WindowTitle, WindowText, 60)
           
            If r = 0 Then 'Timeout
                'A minute has passed and app was inactive all the time, increment counter
                MinutesElapsed = MinutesElapsed + 1
            Else
                'The app was activated, reset counter
                MinutesElapsed = 0
            End If
           
            If MinutesElapsed >= MinutesToWait Then
               
                'Use this to close window gently:
                ai.WinClose WindowTitle, WindowText
               
                'Use this to force window kill:
                'ai.WinKill  WindowTitle, WindowText
           
            End If
       
        Else
       
            ai.Sleep 10 * 1000 'Sleeps 10 secs
       
        End If
       
    Loop
0
 
LVL 13

Expert Comment

by:rettiseert
ID: 18795454
(you also need to MinutesToWait = 60 or any value you want to wait until closing inactive window)
0
 

Author Comment

by:cnrlzen
ID: 18796358
Thanks for the suggestion . We would prefer a solution that does not require us to deploy a third-party library that, although free, has no guarantee of being supported in the future. A purely VB script would be preferable.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:rettiseert
ID: 18800675
No dependency files is always better, of course, but since you are working with other processes I think that in some point you will need more power than a script file can give you.

AutoIt is an excelente library, and even if it is not supported in the future, you already have what you want (and more) with its current version. It is a very stable library and I think its even more supported than winmgmts (I find very little information for winmgmts, but autoit has an excelent help file).

Also, autoit its a great help if you want to write other kind of scripts. It has dozens of useful functions, and you anly need to distribute a single dll, and if you dont want that, then you can compile an autoit script into an exe and you will be distributing only that exe.

0
 

Author Comment

by:cnrlzen
ID: 18808524
We have something that appears workable but, as often is the case, the original project requirements have broadened and we may have to do this for a number of apps. Is it possible to modify the script to read a text file for a list of the applications (i.e. WindowTitle) to be monitored and closed when inactive? Each app may have its own time limit for each activity. Lastly, would there be a way to add an entry to the Application Event Log EVERY time the script closes an application? I've tried the following (again, from Microsoft) but it only seems to write one entry ever for the application closure:

Const EVENT_SUCCESS = 0
Set objShell = Wscript.CreateObject("Wscript.Shell")
objShell.LogEvent EVENT_SUCCESS, _
    "Payroll application successfully installed."

Thanks a lot for any help.
0
 
LVL 13

Expert Comment

by:rettiseert
ID: 18827032
Thanks for the points, I'm sorry I didn't see your question until now, but anyway, I have to tell you that right now I dont have the time to develop the entire script you need. I recoomend to create many scripts as the one I posted, one for each program you wish to close. you just need to change

WindowTitle = "The title of the window you wanna monitor"

hope this helps
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses
Course of the Month17 days, 8 hours left to enroll

830 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