Terminal Service - Handle session by SQL Server Transaction

I'm sharing with you some commands which you can use to handle some server sessions:


--// Identify who are connected:
xp_cmdshell 'query session'


--// Identify the server name
select @@servername


--// Send the alert message (change the server name and ID number):
xp_cmdshell 'msg /server:server_name 3 Please, disconnect from this session in 5 minutes!'


--// Kill the ID connection (change the server name and ID number):
xp_cmdshell 'logoff /server:server_name 3 /V'


--// Get more information
xp_cmdshell 'query session /?'


Sometimes we need to access the server and to don't remove all connections with the command "mstsc /admin", these commands upper can help you to decide who will be killed.


Be careful to never execute command like open the pop-up ex: mstsc by xp_cmdshell, because it can be a big problem!