Solved

Inactive window process termination script for Windows 2000/XP

Posted on 2007-03-26
6
412 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 500 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
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.

760 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

20 Experts available now in Live!

Get 1:1 Help Now