Subscribe to Windows IT Pro
August 13, 2002 12:00 AM

Scripting Solutions with WSH and COM: More Short, Sharp Scripts

Windows IT Pro
InstantDoc ID #26024
Rating: (0)
Downloads
26024.zip

Here are more scripts like the ones I presented in "Short, Sharp Scripts," January 2002, InstantDoc ID 23366. I've used this mixed bag of scripts to perform a variety of tasks over the past month: killing programs, performing folder recursion, retrieving details about all the printers attached to a PC, retrieving a list of Microsoft SQL Server systems in a domain, and starting and stopping FTP sites. You might find these scripts useful for your scripting toolkit.

Some of the scripts use freeware ActiveX COM components that you need to download to run the scripts. These components sometimes offer more functionality than I use, so check out the URLs that I provide to find documentation that can help you expand and enhance the scripts.

For each script, the computer running the script is the source, and the system that the script connects to is the target. When the source and target are the same computer, you need to run the script locally on that machine.

Killing a Program
One useful tool is a script that terminates unwanted programs. You might need to kill all instances of a particular program because a fault has occurred, or during a file-copy operation, you might need to kill a program that has the file open. In such cases, you can open Task Manager and manually kill one or more processes, but you might want an automated solution when you need to terminate many processes.

Listing 1, page 2, shows KillingProcesses.vbs, which uses Windows Management Instrumentation (WMI) to iterate a client's processes, then kill each one. The script uses the WMI programmatic identifier (ProgID), the WMI connection string, and the WMI Query Language (WQL) Select statement. KillingProcesses.vbs terminates the processes by using a simple For Each...Next loop and the Win32_Process::Terminate method.

To use the script, replace TESTPC with the name of your target system and THEAPPTOKILL.EXE with the name of the application that you want to terminate on the target. You can run the script on any system that can access the target over the network. The account you use to run the script must have administrative privileges on the target system.

Web Listing 1, which you can download from http://www.winscriptingsolutions.com, InstantDoc ID 26024, contains the same code as Listing 1, but I've placed the code inside a subprocedure to show you how to build subprocedures for these code fragments should you choose to do so. You should pass the same two parameters to the subprocedure that I told you to replace in Listing 1. If you type in rather than download the scripts in Listing 1 and Web Listing 1, be sure to include a single quote (') before and after the name of the application to kill and a double quote (") before and after the entire Select string.

Related Content:

ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.