Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Inactive window process termination script for Windows 2000/XP

Posted on 2007-03-26
6
Medium Priority
?
432 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
[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
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

650 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