Writing Terminal Service Based Scripts (Part 3)

by [Published on 16 Jan. 2008 / Last Updated on 16 Jan. 2008]

This article continues the series on writing terminal service based scripts by discussing more of the commands that are available to you.

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

So far in this article series, I have shown you several different commands that you can use to derive information about your terminal service environment. There are still several more commands that I want to show you how to use, and in this article I will continue the discussion by demonstrating a few of these commands. Eventually, I will conclude the series by showing you several different scripts that you can create based on the commands that I have shown you. You can use these scripts to automate various terminal service maintenance tasks. With that said, let us get started by talking about some more commands.

Query User

The Query User command is designed to allow you to see information about the users who are currently connected to the terminal server. In its simplest form, you can just type Query User to view a report of all of the users who are connected to the terminal server, as shown in Figure A.


Figure A: The Query User command allows you to view some basic information about all of the users who are connected to the terminal server

The first thing you probably noticed about the Query User command is that it displays information related to the server that you are currently using. However, there is a switch that you can use to examine a different server. This is helpful if you want to take a look at a server from a workstation, or if you are building a script based on the Query User command and you do not know exactly which machine the script will be run on. To direct the Query User command to run on a specific server, enter the following command:

Query User /SERVER:servername

You can see an example of how this command works in Figure B.


Figure B: You can direct the Query User command to run against a specified server

The other interesting thing about the Query User command is that you can filter the information that is returned based on user name, session name, or even session ID. You do not have to specify a command line switch when you filter for specific information. All you have to do is to type the information that you are filtering for, although you can use a filter in conjunction with the /SERVER switch.

For example, if you take a look at Figure C, you will notice that the two commands shown in the figure are identical, except that the second command has a number 1 appended to the end of it. By doing so, I have created a filtered view that shows me session ID number 1 on a specific server.


Figure C: By adding the number 1 to the end of the command, I have created a filtered view in which session number 1 is displayed

The same concept applies if you want to filter on another piece of information. If you take a look at Figure D, you'll notice that the command syntax is not changed at all. The only thing that I have done differently is replaced the session ID with a username. In doing so, I am able to create a filtered view of all of the sessions that were opened by a particular user.


Figure D: You can append a username, session name, or session ID number to the end of the Query User command

Basic Commands

So far in this article series, I have shown you some of the more complex commands that you can use to interact with a terminal server from the command line. I want to spend the remainder of this article talking about some of the more basic commands that you can use. There are still some more fairly complex commands that I want to show you, but I will cover those in future articles.

Reset Session

As you would probably expect, the Reset Session command allows you to reset a session that has hung. The actual command syntax is simple. Simply follow the Reset Session command with either the session name or session ID. You can also specify the name of the server containing the session that you want to reset, but this is only necessary if the session is running on a server other than the one that you are currently attached to.

The command’s syntax allows you to append a session name or session ID directly to the end of the Reset Session command. If you want to specify a server, you will have to use the /SERVER:servername switch. There is also a /V switch that you can use to view the results in more detail.

LOGOFF

The Logoff command allows you to end a particular user’s terminal server session. The Logoff commands syntax is completely identical to the syntax used by the Reset Session command. Essentially, this means that you can control which user you want to log off by appending either a session name or session ID number to the logoff command. Like the Reset session command, you can use the /SERVER switch to specify a server name, and you can use the /V switch to request more detailed output.

TSDISCON

It is important to understand that there is a difference between logging a user off, and disconnecting the user's session. Technically, it is possible for session to be connected even if nobody is actually logged on to that session. This is where the TSDISCON command comes into play. The TSDISCON command allows you to disconnect a session from the terminal server.

Once again, this command's syntax is identical to the syntax used by the Reset Session command and by the Logoff command. You can see a breakdown of the syntax below:

TSDISCON [sessionid | session name] [/SERVER:servername] [/v]

  • Session ID: the session ID number
  • Sessionname: the name of the session
  • /SERVER:servername: By default the TSDISCON command uses the current server, but this optional parameter allows you to specify a different terminal server.
  • /V: This optional parameter allows you to receive more detailed (verbose) output.

Keep in mind that the syntax that I have just outlined can be used with the TSDISCON, LOGOFF, or the RESET SESSION commands.

TSSHUTDN

THE TSSHUTDN command allows you to completely shut down, or reboot the terminal server. Although there are several switches that can be used with this command, the command itself is fairly self explanatory. The command syntax is listed below:

TSSHUTDN [wait time] [/SERVER: server name] [/reboot] [/powerdown] [/DELAY:delay time] [/V]

  • Wait time: The number of seconds to wait before terminating user sessions (the default is 60 seconds)
  • /REBOOT: Reboot the server
  • /POWERDOWN: Shut down the server
  • /DELAY: delay time: The number of seconds to wait after users have been logged off before shutting down the server (the default is 30 seconds)
  • /V: Verbose output

Conclusion

In this article, I have discussed several more commands that can be used when creating terminal service based scripts. There are still roughly about eight to ten more terminal service specific commands that I want to discuss. After that, I intend to cover some basic language syntax, and then plan to start showing you how to build scripts using the commands that I have discussed.

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

Advertisement

Featured Links