Script for users end remote session

2

I have a couple of DB servers that each run a RemoteApp that users launch to access said DB. The problem is that the application itself does not always exit gracefully when the user ends it, which leaves a remote session open and the user is unable to access said app again.

What I am trying to create a script that would terminate any current existing connection to each specific server (i.e. Script A would terminate for RemoteApp A, Script B would terminate for RemoteApp B).

Not sure if it adds an extra complication but the users are all working in a Remote Desktop environment (i.e. user connects to RDS1 or RDS2 and then launch RemoteApps to DB1, DB2 etc. from there).

All servers are running Windows Server 2012 R2.

Mickycampbell

Posted 2016-08-25T05:47:18.813

Reputation: 21

Not sure if this meets your requirements, but you could also configure the remote server to automatically end the user's sessions after they've been disconnected for a set amount of time. No script required. – I say Reinstate Monica – 2016-08-27T19:40:57.677

I actually have that currently setup, however client has conflicting needs for a way to terminate quickly and to have the session stay open for a significant period of time (idle time of up to half an hour). The joys of IT – Mickycampbell – 2016-08-30T03:33:27.130

Micky - Any update on this and whether or not the answer I provided helped with the task? – Pimp Juice IT – 2017-04-10T20:37:44.050

Answers

2

You could use Taskkill commands to kill a specific process on a specific server running as a specific username (see below).

Kill a process by targeting it based on...

  1. the remote server name which it's running
  2. the username running the process
  3. the process name on the remote server

Example Command

Be sure to set the appropriate values in the <RemoteServerName>, <Username>, and <appname.exe> parts of the taskkill command below accordingly for your environment and needs.

TASKKILL /S <RemoteServerName> /F /FI "USERNAME eq <Username>" /IM <appname.exe>

Further Resources

  • Taskkill

  • Taskkill /?

  • /S    system           Specifies the remote system to connect to.
    
    /FI   filter           Applies a filter to select a set of tasks.
                           Allows "*" to be used. ex. imagename eq acme*
    
    /F                     Specifies to forcefully terminate the process(es).
    
    /IM   imagename        Specifies the image name of the process
                           to be terminated. Wildcard '*' can be used
                           to specify all tasks or image names
    
    Filters:
    
          USERNAME      eq, ne              User name in [domain\]user
                                            format
    

Pimp Juice IT

Posted 2016-08-25T05:47:18.813

Reputation: 29 425

Interesting, I'll have a play around with this. Thank you – Mickycampbell – 2016-08-30T03:34:56.070

@Mickycampbell No problem, just be sure to let me know if this works for you, feel free to upvote my answer if you'd like, and also feel free to accept my answer if you find it to be the solution you're looking for satisfying your needs.

– Pimp Juice IT – 2016-08-30T05:33:08.437