A new way to get data into your scripts
If you've been creating command-shell scripts, you've probably experimented with all the various ways to input data into your scripts. If you still hard-code your data into your scripts, you might want to read "Command-Line Argument FAQs," December 2003, http://www.winnetmag.com/windowsscripting, InstantDoc ID 40514, to learn the pitfalls of that approach. I've recently discovered a new way to input data into your scripts. You've probably tried using menus, arguments, input files, and the Set /P command for script inputs, but have you tried using the Windows Clipboard? A third-party tool and a little creative coding are all that you need to feed Clipboard input into your scripts. This input technique works equally well with single- or multi-item data.
Extracting Clipboard Text
You're probably familiar with how to add text to the Clipboard by highlighting the text and pressing Ctrl+C or right-clicking and selecting Copy from the context menu. Then, typically, you press Ctrl+V or right-click and select Paste to paste the text into a text file or other document. What if, after you copied text to the Clipboard, you could channel the Clipboard contents into a script and run other commands against the Clipboard item or list?
The key utility you need to accomplish this task is the third-party tool clip2txt.exe from James Greene Informatics Consulting. The company's Web site has several other command-shell utilities that you might find interesting (http://www.informatics-consulting.ch/software/index.htm).
Clip2txt.exe has several switches, including the -n (or -nologo) switch, which suppresses banner output about the utility's copyright. You'll want to use this switch to avoid this unnecessary text. The -q (or -quiet) and -s (or -silent) switches duplicate the functionality of the -n switch. If you're going to use clip2txt.exe on scripts for which you want to echo the Clipboard contents directly to the console, you might want to use the -d (or -display) switch. In the three sample scripts I discuss, you don't need the -d switch because you're going to use a For command to set a variable with the Clipboard contents.
First, I demonstrate the Clipboard input concept with a simple Ping command script. I've chosen a simple ping task so that you can concentrate on the Clipboard extraction code and less on the operation code. You can replace the ping task with other more advanced operations, which I demonstrate how to do in a script that captures machine information and in an Active Directory (AD) and virus definitions capture script later.
Clipboard-Powered Ping Command
In the ClipboardPing.bat script that Listing 1 shows, I use a For command to parse the results of the clip2txt.exe utility and set the resulting text as a variable. This approach lets you enter one PC or an entire list of PCs to ping. The For command continues to process the output until it reaches the last PC. ClipboardPing.bat displays the output on the console. Optionally, you can redirect the output to a text file by placing the >> redirection symbol followed by a filename (e.g., C:\OutputFile.txt) after the final two Echo commands in the code.
To test ClipboardPing.bat, download clip2txt.exe and configure its location in the script. Copy either one or multiple PC names (one per line) into the Clipboard. This script assumes that your PCs' names don't contain spaces. After you run the script, you should get accurate online and offline information for each machine that you ping.
Clipboard-Powered AD Queries
Often I receive a computer name in an email message or I'm looking at a computer object in the Microsoft Management Console (MMC) Active Directory Users and Computers snap-in and want to get more information about that computer. With all the worms and viruses running around, I need an easy way to see the machine's ADsPath location, OS and service pack versions, stale password information, disabled state, versions of the virus-scanning engine and virus-definition file, and installation date of the remote procedure call (RPC) hotfix. Although the Active Directory Users and Computers snap-in provides some of these items, it doesn't include the stale password information and virus-scanning information I need. So, I created ClipboardPCQuery.bat, which Web Listing 1 (http://www.winnetmag.com/windowsscripting, InstantDoc ID 40880) shows.
In ClipboardPCQuery.bat, I use the same Clipboard input concept to capture the computer information I just described. Because I use the Net Send command to create a pop-up window but I don't want multiple pop-ups to appear on the console, I modified the code so that the script runs against only one computer. If the Clipboard input contains multiple lines, the script uses only the first PC name.
Clipboard-Powered Web Input
In my IT office, my coworkers and I have an intranet Web site that lets us retrieve friendly usernames (the first and last names of a user) by entering a user ID. Our account managers use this functionality by copying a user ID from email messages or trouble tickets and entering the user ID into a form. Then, the page calls another Active Server Pages (ASP) page by using the information entered on the form as a parameter. This step seemed unneccessary because the user ID was already in the Clipboard. So I created the ClipboardWebsearch.bat script to capture the user ID from the Clipboard directly and eliminate the step of pasting the ID into the form and clicking the submission button. Now, the account manager just copies the user ID and double-clicks the script that launches Microsoft Internet Explorer (IE), pointing to the page with the parameter.
To demonstrate this technique and give you the basic code that you can customize for your environment, I adapted ClipboardWebsearch.bat so that you can use it to search the Microsoft TechNet site for specific Knowledge Base articles. Listing 2 shows the adapted script. Just copy an article number from a Microsoft Security Bulletin or from the hotfix uninstallation directories on your Systemroot directory, and run the ClipboardWebsearch.bat script. You'll need to customize the script with the appropriate Web site on your intranet or on the Internet.
Putting the Scripts to Work
To get these three scripts working in your environment, perform the following steps:
- Download clip2txt.exe (http://gucc.org/greene/software/clip2txt.zip) and copy it locally. If you plan to run the scripts on more than one machine, copy the utility to a shared folder location.
- Configure the scripts to point to the utility's location.
- To run ClipboardPCQuery.bat, store the Windows Server 2003 DSquery and Windows 2000 Support Tools Search.vbs utilities locally or in a shared folder if you plan to run the script on more than one machine.
Using the Clipboard to bring data into your scripts can breathe new life into scripts in which you've hard-coded input or you've provided input through arguments or text files. Add this new technique to your scripting tool belt and begin to brainstorm ways you can use it to make life easier for yourself and other users in your environment.