Editor's Note: This article is the first part of a two-part series about how to begin writing scripts in the Windows NT shell scripting language. This installment looks at how you go from having an idea for a script to determining that script's requirements and writing the pseudocode. The second installment will cover how to transform the pseudocode into a script and how to test the script.
What's the best way to get started in Windows NT shell scripting? I have four recommendations:
- Read Tim Hill's book Windows NT Shell Scripting (Macmillan Technical Publishing, 1998). This book is the best printed resource for learning NT shell scripting syntax.
- Find a mentor. Mentors are a great resource for reviewing your code for logic errors and debugging problematic code.
- Review and modify code that other scriptwriters have written. Dissecting other scriptwriters' code can help you see how they've accomplished certain tasks. You can then leverage their ideas to automate your system administration tasks.
- Master three NT shell commands (Findstr, For, and If) and the command-line utilities in the Microsoft Windows NT Server 4.0 Resource Kit. Most NT shell scripts use these tools to capture and manipulate data.
To help you master the Findstr, For, and If commands and one resource kit utility, I'll show you how to create a script that uses these tools. This exercise will not only teach you how the tools work but also give you a script that you can use in your NT system.
Suppose you're a junior systems administrator who is an aspiring scriptwriter. You work in a server farm that has several file servers containing client documents. Occasionally, the shares are accidentally deleted or get lost during system outages. Related information (i.e., share names, paths, and descriptions) is also lost. The shares are difficult to rebuild because some shares have different names than the folders in which they reside and other shares are deep within the directory structure. Thus, systems administrators must perform most of the recovery from their recollections about the lost shares.
You want to distinguish yourself as a valuable asset on the IT team, so you volunteer to write an NT shell script, ShareCapture.bat, to solve this problem. ShareCapture.bat periodically captures share settings. Its output creates another script, RecreateShare.bat, which rebuilds shares if they disappear. Your boss supports your scriptwriting aspirations, so he gives you a development server named Work1 on which to plan, develop, and test your script.
To turn your idea into reality, you need to
- Determine the script's requirements.
- Write the pseudocode.
- Transform the pseudocode into a script.
- Test the script.
Determining the Script's Requirements
To begin, install the resource kit on a development server called Work1 (or something similar) and create six test shares: C-Drive, Collaboration, Engineering, Sales, SoftwareTeam, and WorkingDir. In the resource kit, locate the remote share utility Rmtshare.exe. This utility gives you the basic share information you need. To run the utility's Rmtshare command, type
at the command prompt. Figure 1 shows the results. Note that I've numbered the lines in Figures 1 and 2 for clarity. These numbers don't appear in the actual results. Lines 1, 3, and 16 in Figure 1 are blank lines.
Figure 1 shows two problems with Rmtshare's output:
- Rmtshare mixes the information you need with information you don't want. You want to rebuild only the file shares, so you don't want the lines containing information about other types of shares, such as default shares (e.g., C$, D$) and administrative shares (e.g., ADMIN$, IPC$). Rmtshare doesn't support setting up printers, so you might also need to filter out any printer shares, even though the example output in Figure 1 doesn't include any. (See the resource kit's Help files for the information about Rmtshare and printers.) In addition, you don't want miscellaneous lines, such as line 17, which says the command completed successfully.
- Rmtshare truncates long paths. For example, in line 15, Rmtshare truncated the path for the WorkingDir share to C:\Collaboration\TeamA\Worki. Truncation presents a problem because you need an absolute path (i.e., a fully qualified path that contains drive and folder information before the filename) to rebuild the share.
To solve the first problem, you can use the Findstr command, which searches for strings, and the For command, which iterates commands. With the Findstr and For commands, you can find and eliminate any miscellaneous lines and any lines containing default and administrative shares.
To solve the second problem, you can use the Rmtshare command's options, or switches, to customize the output. To learn about Rmtshare's switches, type
at the command prompt. Rmtshare's syntax information will appear, revealing that if you specify a share name as a switch to Rmtshare, the command returns additional details about that share. Therefore, if you run the command
you'll obtain the results in Figure 2. Of interest are lines 2 and 3. Line 2 contains the WorkingDir share's absolute path, and line 3 contains that share's description, or remark.
Writing the Pseudocode
The output in Figures 1 and 2 provides all the information you need to pseudocode your script. Pseudocoding is the process of writing, in sentence form, the tasks that you want the script to accomplish. In this case, you want the script to perform four tasks:
- Run the Rmtshare command to capture a server's share information.
- Filter out the lines in the Rmtshare output that you're not interested in (i.e., the lines containing the default and administrative shares and miscellaneous lines).
- Run the Rmtshare command again to obtain the absolute paths and remarks for the remaining shares.
- Use the output from the second Rmtshare command to create the script RecreateShare.bat.
Now that you've determined the script's requirements and wrote the pseudocode, you're ready to transform the pseudocode into a script. I'll show you how to accomplish this transformation next month. In addition, I'll show you how to test and run the ShareCapture.bat and RecreateShare.bat scripts.