Categories Terminal Services

Writing Terminal Service Based Scripts (Part 10)


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



In the previous article in this series, walked you through the creation of a simple menu. This menu had absolutely nothing to do with the terminal services, but in creating it, I explained several important concepts. Now that you know how menus work, I want to adapt the menu to a more terminal service specific purpose. As you may recall, in Part 8 of this series, I showed you how to execute the Query Users command, and write the results to a date specific log file. In this article, I want to take the concepts that you learned in parts 8 and 9, and build a script around them. For the sake of demonstration, we will create a simple script that gives you a choice of creating a log file, viewing a specific log file, or of printing a log file.


Adapting the Menu


As you may recall, the menu that we created in Part 9 was just a simple menu that asked you to enter a letter that corresponded to your favorite car. Once you made a choice, the menu would execute a sub-routine that would basically just tell you what car you had chosen. Although this type of menu is extremely simplistic, it is handy in that we can easily adapt it to work with the Query Users command.


For the sake of reference, here is the menu script that we created in Part 9:


@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


Below is a listing of the Query Users script from Part 8:


@Echo Off
Echo Creating log file.
for /F "tokens=2,3,4 delims=/ " %%i in ('date /t') do set datefile=%%i%%j%%k.log
Query User > %datefile%
Echo Log file created successfully.


Combining these two scripts is really simple. The first thing that I did was to get rid of the variable named Car and replaced it with something more suitable. Now, I am calling the variable Menu. I have also renamed the subroutines from Lamborghini, Ferrari, and Porsche to Create, View, and Print. Of course I have also had to revise the calls to the sub routines to reflect the new subroutine names. After I did that, I changed the menu text to display the new intent of our menu. Finally, I pasted the contents of the Query Users script into one of the subroutines. After doing so, the script looks like this:


@echo off
set _menu=D
:Loop
cls
Echo What do you want to do?
Echo A: Create a new Query Users report
Echo B: View a Previous Report
Echo C: Print a Previous Report
Echo.
Echo D: Quit
Echo.
Set /P _menu=Please enter your choice:
if not "%_menu%"=="" set _menu=%_menu:~0,1%
REM echo %_menu%
If /i "%_menu%"=="A" goto sub_create
If /i "%_menu%"=="B" goto sub_view
If /i "%_menu%"=="C" goto sub_print
if /I "%_menu%"=="D" goto:eof
goto:loop
:sub_create
echo You chose to create a report
@Echo Off
Echo Creating log file.
for /F "tokens=2,3,4 delims=/ " %%i in ('date /t') do set datefile=%%i%%j%%k.log
Query User > %datefile%
Echo Log file created successfully.
goto:eof
:sub_view
echo You chose to view a report
goto:eof
:sub_print
echo You chose to print a report


As the script is right now, we can choose option A, and the script will create a date specific log file containing the results of the Query User command. At the moment though, the options to view and to print the contents of a log file are basically empty, because I have been taught you how to use the necessary commands yet.


Viewing a Log File


Now that we have created a subroutine that creates a log file, I want to show you how to build a subroutine that allows you to view the contents of a log file. If we wanted to simply view the log file that was created today, then creating the subroutine would be simple. The subroutine would look like this:


:sub_view
echo You chose to view a report
for /F "tokens=2,3,4 delims=/ " %%i in ('date /t') do set datefile=%%i%%j%%k.log
Type  %datefile%.log |More
goto:eof


The subroutine above is fairly simple and straightforward, but it does use a couple of commands that you haven't learned yet. That being the case, let's go through it line by line. The first line simply declares the subroutine, and the second line is just a placeholder that prints the words You Chose to View a Report.


The third line of the subroutine is identical to a line in the script from Part 8 of this article series. This line simply takes the month, day, and year, and combines them into an environment variable named %datefile%. If you want to learn more about how this line works, there is a full explanation in Part 8.


The second to last line of the file contains both of the new commands. The Type command tells Windows to display the contents of a file. In this case, we don't have a specific file name to display, but we do have an environment variable that we know was used as a part of the file name. We also know that the log files that are created by our script are simple text files using the .LOG extension. Therefore, if we enter the command Type %datefile%.log we are telling the script to display the contents of a log file that was created today.


Just after the filename is the pipe symbol (I) and the word More. The reason why I have included this as a part of the command is because sometimes a log file might be too long to fit on the screen in its entirety. Normally, when that happens, the contents of the file simply scroll across the screen until Windows reaches the end of the file. When you add the pipe more command to the type command, it tells Windows to pause after each screen full of information so that you have time to read the information. Windows will not scroll to the next screen full until you press a key.


The last line in the subroutine is the goto: EOF command. As I have previously discussed, this just tells Windows that it has reached the end of the subroutine.


Conclusion


In this article, I have begun showing you how to adapt our menus into something terminal service specific. In the previous section I showed you how to use a menu option to display the contents of today's log file. Of course sometimes you may want to review a log file from a different date. In the next article in this series, I'll show you how to accomplish this. I will also show you how to build a subroutine that you can use to print the contents of the log file.


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


Brien Posey

Brien Posey is a freelance technology author and speaker with over two decades of IT experience. Prior to going freelance, Brien was a CIO for a national chain of hospitals and healthcare facilities. He has also served as a network engineer for the United States Department of Defense at Fort Knox. In addition, Brien has worked as a network administrator for some of the largest insurance companies in America. To date, Brien has received Microsoft’s MVP award numerous times in categories including Windows Server, IIS, Exchange Server, and File Systems / Storage. You can visit Brien’s Website at: www.brienposey.com.

Share
Published by
Brien Posey

Recent Posts

Amazon SES unveils new Bring Your Own IP feature

You’ve heard of Bring Your Own Device, and now there’s Bring Your Own IP. Here’s a look at this useful…

11 hours ago

Why API security is becoming the next big challenge

The shift to REST APIs has an unintended consequence for DevOps: new attack vectors. A security expert walks us through…

16 hours ago

Can ‘silent meetings’ solve your IT planning woes?

Companies are adopting the concept of silent meetings as a way to make business meetings more productive. Does this work?

19 hours ago

CES 2020: Latest innovations in laptops and smartphones

CES 2020 was more than just wild gadgets and crazy gizmos. There were some serious unveilings of new smartphones and…

1 day ago

WordPress vulnerability puts 300,000 at risk for attack

A WordPress vulnerability that could affect 300,000 users has been identified and patched. By if admins don’t update, they remain…

2 days ago

PowerShell jobs — because you have better things to do than wait

If you run PowerShell commands that take a while to complete, consider using PowerShell jobs, which will allow the command…

2 days ago