Writing Terminal Service Based Scripts (Part 9)

by [Published on 9 July 2008 / Last Updated on 9 July 2008]

This article continues the series on terminal server scripting by talking about menus and subroutines.

If you would like to read the other parts in this article series please go to:

In the previous article in this series, I finished showing you how to create log files that use dynamic file names. In this article, I want to continue the series by moving on to another topic. In this article, I am going to show you how to build menus into your scripts. For the sake of simplicity, the sample menu that I will be using in this article has absolutely nothing to do with the terminal services. However, as the series progresses, I will take the concepts that I have taught you in this article and apply them to the terminal services. For example, using the techniques that I am about to show you and some of the techniques that we have talked about earlier in the series, you could create a script that displays all of the terminal server sessions that are currently active, and then provides menu options for terminating a particular session. I am not necessarily saying that that is the exact script that I am going to be showing you later this series, it was just the first example that popped into my head. The point is that very soon we will be taking all of the concepts that you have learned so far, and applying them to the terminal services.

Subroutines

One of the most important concepts that I want to teach you about in this article is the concept of subroutines. In a batch script, a subroutine is just a section of code that can be called from another section of code. A lot of times, subroutines are based on menu choices, but they do not have to be. For example, you could design a script that performs a calculation and then executes a particular subroutine based on the results of that calculation.

It is actually a whole lot easier to create a subroutine than you might think. Subroutines are defined by a colon, and a subroutine name. You can call a subroutine using the goto command. To show you how subroutines work, I have created these sample script below that I will be referencing throughout this article:

@echo off
set _car=D
:Loop
cls
Echo What is your favorite car:
Echo A: Lamborghini
Echo B: Ferrari
Echo C: Porsche
Echo.
Echo D: Quit
Echo.
Set /P _car=Please enter your choice:
if not "%_car%"=="" set _car=%_car:~0,1%
REM echo %_car%
If /i "%_car%"=="A" goto sub_lambo
If /i "%_car%"=="B" goto sub_ferrari
If /i "%_car%"=="C" goto sub_Porsche
if /I "%_car%"=="D" goto:eof
goto:loop
:sub_lambo
echo You chose a Lamborghini
goto:eof
:sub_ferrari
echo You chose a Ferrari
goto:eof
:sub_porsche
echo You chose a Porsche

The script above creates a very simple menu that is designed to ask the user with their favorite type of car is. The menu displays several choices, and then the user enters the letter that corresponds with their choice. After the user has made a choice, the script tells them what they have chosen. Granted, the script is not exactly exciting, but I wanted to create something simple that I could use to teach you some basic concepts.

If you look through the script, you can see that I have defined four separate subroutines. The subroutines are: loop, sub_lambo, sub_ferrari, and sub_porsche. You can see where each subroutine begins by looking for a colon in front of the subroutine name. Throughout the script, you will also notice several instances of the goto command that call the various subroutines.

One more thing that I want to point out before we move on is that there are several commands in the file that call a subroutine named EOF. I do not actually have to define the EOF subroutine, because Windows knows about it by default. The goto:EOF command tells Windows to terminate the script by going to the end of the file.

The Menu in More Detail

Now that I have explained how subroutines are called, I want to go through the menu line by line, and explain how it works. The first line is simply the @echo off command. It prevents Windows from displaying all of the commands as they are executed.

The second line of the script is Set _car=D. This command initializes a variable named _car to a value of D. The reason why I’m doing this is because further down, the script contains some code that causes the script to end if the user enters D. The script is designed in such a way that if the user doesn’t enter anything, and just presses Enter, that the script will use the default value of D, which I am assigning here, and will therefore terminate.

If all of this is starting to sound a bit confusing, then perhaps the flowchart below will help. You will have to ignore the directional arrows, as I do not know how to control which direction the arrows point in Visio. Even so, the flow chart should give you a good idea of the logic that the script follows, and of how the subroutines work.


Figure A:
This flowchart illustrates our sample script

Now that we have initialized or variable, the third line in the script creates a subroutine named loop. The reason why we're doing this is so that if the user enters an invalid menu choice, then the user's input will be ignored and the script will simply reset itself and give the user another chance to enter valid input.

The fourth line in the script, or the first line in the Looped subroutine is CLS. I cannot remember if I have talked about the CLS command in this article series before or not, but it just tells Windows to clear the screen.

The next several lines of the script are just a series of echo commands to display the menu choices. The only thing that is a bit out of the ordinary in this section of the script is that there are two lines in which the word Echo is immediately followed by a period. These commands tell the scripts to insert a blank line on the text on the screen.

That you'll encounter is the SET /P _car=Please enter your choice: command. In Windows, the Set /P command tells Windows to stop and wait for user input. The input is written to the variable that is specified. In this case, the variable is _car. This command also displays the text “Please enter your choice:”.

Next, the script processes the following command:

If not “%car%”==”” set _car=%_car:~0,1%

This is one of the trickier lines in the command. It is designed to parse the user’s input and make it valid. The command checks to see if the user has entered anything or not. If the user has typed any text before they pressed the enter key, then the user’s input is truncated. Starting at character 0 of the user’s input, this command captures the next character (hence the ~0,1 at the end of the command). This ensures that the script limits the user’s input to a single character.

You will notice that the next line in the script is:

REM echo %_cars%

REM is the Windows remark command. Commands beginning with REM are not actually executed. If you remove the REM command though, you can see the contents of the _car variable. If you look at Figure B, you can see that I have added this command before and after the command to parse the user’s input. When the script asked me to make a selection, I entered the word Apple instead of typing A, B, C, or D. The parsing command truncated the word Apple to just A, as you can see in the figure, which corresponds to the choice for Lamborghini.


Figure B: This is how the parsing function works

Conclusion

In this article, I have shown you how to create a simple menu script. In Part 10 of this series, I will continue the discussion by talking about how this script works. Once I finish the explanation, I will start adapting the script to work with the terminal services.

If you would like to read the other parts in this article series please go to:

Featured Links