Using command shell scripts has always been one of the best ways to manage a Windows server, but writing flexible scripts often requires extra command-line tools from the OS support tools, a resource kit, or a third-party vendor. Each new version of Windows has incorporated some of these tools into the base OS. Windows Server 2003 incorporates more than 60 new command-line tools. Here are my 10 favorite new scripting-oriented command-line tools in Windows 2003.
A longtime staple of the Windows resource kits, the Choice command prompts the user to choose an available option and returns the user's input to the script in the ERRORLEVEL environment variable. For example, to ask the user to select option 1, 2, or 3, you'd type
choice /c:123 Select an option
The Clip command lets you redirect a command's output to the Windows Clipboard or send the contents of the Clipboard to a command. For example, to redirect the Dir command's output to the clipboard, you'd type
dir | clip
The Setx command sets an environment variable according to values supplied in the script, in a file, or even in a registry key. For example, to set the MACHINE environment variable to the value Dell Precision 610, you'd type
setx machine "Dell Precision 610"
The Waitfor command synchronizes the actions of multiple systems on a network. Waitfor can pause a script until the command processor either receives a specified signal or sends a signal to one or more systems on the network. The following commands wait for the Batchdone signal, then use the /si switch to send the signal:
waitfor Batchdone waitfor /si Batchdone
Similar to the Pause command, the Timeout command pauses the execution of a script for the specified number of seconds. To pause a script for 10 seconds, you'd type
The Systeminfo command outputs basic system information to a file or to the standard output device. The information that Systeminfo returns includes the host name, OS version, BIOS version, processor type, system uptime, Windows directory, total system memory, and logon server. For example, to send system information to the mysystem.txt file, you'd type
systeminfo >> mysystem.txt
The Where command returns the location of all files that match a specified search pattern. To find all .inf files that reside in the C:\winnt directory, you'd type
where /r c:\winnt *.inf
The Forfiles command executes a command for each file that matches the supplied search criteria. You can use wildcards in filenames and search on dates. In the command to be executed, you can also use a special set of variables that contain environment information such as the filename, file date, and size. The following command displays the filename, date, and size for all files older than 5 days:
forfiles /d -5 /C "cmd /c echo @fname @fdate @fsize"
The Eventcreate command writes events to the event log. Eventcreate improves on the Logevent utility. The following command adds a custom log entry to the Application log:
eventcreate /t error /id 10 /l application /D "MyScript ended with an error"
The Eventquery command lets you extract selected entries from one or more event logs, including logs on remote systems. Unlike the other utilities I discuss, Eventquery is a Windows Script Host (WSH) script, so you need to use cscript.exe to run it. The following command filters the Application log and looks for the custom log entry that I used Eventcreate to create:
cscript c:\windows\system32eventquery.vbs /l application /fi "id eq 10"