Categories Terminal Services

Writing Terminal Service Based Scripts (Part 11)



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



  • Writing Terminal Service Based Scripts (Part 2)
  • Writing Terminal Service Based Scripts (Part 3)
  • Writing Terminal Service Based Scripts (Part 4)
  • Writing Terminal Service Based Scripts (Part 5)
  • Writing Terminal Service Based Scripts (Part 6)
  • Writing Terminal Service Based Scripts (Part 7)
  • Writing Terminal Service Based Scripts (Part 8)
  • Writing Terminal Service Based Scripts (Part 9)
  • Writing Terminal Service Based Scripts (Part 10)
  • Writing Terminal Service Based Scripts (Part 12)
  • Writing Terminal Service Based Scripts (Part 13)
  • Writing Terminal Service Based Scripts (Part 14)

  • Throughout this article series, I have taken the time to show you numerous scripting techniques. Since a lot of administrators are not well-versed in scripting, I wanted to take the time to explain a lot of the basics rather than just jumping right into the terminal service related stuff. I really appreciate everyone's patience as the series has progressed, but now I finally feel as though I've covered enough ground that we can actually begin to do something useful.


    That being said, I want to take some of the techniques that I have covered in this article series, and show you how to use those techniques to build a session management script. Later on in the series, I am going to show you a technique for creating nested menus, and we are going to join our session management script to a collection of other scripts.


    Session Management


    If you go back to the beginning of this article series, you will recall that I showed you a number of different commands that were related to viewing or managing terminal server sessions from the command line. I do not want to repeat the full syntax of these commands, because I have already covered the syntax earlier in this series, but I do want to briefly mention the session management related commands that are available to you:


    Query Session – Displays information about the sessions that are running on the terminal server


    Reset Session – Resets a specific session to a known state


    TSCON – Attaches a user to an existing session


    TSDISCON – Disconnects a client from a specific terminal server session.


    Change Logon – Allows you to enable or to disable the user’s ability to log onto the terminal server.


    Now that I have refreshed everyone's memory as to which commands are available, the first project that I want to take on is to create a script based on these commands.


    As you can imagine, there are a lot of different ways that I could approach this type of script. The first thing that I want to do though is to create some code that allows administrators to know which terminal server they are working with, and to switch servers if necessary. After all, some of the commands that I mentioned a moment ago are server specific, and we really don’t want to be locked into using one specific server all the time.


    With that in mind, let’s start out by adapting the menu that we created in the previous article to the task at hand. If you look at the sample code below, you will see that it is basically the same as what we have already covered, I have just gotten rid of some menu choices, and added a choice for selecting a different server. The code is not really functional yet, I just want to use this as a starting point.


    @echo off
    set _menu=D
    :Loop
    cls
    Echo What do you want to do?
    Echo A: Select a Different Terminal Server
    Echo B: This choice is not yet available
    Echo C: This choice is not yet available
    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_server
    If /i "%_menu%"=="B" goto sub_nothing
    If /i "%_menu%"=="C" goto sub_nothing
    if /I "%_menu%"=="D" goto:eof
    goto:loop


    :sub_server
    echo This subroutine allows you to choose a different server
    @Echo Off


    goto:eof


    :sub_nothing
    echo Reserved for future code
    goto:eof


    As I mentioned earlier, some of the session related commands are server specific. The way the command’s syntax work, you do not have to specify a server, but if you do not specify a server then Windows will assume that you want to run the command against the current server. That being the case, there are two things that we are going to need to do.


    First, we have to assign a default server choice. The reason for this is that if we are going to design the script in a way that allows it to perform actions against multiple terminal servers, then the script is going to have to include a variable that contains the server name. We have to initialize this variable just in case the administrator forgets (or doesn’t want to take the time) to select a server.


    The other thing that we have to do is to provide the administrator with some sort of mechanism for connecting to a different server.


    Let’s tackle the server variable first. For the purposes of this article, we will just call the variable _SRV. If you look at the second line of the block of code above, you can see that we are already initializing one variable, and using it to assign a default menu choice. We can use this exact same technique to assign a default server. For example, in my organization I want the script to apply to a server named Mirage by default. Therefore, I would add the following command to the script:


    Set _SRV=Mirage


    I tend to think that just specifying a default server really isn’t enough though. In a large organization it can be difficult to remember which server you are attached to. Therefore, it seems prudent to add a command to the script that tells the administrator which server they are presently attached to. Therefore, I will add the following command to the menu loop:


    Echo The currently selected terminal server is %_SRV%


    You can see what this looks like in the code block below:


    @echo off
    set _menu=D
    set _SRV=Mirage
    :Loop
    cls
    Echo What do you want to do?
    Echo A: Select a Different Terminal Server
    Echo B: This choice is not yet available
    Echo C: This choice is not yet available
    Echo.
    Echo D: Quit
    Echo.
    Echo The currently selected terminal server is %_SRV%
    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_server
    If /i "%_menu%"=="B" goto sub_nothing
    If /i "%_menu%"=="C" goto sub_nothing
    if /I "%_menu%"=="D" goto:eof
    goto:loop


    :sub_server
    echo This subroutine allows you to choose a different server
    @Echo Off


    goto:eof


    :sub_nothing
    echo Reserved for future code
    goto:eof


    When we execute the script, it looks like what you see in Figure A. As you can see in the figure, the script displays the currently selected terminal server, just beneath the menu. The option to select a different terminal server doesn’t actually do anything yet, but we will write the code for that function in Part 12.



    Figure A: The script now displays the currently selected terminal server


    Conclusion


    In this article, I began constructing a script that will eventually allow you to manage terminals server sessions from a command prompt. After writing this article I realized that I have gotten ahead of myself. In Part 10, we began creating a script that would allow you to create, view, and print terminal service related log files. When I sat down to write this article, I was thinking that I had completed the previous script, but it later occurred to me that I promised to show you how to modify the previous script so that you can choose a specific report that you want to view or print.


    In the next article in the series, I want to backtrack a bit and show you this technique. Not only is the technique necessary for selecting log files, but we will have to use the exact same technique in the script that we began constructing above, because the process of selecting a different terminal server relies on this technique.


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



  • Writing Terminal Service Based Scripts (Part 2)
  • Writing Terminal Service Based Scripts (Part 3)
  • Writing Terminal Service Based Scripts (Part 4)
  • Writing Terminal Service Based Scripts (Part 5)
  • Writing Terminal Service Based Scripts (Part 6)
  • Writing Terminal Service Based Scripts (Part 7)
  • Writing Terminal Service Based Scripts (Part 8)
  • Writing Terminal Service Based Scripts (Part 9)
  • Writing Terminal Service Based Scripts (Part 10)
  • Writing Terminal Service Based Scripts (Part 12)
  • Writing Terminal Service Based Scripts (Part 13)
  • Writing Terminal Service Based Scripts (Part 14)
  • 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

    Samsung Unpacked 2020: Galaxy S20, Galaxy Z Flip, and more

    Samsung is again the first major company to roll out new smartphones in the new…

    2 hours ago

    PhotoSquared data leak exposes users’ photos, information

    PhotoSquared has experienced a data leak, mainly because the popular U.S.-based photo app failed to…

    5 hours ago

    Moving data from an Azure VM to Storage Account with AzCopy

    Here’s an elegant and modern way to move data from your Azure virtual machine to…

    21 hours ago

    A lot not to like: Analysis of recent Facebook data breach

    The effects of the recent Facebook data breach are still being felt. In this new…

    1 day ago

    Exchange 2019: Building an environment from scratch

    Are you finally ready to take the plunge into Exchange 2019? If you are building…

    1 day ago

    Cyber-extortion scheme targets Google AdSense users

    A cyber-extortion scam targeting Google’s AdSense users is making waves. Here are the facts that…

    2 days ago