Windows shell scripting can expedite network admin tasks
Some of the more common Windows scripting languages and platforms include Windows shell scripting, Visual Basic Scripting (VBS), and JavaScript.Now we're going to take an fully look at Windows shell scripting as it relates to network administration. For the purposes of this discussion, I'm going to assume that you understand the basics of using the Windows command line, including the use of parameters and switches, such as the following:
del? myfile.txt /f
Here, "del" is the command, "myfile.txt" is the parameter (which provides needed information to the del command—in this case, which file to delete), and "/f" is the switch, which modifies the behavior of the command so that it forces the deleting of read-only files.
Logon scripts
possibly the most common use for shell scripting is logon scripts. A logon script is used to organize the Windows environment for a user at the time of logon and is usually specific to a group of users. For example, members of the Finance group may automatically map network drives to the finance network share folder, while the Marketing group may automatically map their network drives to the marketing network share.
To make this work, a script is created for each user or group of users and then copied to the correct server location (based on the version of Windows). In Windows NT, the script file is normally placed in C:\Winnt\System 32\Repl\Export\Scripts (or introduce, depending on how you have configured replication). You then point to that file in the User Account Properties dialog box.
When using Active Directory, you organize the logon script via a Group Policy. First, copy the script file to the Sysvol subfolder. Figure A shows where to access this subfolder.
Finding the Scripts folders in Sysvol
Note that you also have the choice of scripts for logoff, startup, and shutdown. You can then directly edit the Group Policy object for a given container to include that script, as shown in Figure B.
Edit the script in a specific container in Active Directory.
Commands in logon scripts
One of the most useful commands to use in a logon script is NET USE. It's just one subset of many available NET commands.
The NET USE command allows you to set up drive mappings. When used in a logon script, it can adjust the drive mapping to a specific user or group of users. For example, let's say that the marketing department requires a drive mapping to the marketing folder on Server3, another drive mapping to the admin folder on Server2, and another drive mapping to the human individual user's home folder found in the "home folder" share on Server1. Here is an example of how you would script that:
NET USE F: \\server3\marketing PERSIST:NO
NET USE G: \\server2\admin PERSIST:NO
NET USE H: "\\server1\home folder\%username%" PERSIST:NO
The keyword PERSIST at the end of each line addresses whether to reconnect the drive mapping at the next startup. Most often, you do not want this to happen because another user may require different drive mappings.
You also should note a couple of things in the third line of code. First, there are quotation marks around the path. This is necessary because there is a space within the pathname. Second, I used the environmental variable %username%. When a user logs on, that username is stored temporarily in the registry and is available within the Windows shell. (To verify this, type echo %username% at the command line and you should see your own logon name provided as output.) If each user has a folder that is named the same as that person's logon name, you can use that to automatically map a drive to the home folder.
The %username% variable is one of some environment variables that are created automatically by Windows and that can be used similarly in your scripts. To see a complete list, you can type set at the command prompt. You can also create your own variables with this command. To find out how, type set /? at the command prompt.