Creating a Shell Scripting Tools Menu System
Make tools and utilities easy for anyone to use
May 4, 2003
Users often ask me to check user group memberships, troubleshoot desktop-PC or server problems, determine who's logged on to a PC, or perform other simple administrative or troubleshooting tasks. Usually, before the request is complete, I've typed a few commands and answered the question, much to the user's amazement. I can't count how many times someone has said, "That's incredible! Can you teach me how to do that, too?" This starts a cycle of me teaching the user a few commands, the user forgetting the commands, and me reteaching the commands the next time the task needs to be done.
If you don't use command-shell tools frequently, you can easily forget which command will accomplish a particular task or the command's usage syntax. Even experienced scriptwriters often need to refer to the online Help for the Microsoft Windows 2000 Server Resource Kit, third-party tool, or internal command to refamiliarize themselves with a command's syntax. Users on your security and support teams would certainly benefit from using command-line tools, but often they just don't use the commands frequently enough to remember which command or utility to use in a particular situation or the utility's syntax.
Using the Choice Utility with the Set Command
Before Windows XP and Win2K, you could use the Microsoft Windows NT Server 4.0 Resource Kit's Choice utility to create a menu of especially helpful tools, but you had no easy way to capture the user inputs most tools require and send the input to the tool as an argument. XP and Win2K provide new ways to use the Set command to capture user input and effectively run resource kit and third-party tools from a menu. When you use the Set command with the /p switch, you can capture users' keyboard input into an environment variable. For example, you can use the Choice utility to direct a script's flow to the code for a tool that you select from the menu. Then, to prompt the user for the input that the tool requires, you can use the Set /p command. Listing 1, page 2, shows an example of how you can use the Set /p command to capture the arguments that the Local command needs to run (i.e., the group name and domain).
In the script ToolsMenu.bat, I use the Choice utility and the Set /p command to create a menu from which users can run nine common tools and internal commands used to diagnose PC and server problems. The simple menu interface that the script creates can help even advanced users and administrators more quickly accomplish repetitive tasks.
In ToolsMenu.bat, I use the Choice command to present users with a rudimentary menu from which they can select options (e.g., letter A, B, C, D, or E). If the user enters an invalid choice, such as the letter Z, the Choice command triggers an audible warning and pauses until the user enters a valid value. The code that Listing 2 shows tests the errorlevel exit code that the Choice command returns to determine the user's menu selection and uses the Echo command to display a message that tells the user which key he or she pressed. ToolsMenu.bat then uses the Goto command to direct script flow to a label that marks the beginning point for a section of code for that menu option.
To create the menu and run the various tools, ToolsMenu.bat uses the Local, Global, Findgrp, Srvinfo, and Uptime tools from the Win2K resource kit. ToolsMenu.bat also uses Sysinternals' PsLoggedOn and PsExec tools, which you can download from http://www.sysinternals.com.
Customizing ToolsMenu.bat
You can download ToolsMenu.bat from the Code Library on the Windows Scripting Solutions Web site (http://www.winscriptingsolutions.com, InstantDoc ID 38666). As written, the menu that ToolsMenu.bat creates includes the following commands and tools: Local, Global, Fndgrp, PsLoggedOn, Uptime, Srvinfo, Ipconfig, Net Send, and Time Sync Check. However, the script provides space for 14 more utilities. To customize ToolsMenu.bat with additional tools, follow these steps:
Determine the additional tools you want to add to the menu. For example, the PsExec utility is handy for remote troubleshooting. Some commands, such as Ipconfig, will run on only a local machine, but PsExec lets you run many local commands against a remote PC. Keep PsExec in mind as you pick the tools for your arsenal. For some of the nine tools in ToolsMenu.bat's menu, you might also want to support additional switch options that I don't use in my environment but that might be useful in yours. Run each command with the /? switch to determine whether other options might be more appropriate for your environment. Use the code from ToolsMenu.bat's routines as a pattern to write routines for the tools and switches that you want to add to the script.
Determine the location of the utilities you plan to use. If they're installed locally, you can run the script as is, without further changes. If the utilities are on a server share, you'll need to specify that location at the start of the script. If some utilities are installed locally and others are on a server, you can modify the location in each section of code. However, best practice is usually to put all the code on a server to ensure that the tool always exists and is the correct version. For example, if your utilities are on the Tools share on SRV1, you'd add the line
Set UtilLocation=\SRV1tools
at the beginning of the ToolsMenu.bat script. If you're going to deploy the script to several individuals, you might find it easier to put the utilities on a share rather than install all the utilities locally on each user's PC. The code that follows the SET UtilLocation= command adds a trailing backslash if you specify a remote share. Putting paths in variables instead of hard-coding them is a scripting best practice you should follow. For more tips, see the sidebar "Best Practices for Scripting."
After you test the code and ensure that it's operational, you can send a shortcut to all command-shell newbies who want the benefits of the command shell but can't remember command names or syntax.
Introduce the Power of Scripting
ToolsMenu.bat is a useful script that packages frequently used troubleshooting tools and commands. It's also a great way to introduce users who have command-shell phobia to the power of command-shell scripting. Maybe a few of your scripting disciples will take the next step and start learning the commands after ToolsMenu.bat introduces them to the power of scripting.
About the Author
You May Also Like