Writing Terminal Service Based Scripts (Part 14)

by [Published on 3 Dec. 2008 / Last Updated on 3 Dec. 2008]

How to add additional terminal service functionality to our existing script framework.

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

Introduction

In the last part of this series, we integrated a function that allowed us to view the current terminal service sessions into the script framework that we have constructed. In this article, I want to conclude the series by adding a few more functions to our script.

Before I Begin…

It occurred to me that I did not provide the current code for our script at the end of the previous article, so I want to go ahead and show you what our script looks like as of right now:

@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: View the Current Sessions
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_session
If /i "%_menu%"=="C" goto sub_nothing
if /I "%_menu%"=="D" goto:eof
goto:loop

@Echo Off
Set /P _SRV=Please enter the name of the server that you want to work with
goto:Loop

:sub_session
Query Session /server:%_svr% /Counter
Pause
goto:Loop

goto:eof

:sub_nothing
echo Reserved for future code
goto:eof

One thing that I want to point out about the code above before we get started is that I have added the Pause command to the end of the :sub_session subroutine. This prevents the script from clearing the screen before you have had time to even read the output.

Resetting a Session

Now that our script is able to display session information, let us add a subroutine that will allow us to reset a session. We can accomplish that by way of the Reset Session command, but that command requires us to provide a session number. To accomplish this, we will ask the administrator for a session number, confirm the session number, and then reset the session.

The code used when resetting a session is a little bit tricky, but we are not using anything new. Here is the code:

:sub_reset
Set /P _sesnum=Please enter the Session ID that you want to reset
Echo The session that you are about to reset is:
echo.
query session %_sesnum%
echo.
Set /P _yn=Is this Correct (Y/N)
If /i "%_yn%"=="Y" then goto sub_reset2
Goto:loop

:sub_reset2
reset session %_sesnum%
goto:sub_session

As you can see, we are asking the administrator to enter a session number that they want to reset. We are reading that session number into a variable named _sesnum. We are then using the query session command to display just that session for confirmation.

At this point, we ask the administrator if that is the session that they want to reset or not, by asking them to enter either Y or N. The value is read into a variable named _yn. Incidentally, it is not shown in the block of code above, but at the beginning of the script, I initialized this variable to N so that nothing accidentally gets reset.

If the administrator enters N, then we drop back to the loop subroutine without doing anything. If the administrator enters Y, then we drop to a second subroutine names :sub_reset2, perform the actual session reset, and then go back to the script’s main loop.

Connecting and Disconnecting

As you may recall from earlier in the series we can use the TSDISCON command to disconnect a session, and the TSCON command to connect to a session. With very little effort, we can modify the code above to facilitate connecting and disconnecting sessions.

You can see an example of what the code that is used to disconnect a session looks like below:

:sub_disconnect
Set /P _sesnum=Please enter the Session ID that you want to disconnect
Echo The session that you are about to reset is:
echo.
query session %_sesnum%
echo.
Set /P _yn=Is this Correct (Y/N)
If /i "%_yn%"=="Y" then goto sub_disconnect2
Goto:loop

:sub_disconnect2
TSDISCON %_sesnum%
goto:sub_session

As you can see, this code is a direct adaptation of the code that we used to reset a session. By changing just a few characters, we can create subroutines for establishing a connection, as shown below:

:sub_connect
Set /P _sesnum=Please enter the Session ID that you want to connect to
Echo The session that you are about to connect to is:
echo.
query session %_sesnum%
echo.
Set /P _yn=Is this Correct (Y/N)
If /i "%_yn%"=="Y" then goto sub_connect2
Goto:loop

:sub_connect2
TSDISCON %_sesnum%
goto:sub_loop

Disabling Logon

The last thing that I want to do is to create some subroutines that you can use to disable or enable user logons. For this, we will need three separate subroutines. One will enable logins, one will disable logins, and one will disable new logins, but will still allow users to connect to existing sessions. The code for disabling logins looks like this:

:sub_Disable_Login
Change Logon /Disable
Goto:loop

As you can see, this is an extremely simple subroutine that uses the Change Logon command with the /Disable switch to disable logins. As I am sure you have guessed, you can enable logins by using the /Enable switch. If you want to disable new logins, but allow reconnections to existing sessions, then you would use the Change Logon command with the /Drain switch.

Our completed Script

The listing below shows our completed script. As you can see, I have had to add a few menu options to account for all of the new functions that we have added. I have also added a line of code near the top of the script that shows us whether or not logons are currently enabled. Here is the completed script:

@echo off
set _menu=D
set _SRV=Mirage
set _YN=N
:Loop
cls
Echo What do you want to do?
Echo A: Select a Different Terminal Server
Echo B: View the Current Sessions
Echo C: Reset a Session
Echo.
Echo D: Disconnect a Session
Echo E: Connect a Session
echo.
Echo G: Disable User Login
Echo H: Prevent New User Logins
Echo I: Enable User Login
echo.
Echo J: Quit
Echo.
Echo The currently selected terminal server is %_SRV%
Change Logon /Query
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_session
If /i "%_menu%"=="C" goto sub_reset
If /i "%_menu%"=="D" goto sub_disconnect
If /i "%_menu%"=="E" goto sub_connect
If /i "%_menu%"=="F" goto sub_reset
If /i "%_menu%"=="G" goto sub_Disable_Login
If /i "%_menu%"=="H" goto sub_Drain_Login
If /i "%_menu%"=="I" goto sub_Enable_Login
if /I "%_menu%"=="J" goto:eof
goto:loop

@Echo Off
Set /P _SRV=Please enter the name of the server that you want to work with
goto:Loop

:sub_session
Query Session /server:%_svr% /Counter
Pause
goto:Loop

:sub_reset
Set /P _sesnum=Please enter the Session ID that you want to reset
Echo The session that you are about to reset is:
echo.
query session %_sesnum%
echo.
Set /P _yn=Is this Correct (Y/N)
If /i "%_yn%"=="Y" then goto sub_reset2
Goto:loop

:sub_disconnect
Set /P _sesnum=Please enter the Session ID that you want to disconnect
Echo The session that you are about to disconnect is:
echo.
query session %_sesnum%
echo.
Set /P _yn=Is this Correct (Y/N)
If /i "%_yn%"=="Y" then goto sub_disconnect2
Goto:loop

:sub_disconnect2
TSDISCON %_sesnum%
goto:sub_session

:sub_connect
Set /P _sesnum=Please enter the Session ID that you want to connect to
Echo The session that you are about to connect to is:
echo.
query session %_sesnum%
echo.
Set /P _yn=Is this Correct (Y/N)
If /i "%_yn%"=="Y" then goto sub_connect2
Goto:loop

:sub_connect2
TSDISCON %_sesnum%
goto:sub_loop

:sub_reset2
reset session %_sesnum%
goto:sub_session

:sub_Enable_Login
Change Logon /Enable
goto:eof

:sub_Disable_Login
Change Logon /Disable
goto:eof

:sub_Drain_Login
Change Logon /Drain
goto:eof

goto:eof

Conclusion

I really hope that you have enjoyed this series as much as I have enjoyed writing it. I have always enjoyed the creative part of writing programs and scripts, but it is not something that I get to do as often as I would like to.

There are plenty of other terminal services related functions that you can accomplish through scripts, and I could probably continue writing this series indefinitely. Even so, there are other terminal server related topics that I want to write about, so I need to conclude this series so that I can explore some of those other areas.

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

Advertisement

Featured Links