Finding forgotten RDP sessions?

9,741

Solution 1

This should do it:

for /f %i in (servers.txt) do query user jsmith /server:%i  

You can eliminate jsmith if you want to display all usernames.

If used in a cmd script, double the percent sign:

SETLOCAL  
SET FilePath=%1%  
SET UserNameToSearch=%2%  
FOR /F %%i IN (%FilePath%) DO query user %UserNameToSearch% /server:%%i  
ENDLOCAL  

Solution 2

Why not just set GPOs for the server for session idle timeouts so if you forget about one it automatically logs you off after a certain amount of inactive time. Set it for a couple of hours or more. Worst case you forget when you leave for the day and the servers automatically disconnect you that night.

If you want to do this yourself and actually go look for these suckers you could easily script it out, here's the MSFT CLI docs for TS

Share:
9,741

Related videos on Youtube

Rob C.
Author by

Rob C.

Updated on September 18, 2022

Comments

  • Rob C.
    Rob C. over 1 year

    I work in an enterprise environment with hundreds upon hundreds of Windows servers (all in a single AD domain), both locally and in other countries. I'm often required to log into an RDP session on various servers and, sometimes, I may forget to log off before closing the window. Obviously, this is not ideal, and I'd like to know if anybody knows a way to scan a domain for remote sessions running from my (or another user's, for that matter) account. Thanks!

  • Rob C.
    Rob C. over 11 years
    Hi, thanks for the response. Unless I'm using these incorrectly, they don't seem to be able to do what I need which is, essentially, scan all the servers in my domain, and report back to me which ones have an open session with my username on it.
  • Dave M
    Dave M over 11 years
    Hmmm I have the MMC snapin configured so that all my servers show in the snapin. It is then a simple matter to see what servers have open sessions.
  • Rob C.
    Rob C. over 11 years
    We literally have hundreds of servers. :^)
  • Brent Pabst
    Brent Pabst over 11 years
    Why aren't you using UNC file paths for data transfers instead of through the RDP shell. That would take longer than the built in network file copy process.
  • Brent Pabst
    Brent Pabst over 11 years
    All this being said, we use a product at work called Remote Desktop Manager from Devolutions that allows you to view connections and sessions, assuming users have connected through the software tool. It does have some other benefits too though.
  • Rob C.
    Rob C. over 11 years
    When migrating data from non-domain member servers following acquisitions, I generally map a network drive (which requires domain creds) and kick off a Robocopy. Since the servers are usually not physically accessible to me and ILO or DRAC haven't been configured, I can't do it from the console, so I need to leave the session running for the duration of the migration. Fun, huh?
  • Rob C.
    Rob C. over 11 years
    This method worked rather well, once I pulled a list of servers! Thanks, Greg!