Microsoft

Scripting with the NT Resource Kit, part 1: The essentials

If you're like Richard Charrington, you may wonder about the usefulness of the NT Resource Kit. However, as Richard explains in the first of a series of Daily Drill Downs, it offers many powerful commands. For an introduction to this tool, read on.


I’ve always known about the NT Resource Kit. Up until a year ago, however, I never understood the usefulness of its strange commands. Whenever I looked through the directory, I thought, “Surely this stuff is only for geeks!” Then, I went contracting. During my first contract, I was asked to gather statistics on all of the servers in all of the domains to which I had access, to gather details about the configuration of these servers, to determine which servers didn’t conform to the standard, and to roll out McAfee Virus Scan updates on every server. There were no commands in my standard scripting armory that allowed me to accomplish what I was requested to do, so I took another look at the NT Resource Kit. Eventually, I was able to build some powerful scripts.

In this series of Daily Drill Downs, I will pass on some of the information that I learned and describe a few of the more useful NT Resource Kit commands that I found and used. As usual, I will supply a liberal amount of examples. I don’t intend on writing an NT Resource Kit manual because the Resource Kit comes with its own help files. You may have come across other useful commands that I don’t cover. If you have, please feel free to send me information and examples of how you used them. If there is enough feedback, I may write an update.

Where to begin
This week, I will ease you in gently by beginning with the easy stuff (the essentials, if you will). The first three commands are Choice, which allows for a limited form of user input; Now, which enables you to build a log of the sequence of events in your script; and Shutdown, which allows you to shut down NT from the command line. I will conclude with a utility instead of a command; AutoExNT allows you to have a script that will run when a system starts up—without forcing somebody to log on first.

Choice
Choice is a useful command that provides a way of getting some input from the person who runs the script. Although the input is limited and checking the responses can become very cumbersome, it’s still better than anything that Windows NT alone has to offer. There are third-party offerings that set an environmental parameter with the user’s response. When I am dealing with Windows NT servers, however, I prefer to limit myself to one or two official sources.

The syntax of Choice is as follows (with Table A containing definitions):
CHOICE [/C[:]choices] [/N] [/S] [/T[:]c,nn] [text]
Table A
/C[:]choices Specifies allowable key presses; default is Y/N
/N Doesn’t display choices and ? at the end of a prompt string
/S Treats choice keys as case sensitive; choice keys appear in the case that was specified (unless /N is used)
/T[:]c,nn Sets default choice to c after nn seconds (0 to 99)
Text Sets prompt string to display

Errorlevel is set to the offset of the key that the user presses in choices (see the example below). Choice accepts a single keystroke. If the switch /T:c,nn is used, then the key c is assumed to have been pressed (if nn seconds have passed and no key has been pressed). This information is useful for scripts that need to run with or without user intervention.
For those of you new to my style of scripting, I use the characters ::> in place of REM.
Here’s an example of how you can use this information:
Choice /C:csq /s Do you want to continue, skip or quit
If errorlevel 3 goto :EOF
If errorlevel 2 goto :Skip
::> rest of code

:Skip
::> code
You should see the following line:
Do you want to continue, skip or quit[c,s,q]?
If you press [q] (the third choice), the script will close. If you press [s] (the second choice), processing will skip to the label :Skip. Otherwise (if you make the implied first choice of [c]), processing will continue to the next line.

Just to get you thinking of the right way of doing things, here is a cleaner way of writing the above example:
Choice /C:csq /s Do you want to continue, skip or quit
Goto %ERRORLEVEL%
:1 ::> Continue
::> rest of code

:2 ::> Skip
::> code

:3 ::> Quit
goto :EOF
As you can see, this version gets rid of those awful if statements and explicitly handles the Continue Choice (see errorlevel 1).
To get the most from this series, when you see an example, try to use some of my techniques (or your own) to determine how the code could be improved.
Now
If you’re using the scheduler to run a script, you won’t know if it ran, what it ran, or if there were any problems. If you are creating a log file, this command will help you with the first two points. The syntax is:
NOW text
Now displays the current time, followed by “text.” It’s the same as the standard Echo command, except that Now has a time stamp. With this syntax as an example, the output would be:
Thu Jan 06 19:16:47 2000 — text
Placing such lines on either side of major processing sections in a batch file will enable you to spot any bottlenecks. Then, you can program around the bottlenecks or improve your script to reduce them, if possible.

Shutdown
If your script does something that requires a reboot, this command will make the reboot happen, and it won’t rely on user interaction. The syntax is as follows (with Table B containing definitions):
Usage: SHUTDOWN [/?] [\\Computer] [/L] [/A] [/R] [/T:xx] ["Msg"] [/Y] [/C]
Table B
/? Shows this screen
\\Computer Specifies a remote computer to shutdown
/L Specifies a local shutdown
/A Aborts a system shutdown only during the timeout period; all other switches are ignored
/R Specifies that the machine should reboot after shutdown
/T:xx Sets the timer for system shutdown in seconds (20 second default)
"Msg" Specifies an additional message
/Y Answers all following questions with yes
/C Forces running applications to close

Obviously, if you were using an unattended script, you would have to use the /C or the /Y switch to ensure that the server shutdown isn’t stalled by an application that needs confirmation to close when not saving changes. And a word of caution: If you use the /C switch, NT ignores the application's option to save data that may have changed. You won’t see a dialog box that asks if you want to save changes because NT will force the application to close. Thus, you’ll lose all information that wasn’t saved previously.

In a batch file, a typical shutdown statement might be:
Shutdown /L /R /C /T:30 “Administrative shutdown in 30 seconds. Call ext 123 to cancel.”
There are two important points that you should follow. First, you should use the /R switch to ensure that the server will reboot. If you plan to shut the server down and await the arrival of an engineer or an administrator, you must leave this switch out. Second, you should ensure that a useful message pops up on the server console in case someone’s working on it. Allot enough time for the user to respond and for you to cancel the shutdown, if necessary. This command has the capability of wreaking havoc, so think twice before you run it!

AutoExNT
Have you ever wanted to have a batch file that executed as soon as NT started, instead of having to wait for someone to log on? You could compile your batch file with a third-party batch-to-exe utility and install the resulting .exe file as a service. With NT Resource Kit, however, there’s an easier way, and you can leave your batch file in its editable form. Of course, you have to install a service (AutoExNT), but everything you need—including instructions—is available in the Resource Kit.

Installing the ‘AutoExNT’ service
  1. Copy the AutoExNT.exe and Servmess.dll files into the %Systemroot%/System32 directory.
  2. Use a text editor to create a batch file that contains the commands you wish to include and name it AutoExNT.bat.
  3. Copy AutoExNT.bat into the %Systemroot%/System32 directory.
  4. At the command prompt, type instexnt install.

When installation has completed successfully, the application will give you the following response:
>> CreateService ‘AutoExNT’ SUCCESS
with InterActive Flag turned OFF.
To view the installation processes that are running on the desktop, use the /interactive option. At the command prompt, type:
instexnt install /interactiveIf you use the /interactive option, any users who are logged on locally have the ability to stop the processes from running.
When installation has completed successfully, the application gives the following response:
>> CreateService AutoExNT SUCCESS
with InterActive Flag turned ON.
Testing AutoExNT
To test the AutoExNT service, log on as an administrator and type the following command at the command prompt:
net start ‘AutoExNT’
If the service starts correctly, AutoExNT.bat will run.

Configuring AutoExNT to run at startup
To configure the AutoExNT service to run when you start your computer, follow these steps:
  1. Double-click Services under Control Panel.
  2. In the Services list, click AutoExNT.
  3. Click Startup. In the Startup Type box, click Automatic.
  4. Choose OK.

Three facts to note
  • AutoExNT includes an /interactive option, just like the /interactive option of the At command. This option allows you to view the status of processes as they run.
  • Since no user-environment variables, including PATH, have been declared when AutoExNT runs at system startup, you must use full pathnames for all commands and file locations.
  • If you need to run a service, such as the Server or Schedule service, include a Net StartService command at the beginning of your AutoExNT.bat file before you start running a dependent process.
When you need to access an area outside of your local system (when you’re using the Net Use command, for example), click Logon As: | This Account and specify an account that has adequate permissions on the domain. Provide a password, if needed.
Conclusion
This time, I’ve covered a few simple commands and a technique for running a batch file before anyone logs on to a Windows NT machine. Next week, I’ll describe a few more simple commands that can help you create simple script. Then, I’ll move on to more technical commands, which allow you to do things in scripts that you may have thought you could only do in real time, online, or through GUI commands.

Richard Charrington’s computer career began when he started working with PCs—back when they were known as microcomputers. Starting as a programmer, he worked his way up to the lofty heights of a Windows NT Systems Administrator, and he has done just about everything in between. Richard has been working with Windows since before it had a proper GUI and with Windows NT since it was LANManager. Now a contractor, he has slipped into script writing for Windows NT and has built some very useful auto-admin utilities.

The authors and editors have taken care in preparation of the content contained herein, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for any damages. Always have a verified backup before making any changes.

Editor's Picks