How to retrieve Session or ID Information from Command Line on RDP Server

We are currently running RDP on a Windows 2008 server.  Some of our users are utilizing generic accounts to access the systems then using a RemoteApp to access our primary software.

The issue we are running into is that our software has some issues and frequently we have to reset the sessions for users.  Now i have used the below script for users who are accessing from their own desk / login and it has worked wonderfully.  The problem I have is if I cannot query the session by UserName I am unable to return the right Session Name or Session ID.  This does not work for users in areas that use generic logins into the computer.

Now I will be the first to state that the shared/generic login is poor practice and needs to go, that is something I am working on but it taking time to get these changes in place.  

What I am trying to do is identify the Session Name or Session ID for the current connection from the computer the file is being ran from.  I have found a couple of things that look at registry but that has not been working.  In layman terms I want to be able to run a bat file that look at my current outbound connection to ServerName and reset it or reset all if there are multiple.  Is this possible in any way?  

Bat file I am using currently:
echo Current RDP Session ID: %RDP_SESSION%
for /f "tokens=1 delims= " %%G in ('query session %username% /server:ServerName') do SET RDP_SESSION=%%G
reset session %RDP_SESSION% /server:ServerName /v

Open in new window

Casy LaibIT ManagerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

EugeneZ ZhitomirskySQL SERVER EXPERTCommented:
query session /server:yourserver

 do you see all what you need to work with ?

can you post some dummy output that you'd like to "fix"
with dummy output after resetting unwanted sessions?
Casy LaibIT ManagerAuthor Commented:
When I query the session I get the below.  Device mane is blank otherwise I would be able to utilize that.  Also with shared UserNames I am unable to utilize that to kill the session.

Is there a way to query and return the Device information because then I would be able to make that work.  If not is there a way to query the server and just say show me my connections?

Thank you!!

C:\Users>query session /server:Server
 SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE
 services                                    0  Disc
 rdp-tcp#1         north                   2277  Active  rdpwd
 rdp-tcp#2         main                    2280  Active  rdpwd
 rdp-tcp#3         tech                     2281  Active  rdpwd
 rdp-tcp#4         north                   2282  Active  rdpwd
 rdp-tcp#5         od                        2283  Active  rdpwd
 rdp-tcp#6         sf                          2284  Active  rdpwd
 rdp-tcp#7         north                    2285  Active  rdpwd
 rdp-tcp#8         north                    2286  Active  rdpwd
 rdp-tcp#9         north                    2287  Active  rdpwd
 rdp-tcp#10        north                   2288  Active  rdpwd
 rdp-tcp#11        north                   2289  Active  rdpwd
 rdp-tcp#12        vj                          2290  Active  rdpwd
 rdp-tcp#13        north                   2291  Active  rdpwd
 rdp-tcp#14        north                   2292  Active  rdpwd
 rdp-tcp#15        north                   2293  Active  rdpwd
 rdp-tcp#16        north                   2294  Active  rdpwd
 rdp-tcp#17        north                   2295  Active  rdpwd
 rdp-tcp#18        bb                        2296  Active  rdpwd
 rdp-tcp#19        cs                           2297  Active  rdpwd
 rdp-tcp                                 65536  Listen

Open in new window

EugeneZ ZhitomirskySQL SERVER EXPERTCommented:
hmm. you may not be able to get   session Device using this method

 DEVICE, which is not present for the console or network-connected sessions, is the device name assigned to the session.

you can try to use " task manager" as an alternative
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

EugeneZ ZhitomirskySQL SERVER EXPERTCommented:
check this free Remote Process Explorer from Lizard systems-- remote Taskmanager
Casy LaibIT ManagerAuthor Commented:
The only problem with the Lizard Systems one is I need to do it from the command line, more importantly I need my users to be able to do it from their desktop without me having to.

I am able to if I have a unique username but not on shared workstations.  Just trying to find a way to see if there is a way to find the information from the current connection by the user or if I can somehow taskkill mstsc but have it send a reset command to the server.

I know the possibilities are pretty thin with the user name restriction but still trying to find a solution.
EugeneZ ZhitomirskySQL SERVER EXPERTCommented:
try these commands to see if it will help

quser /server yourserver  (see active users)
qwinsta /Server: yourserver  (like query session /server:Server)

as abd example
PowerShell script to Find RDP sessions on Servers
Casy LaibIT ManagerAuthor Commented:
Not having any luck, don't thing there is a way to get the the device name that would be needed or a way from the computer to send a reset session command.  I will keep searching but I greatly appreciate your help!
EugeneZ ZhitomirskySQL SERVER EXPERTCommented:
what about this one

Terminal Services PowerShell Module

or this one

Getting RDP Sessions with Client Computer Name

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Casy LaibIT ManagerAuthor Commented:
This one is the closest I have been able to get.  It is giving me the client name but I am unable to figure out how to filter it down just to my client name and then close the returned session number.  Thank you for your help with this but I am realizing I am at my limits and that there is going to be no way to do this via command line.  I will come back to this in the future and see if anything comes about, but not holding my breath.
Casy LaibIT ManagerAuthor Commented:
Appreciate all of the help and assistance
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.