Some time ago I implemented (actually still implementing and enhancing) MOM 2005 to monitor and manage a lot of servers (>600).
Some MOM Basics:
A nice feature in MOM is the ability to start tasks in the Operator Console. When you create a task you can specify variables in the command line. The most common variable is $Computer Name$ or $TargetComputer$. Using this placeholder you are able to dynamically fill in the computer name from the console selection in the command line. Because alerts and events are always related to a computer, you can start tasks from these views as well.
You can define local or remote tasks. For now we are just focusing on local tasks, which are command lines (scripts are not possible) executed on the machine running the Operator Console.
Some Remote Desktop Basics:
How to find out who has remote desktop connections to a server?
You have more than one choice to find out who is currently connected. One of course is Royal TS if the security context allows it, another is the Terminal Services Manager from the Administrative Tools (you may have to install the adminpak.msi first), the fastest way is to fire up a command prompt and type:
query session /server:[servername]
As you can see, the query session command lists all users connected. Next to the user name you see the session id.
So if you really need to get on that box you may want to kick out one of the users:
reset session 1 /server:web01
The command above kills the connection with the session id 1 from the user “Admin”. Note: if this user is still connected or has any opened documents, it’s all gone…
Some Command Shell Basics:
I wanted to have some sort of batch file which shows me all sessions, asks me if I want to kill one and kills the session id I entered. This batch file should look something like this:
Here a short description of the code:
The command line parameter %1 will be stored in the environment variable %ComputerName%.
Query session will be exectued.
SET /P asks for user input and stores it in the environment variable %uinp%.
Reset session will be executed with the session id the user provided in the line before.
Clear the screen and show again the sessions to verify it worked.
So I place the batch file in C:\batchfiles\resetsession.cmd on the machine(s) where the Operator Console is running and create a task with the command line: C:\batchfiles\resetsession.cmd $Computer Name$
All set. Except: I don’t really want to deploy batch files to all machines running Operator Console. So what can be done here?
There is a very useful feature in the command shell: executing multiple commands in one line. You can use “&”, “&&” and “||” to combine multiple commands. For example:
command1&command2 will execute command2 after command1 completed.
command1&&command2 will execute command2 after command1 completed successfully (ERRORLEVEL=0)
command1||command2 will execute command2 when command1 was unsuccessful (ERRORLEVEL<>0)
CMD /V:ON /C "QUERY session /server:$TargetComputer$&SET /P uinp=Enter Session ID to reset and press [ENTER]:&RESET session !uinp! /server:$TargetComputer$&CLS&ECHO Session reset command executed.&QUERY session /server:$TargetComputer$&PAUSE"
One thing to mention here is the parameter /V:ON and the different handling of the environment variable %uinp%. /V:ON allows to expand a variable at execution time but then you have to use “!” instead of “%”.
Instead of deploying batch files you can do more complex things in one command line. Paste this into the task command line and you are all set. Have fun.