I recently attended a Windows Server 2003 training seminar. During our class discussion, the instructor mentioned several operations, a few of which were scriptable. Out of the approximately 20 students in the room, all of whom were experienced systems administrators, only a few of us had any scripting experience. The others wanted to hear more after they learned about the possibilities. As a scripting evangelist, I often discover that once systems administrators get bitten by the scripting bug, they're hooked for life. Scripts let you accurately and quickly perform repetitive functions. So, for example, knowing how to write a script to capture information from several remote nodes and create meaningful reports can make the task fun instead of drudgery.
For many systems administrators, the first question is, How do I get started? For the beginner, the difference between scripting 1 line of code to display the words "Hello World" on screen and scripting 200 lines of code to perform a meaningful task is a quantum leap. If you're new to scripting, you probably just want to know what scripting can do for you. Often, the best approach is to start by running some simple but useful commands. Then, after you master the basics, you can turn to more advanced resources, including books about scripting, to enhance your skills.
I've created 101 "how to" scripting commands to answer 101 of the most common "How can I?" questions, and I provide some sample script code to get you started. These examples are designed to introduce you to the power of shell scripting, get you hooked on the potential of scripting for making your job easier, and provide you with greater safety, security, and accuracy when performing your daily tasks.
Getting the Scripting Commands to Work in Your Environment
Before you jump in, you need to ensure that the scripting commands presented in this article will work in your environment. Be aware that I tested these commands on Windows XP Professional Edition Service Pack 1 (SP1) and Windows 2000 SP3. I've provided the following prerequisites and tips to help you get started. Let's first consider the following prerequisites:
- Install the utilities that the code requires. For a list of these resources, see Web Table 1, http://www.winnetmag.com/windowsscripting, InstantDoc ID 41198, or refer to the path name hints I've used that apply to my test machine. Most of the tools are from the Microsoft Windows 2000 Resource Kit and Win2K Support Tools or are built-in commands, but a few are third-party freeware tools.
- Configure the code to include the path to the utilities you installed.
- Configure the Lightweight Directory Access Protocol (LDAP) path or other required information if the script doesn't prompt you for user input.
Next, let's consider a few tips that can make running the scripts easier:
- Many of the scripts use the Set /P command to ask the user for input. If you'd rather use hard-coded server names or usernames, remove "(SET /P C=Enter computer name: ) &" and replace the !C! or !U! variable with the hard-coded computer name, username, or other information for which the script prompts you.
- You can create a shortcut that in turn creates an environment in which to run the scripts. Typically, when you add code to a script, you set a variable, such as a computer name, in one line of code and use that variable in another line of code later in the script, such as you see in this example:
(SET /P C=Enter computer name: )
ECHO %C%
However, because we're going to create one-line scripts, you need a way to set the variable in one line. If you simply use the Set /P command on the same code line as the variable, you must operate in a command-shell environment in which the Setlocal command has been run with the optional argument Enabledelayedexpansion.
Instead of opening a command prompt and running the Setlocal Enabledelayedexpansion command every time you want to run a script, you can run the following one-line command to create a OneLinerCmd shortcut on your desktop:
C:\ResourceKit\Reg.exe add
HKCU\Console\onelinercmd ScreenBufferSize=131072080
REG_DWORD & shortcut.exe -f
-t "%systemroot%\system32\cmd
.exe" -a "/V:ON" -d %system
drive%\ -n "%userprofile% desktop\OneLiner Cmd"
(Although this command and the other one-line commands in this article wrap to multiple lines, lines of code that you cut and paste from this article might not work properly until you "unwrap" them to one line. You can download the code in the form of a properly formatted text file from the Windows Scripting Solutions Web site.) The code uses the Cmd /V:ON command to launch a script environment that achieves the same result as using Setlocal. Note that the shortcut that the script creates will change the screen buffer size to give you a larger scrolling window for your command output.
- Copy and paste the scripts into a command-shell window. Highlight the code you want to run, then right-click in the command window to paste in the code. If you can't paste in code, open the Command Prompt Properties window, select the Options tab, and ensure that the QuickEdit and Insert modes are enabled by selecting the associated check boxes.
- The commands and tools included in the scripts in this article offer many more options than I can describe. If you encounter differences running these commands in Windows 2003 or if you just want to see all the options that might be useful in your environment, type
Command Name /?