Customize Business Desktop Deployment with Batch Scripts

By Ralph Ramos

Ralph Ramos

Learn how to use batch files to customize your desktop deployment. TechNet highly recommends that you also visit the Script Center to learn how to create custom solutions using Microsoft Visual Basic Scripting Edition (VBScript), Windows Script Host (WSH), and various other scripting technologies.

Using Batch Scripts for Customization

The Microsoft Solution Accelerator for Business Desktop Deployment (BDD) is flexible and highly customizable. The Solution Accelerator for BDD includes several tools to get you started, but the ability to customize the Solution Accelerator to fit your needs is where its strength lies. I like to use batch scripts, and in this article, I provide some examples of how easily you can use these scripts to customize the Solution Accelerator for BDD.

A batch, or shell, script is a list of commands saved in a plain text file with a .bat or .cmd file extension. For example, you can save organization-specific configuration steps in a batch script, and then execute the script instead of having to type all the commands interactively. Many administrators overlook batch scripts as limited in their overall functionality relative to other scripting languages. But I disagree: Just as each tool has a specific use, so do the various scripting languages.

Because of the simplicity of batch scripting, you can easily use it to customize the Solution Accelerator for BDD and automate tasks during the installation process. (In fact, large portions of the Solution Accelerator for BDD are batch scripts.) These scripts don’t need to be complex. The Solution Accelerator for BDD is best suited to using these scripts to perform a single task, so you can easily choose individual tasks.

To get the maximum benefit from batch scripts, create scripts that automate repetitious and frequently performed tasks?for example, user configuration and configuration of remote access (Remote Desktop and Remote Assistance). The common goal in these scenarios is to provide automation through consistent, repeatable methods that minimize human error.

Batch Scripting Essentials

Most information technologies (IT) professionals are already familiar with batch script fundamentals. You create a text file, give it the .bat or .cmd file extension, and type a list of commands in it.

Batch scripting has much more power in it, though. First, varieties of command-line tools are available for use in batch scripts. If you search for “batch file” or “command line” in the Help and Support Center, you will find additional information on available commands. To get information about a specific command, type the command followed by /?. You can find a list of commands with their description and syntax at Command-line Reference. The Microsoft Windows Desktop Deployment Resource Kit also contains discussions of batch scripting and includes an appendix with commands.

Second, batch scripts support commands that you can use to control their execution. These commands include conditional and looping commands, which add a great deal of power to batch scripting. In this article, I focus on a few of the commands that can provide immediate results in your scripts. (However, I don’t discuss the syntax of each command.) Table 1 describes the more interesting batch script commands. To learn each command’s syntax, type the name of the command followed by /? at a command prompt.

Table 1. Command Batch Script Commands

Command

Purpose

Examples

call

Call another script, and then return to the next statement.

  • Call a batch script in another file.

  • Call a subroutine within the current file.

for

Repeat a command for a list of files or strings.

  • Repeat a command for a list of files stored in a text file.

  • Repeat a command for a list of files that another command outputs.

  • Repeat a command for a comma-separated list of strings.

goto

Go to another part of the batch script.

  • Unconditionally branch to another part of the batch script.

if

Conditionally execute a command.

  • Test whether a file exists.

  • Test whether an environment variable contains a particular value.

  • Test whether a command-line option exists or contains a particular value.

  • Test a previously run command for an error.

setlocal

Configure command extensions and delayed expansion.

  • Enable or disable command extensions.

  • Enable or disable delayed environment variable expansion.

start

Start an external program and control its execution.

  • Run a program with high priority.

  • Run a program in a minimized window.

wmic

Query Windows Management Instrumentation (WMI) data.

  • Load the environment with WMI data; then, use that data in your batch scripts.

Note:  Most batch scripts that perform consequential tasks require administrator privileges to avoid errors. In most cases, items that you configure in the registry or in the file system require these elevated privileges-particularly in locked-down environments.

Creating Custom Actions with Batch Scripts

A custom action in the Solution Accelerator for BDD is simply a task that you are completing when building a disk image. In my case, I’m using batch scripts to accomplish these tasks. The following examples show batch scripts that you can use as custom actions.

Example 1: Custom Command Prompt Window Properties

The script in Listing 1 customizes the Command Prompt window properties by using the reg command. This simple example shows how to edit a setting from a batch script.

Listing 1. Reg.exe

@echo OFFREM Configuring command promptreg add HKCU\Console /v "QuickEdit" /t REG_DWORD /d 1 /f

The reg command is the best way to edit the registry by using batch scripts. Using the reg command, you can do anything possible with Registry Editor. For example, you can read and write values, load and unload hive files, and delete values. For more information about this command, which comes with the Microsoft Windows XP operating system, type reg /? at a command prompt

Example 2: Custom Registry Search/Store Value for Future Use

Listing 2 is another example that uses the reg command. It queries the registry and uses the value returned to conditionally perform additional operations. In this example, the script identifies whether it is running on a Tablet PC and branches accordingly.

Listing 2. Reg.exe with Conditional Operations

@echo offrem Test for a Tablet PC registry key
reg query "HKLM\Software\Microsoft\Windows\CurrentVersion\Tablet PC\Ident" >NUL && set TABLETPC=YES
if %TABLETPC%==YES goto TABLET
goto OTHER
:TABLET
rem Do Tablet PC configuration
goto :EOF
:OTHER
rem Do other configuration
goto :EOF

The command in line 3 can be broken into two parts:

  • A reg command with a

    query

operation. The script searches for the Ident registry subkey. If the subkey doesn’t exist, the reg command returns an error.

  • Test for a condition. The

    &&

symbols represent the successful completion of the previous command. If the reg command finds the registry subkey (thereby not causing an error), the set command runs. If the reg command doesn’t find the subkey (thereby causing an error), the set command does not run.

You have now defined a variable that you can use later in a script. The next command determines whether the value of

TABLETPC

is equal to YES.

Example 3: Custom Copy Files to the Local System

The example in Listing 3 is a bit more complex. It loads WMI data in to the environment so that you can use it in the batch script. First, the script uses the wmic command with the for command to load the environment variables. The wmic command outputs one line for each value in the format name=value. The script takes advantage of this format by using the for command to repeat the set command for each line of output, appending the output to the set command. The result is that all the Win32_OperatingSystem values are in the

OS.Name

variables and all the Win32_SystemEnclosure values are in the FakePre-a5b9a0f74b5c4b42b8ba99ce31f25611-62c10adfefb245d5bf163d117b62c780 variables. There is tremendous power in being able to use WMI values from within batch scripts.

Listing 3. Reading and Using WMI Values

@echo offrem Test for a Tablet PC registry key
rem Dump WMI values in the environment.
for /f "delims=/" %%i in ('wmic PATH Win32_OperatingSystem GET /VALUE') do set OS.%%i >NUL
for /f "delims=/" %%i in ('wmic PATH Win32_SystemEnclosure GET /VALUE') do set ENC.%%i >NUL
goto %ENC.ChassisTypes%
echo "This is an unknown chassis."
goto :EOF
:{3}
:{4}
:{5}
:{6}
:{7}
:{15}
echo "This is a desktop chassis."
goto :EOF
:{8}
:{10}
:{12}
:{14}
:{18}
:{21}
echo "This is a laptop chassis."
goto :EOF
:{23}
echo "This is a server chassis."
goto :EOF

Adding Custom Actions to the Unattend Folder

As discussed in the Computer Imaging System Feature Team Guide, you use custom actions to customize the image. You can add custom actions anywhere in the Master $OEM$\$1 or $$ folders of the Computer Imaging System, but generally you should store them in the Master $OEM$\$OEM$\$1\Local folder.

To make troubleshooting your batch scripts easier, consider logging information in the Solution Accelerator for BDD log file at %SYSTEMROOT%\Setuplog1.log. You can include any helpful information in this log file, including error codes and tracing information. To output the result of a command to the log file, simply add >>%SYSTEMROOT%\Setuplog1.log to the end of the command. Doing so redirects the command’s output to the log file instead of the display. For example, to send text to the log file, add the line:

echo “This is log file text” >>%SYSTEMROOT%\Setuplog1.log

to your batch script.

After you’ve created a batch script that you want to use as a custom action, you must add it to the build by using the Computer Imaging System’s Actions page. To access the Actions page in the Computer Imaging System, click the Actions tab. The Actions page, shown in Figure 1, contains three areas.

Actions page

Figure 1: Actions page

At the top is a list of all the currently defined actions. For each action, you see an indication of whether it’s enabled and whether the system must restart after completing the action. Next is a row of buttons that you use to create or delete an action or move a selected action up or down in the list. The action list is processed sequentially. Finally, the bottom area provides the details of the currently selected action: Action name, Command line, Working directory, Reboot, and Enabled.

To create a custom action for your batch scripts, perform the following steps:

  1. Click New action.

  2. In the Action name box, type a name for your custom action.

  3. In the Command line box, type the command to run your batch script.

  4. In the Working directory box, modify the path, if necessary, or accept the default of C:\Local. (C:\Local is the final location of the Master $OEM$\$1\Local folder on the target computer.)

  5. Select the Reboot check box, if necessary.

  6. Select the Enabled check box to enable the selected action for your current deployment installation session.

  7. Copy the script to Master $OEM$\$1\Local in the build system.

Experiment with Batch Scripts

I encourage you to experiment with batch scripting-an underutilized tool that can make systems administration a lot easier. A functioning, well-tested script can help you reduce errors, become more efficient and effective, and gain valuable time that you can dedicate to the other tasks.

Many resources are available on the Microsoft Web sites, the Help and Support Center, and in command-line help that provide information and help you learn about batch scripting. In addition to the commands that are part of the Windows XP operating system, you can find other command-line tools in the Support Tools folder on the Windows XP installation CD.

For More Information

Discussions in Desktop Deployment
Ask your desktop deployment questions here. Discuss deployment tips and best practices with your peers, and give feedback on articles that are featured in the Desktop Deployment Center.

About the Author
Ralph Ramos, MCSE, is a full-time systems engineer for a software development company in Cincinnati, Ohio. He specializes in the Microsoft Windows product family and is currently experimenting with various scripting technologies. He was a technical reviewer for the Microsoft Windows Registry Guide (Microsoft Press, 2005) and Microsoft Windows Desktop Deployment Resource Kit (Microsoft Press, 2004). You can contact Ralph at rdramos@cinci.rr.com.

Print This Page  Print This Page

Rate This Page  Rate This Page