Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


February 18, 2000

Getting Started in NT Shell Scripting, Part 1


RSS
Subscribe to Windows IT Pro | See More Windows NT Shell Scripting Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

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.

The Scenario
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

  1. Determine the script's requirements.
  2. Write the pseudocode.
  3. Transform the pseudocode into a script.
  4. 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

Rmtshare \\work1

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:

  1. 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.
  2. 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

Rmtshare /?

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

Rmtshare \\work1\workingdir

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:

  1. Run the Rmtshare command to capture a server's share information.
  2. 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).
  3. Run the Rmtshare command again to obtain the absolute paths and remarks for the remaining shares.
  4. 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.

End of Article



Reader Comments
There is no article here??? 8182

kpearce February 05, 2007 (Article Rating: )


You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
Accessing Database Data with ADO

...

The Memory-Optimization Hoax

Don't believe the hype. At best, RAM optimizers have no effect. At worst, they seriously degrade performance. ...

Friday at PASS Europe 2006

Kevin talks about the closing day of the event and shares a funny Microsoft film. ...


Related Events Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.

Job Openings in IT


ADS BY GOOGLE SPONSORED LINKS FEATURED LINKS

Maximize your SharePoint Investment – 8 Cities
Discover best practices and tips for both architecting and administering SharePoint. Early Bird Price of $99 through Sept 15th.

Find a new job now on the all new IT Job Hound!
Search jobs, post your resume, and set up job e-mail alerts!

Master SharePoint with 3 eLearning Seminars
Learn how to build a better SharePoint infrastructure and enable powerful collaboration with MVPs Dan Holme and Michael Noel. Register today!

Top Tools for Virtualization Disaster Recovery & Replication
View this web seminar on August 14th to learn about two tools that will result in faster backup and restore with P2V disaster recovery.

SharePointConnections Conference Fall 2008
Don’t miss the premier event for Microsoft IT Professionals in Las Vegas, November 10-13. Register and book your room by August 25 and receive a FREE room night (based on a three night minimum stay).

VMworld 2008 - Sign Up Today!
Join your peers on September 15-18 at The Venetian Hotel in Las Vegas as VMware hosts VMworld 2008, the leading Virtualization event.



Entrust Unified Communications Certs
Secure Exchange 2007 and save 20%. Now through Sept. 2008.

Increase Application Performance
Free White Paper by Editor's Best winner, Texas Memory Systems.

Need to convert between XML, DBs, EDI, and Excel? Try MapForce free!
Drag & drop to transform between popular data formats – get results instantly or generate code.

Microsoft® Tech•Ed EMEA 2008 IT Professionals
Advance your thinking with new ideas and practical real-world solutions at Microsoft’s FIVE day technical infrastructure conference 3-7 Nov., 2008. Register before 26 September 2008 to save €300.

Order Your SQL Fundamentals CD Today!
Learn how to use SQL Server, understand Office integration techniques and dive into the essentials of SQL Express and Visual Basic with this free SQL Fundamentals CD.

Are You Really Compliant with Software Regulations?
View this web seminar that will help you with compliance best practices and check out a management solution to assure that you won’t be in jeopardy of an audit.

Virtualization Congress Oct. 14-16 in London
Don't miss Virtualization Congress, the premiere EMEA conference dedicated to hardware, OS and application virtualization. Oct. 14-16.
Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro Windows Dev Pro IT Job Hound ITTV
IT Library Technical Resources Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing