Writing Terminal Service Based Scripts (Part 4)

by [Published on 6 Feb. 2008 / Last Updated on 6 Feb. 2008]

This article continues the discussion of terminal service related commands by talking about a command that you can use to send messages to terminal service users.

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

So far in this article series, I have demonstrated a number of different commands that you can use to build scripts for your terminal service environment. In this article, I want to continue the discussion by showing you some more commands that you can use.

The MSG Command

One of the most important parts of any script is its ability to keep the administrator or the users informed of the script’s progress. One way of accomplishing this is to design the script so that it transmits messages when key events happen. Fortunately, Windows provides us with a command that is perfect for this task.

The MSG command can be used to send a pop-up message to a user or to a group of users. The syntax is as follows:

MSG {username | session name | session ID | @filename | *} [/server: server name] [/time: seconds] [/V] [/W] [message]

The first part of this command allows you to specify the message’s recipient. As you can see in the command above, you can send the message to a particular user, or you can specify a session name or a session ID. The @filename option allows you to specify the name of a text file containing user names, session names, session IDs, or a combination of all three.

The last option for specifying a message recipient is an asterisk. If you specify an asterisk, then the message will be sent to everyone who is attached to the server. Using the asterisk is a great option if you need to inform everybody that the server will be shutting down.

The /Server:server name parameter allows you to specify the name of the server on which the action should take place. This is an optional parameter, and if it is omitted then the current server will be used by default.

The /V parameter allows you to request that the MSG command provide you with verbose information about the action that is being performed.

The /W switch tells the MSG command to wait for a response from the user.

You can combine the /W switch with the /Time: seconds switch. This allows you to tell the MSG command how long to wait for a response from the user.

The last parameter used with this command is the actual message itself. If you do not enter a message, then the command will prompt you for a message when the command is run.

Now that I have explained what the various command line switches do, I want to show you how to use this command. Let us start with something simple. The command below sends the message “This is a test” to the administrator:

MSG Administrator This is a test

If you look at Figure A, you can see what this command actually looks like. Figure B displays the output that is generated.


Figure A: This command represents one of the easiest ways of sending a message


Figure B: This is what the message looks like when it is received

There are a couple of different things that I want you to pay attention to in regards to the message that I just sent. First, notice that I did not have to use any kind of command line switch to specify the message recipient. All I had to do was to type the recipient’s username. I could have just as easily used a session name or a session ID, and the syntax would have been exactly the same.

The other thing that I want to point out about this command is not really visible in the screen shots. You probably noticed that I sent my test message to administrator. The reason why I chose this particular account is because in many organizations it is not at all uncommon for the administrator to be logged in on several machines at the same time (even though this is not really a good idea from a security perspective). My point is that not everyone who is logged in as the administrator will receive a copy of the message.

Remember that the commands that I am showing you are geared toward being used in a terminal service environment. As such, the command is specific to a certain target server. When you run the command, it looks for instances of the specified user being connected to the specified server, rather than searching the entire network for any instances of the specified user being logged into a domain. That is why you can also specify a session name or session ID as a part of the command. The session name and session ID are also server specific.

Normally, when you run the MSG command, the command assumes that you are trying to send a message to one or more users who are logged into the server that you are entering the command into. This is not always practical because you are not always going to work directly at the server console. Oftentimes, you will probably be doing server maintenance from your workstation rather than traveling to the server room.

Fortunately, the command allows you to use the /SERVER switch to specify the name of the server on which the command should be run. For example, suppose that I wanted to run the exact same command that I already showed you, but this time I wanted to enter the command into my Vista workstation, but have the command apply to a server named Mirage. To do so, I would enter the following command:

MSG Administrator /server:mirage This is a test

Upon entering this command, Windows would send the test message to anyone who is logged in as administrator through a session on the server Mirage.

Oftentimes, you may find that you want a message sent to multiple users. For example, you might need to send a message to the administrative staff when certain conditions occur. The problem is you never know who is going to be logged in at any given moment. One solution to this is to create a text file containing the names of the administrative staff. You can then tweak the command so that it will send a message to the user names on the list.

Creating the list is simple. Just open Notepad, and create a document in which each user name is on a separate line. After doing so, you can reference this file from the MSG command. For example, if you were to create a file named users.txt, the MSG command would look like this:

MSG @users.txt This is a test

Keep in mind that while it is usually easiest to enter user names into the text file, you can also include session names and session IDs. Likewise, you can also use the /server switch in conjunction with your text file, so that the command runs against a particular server.

Conclusion

In this article, I have begun explaining how the MSG command can be used to send pop-up messages to users who are attached to a particular terminal server. In the next part of this article series, I will continue the discussion by showing you more things that you can do with the MSG command. I will then go on to show you some more terminal service related commands.

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

Featured Links