Disconnect-LoggedOnUser - Disconnect a RDP session remotely

My Get-LoggedOnUser script is available here: Get-LoggedOnUserThis function provides the functionality to disconnect a RDP session remotely by providing the ComputerName and the SessionId. It is build to work either as a stand alone function to disconnect RDP sessions remotely or

 
 
 
 
 
5 Star
(1)
5,135 times
Add to favorites
6/3/2015
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question


  • Suggestion
    2 Posts | Last post October 29, 2015
    • I run most of my scripts locally from an account without Domain Administrator permissions so your original script did not work for me. I added and optional credential parameter to the script and then run the following if the credential parameter is present:
          Start-Process rwinsta -ArgumentList @("$_", "/server:$Computer") -Credential $cred -NoNewWindow
    • Indeed, alternatively you could also run your PowerShell session with alternate credentials and then you can run this script or any other script with alternative credentials.
  • typho ?
    4 Posts | Last post September 23, 2015
    • This on Line 73 seems wrong
      
                          rwinsta $_ /server:$Computer
      
      replaced with:
      
                          rwinsta $Id /server:$Computer
      
      Correct?
      Working after change if running from Get-loggedOnUser.ps1 but not if running
       Disconnect-LoggedOnUser -ComputerName server01 -Id 5
      
      Excelent scripts combined :)
      
      Is it possible to have Get-LoggedOnUser.ps1 to send the result in html by mail to the users affected ? and sending warning mail if logged in for more than 24 hours? and next step lets say 48 hours sending You are forced out? and kick affected users out ? :)
      
      Already sending mail with the results but to a admin Group today...
      
      Best regards: Magnus Bringeland
       
    • Hello MagnusBr,
      
      The reason I am using $_ instead of $Id there is because I am piping $Id into ForEach-Object, this allows for multiple IDs to be specified for example:
      
      Disconnect-LoggedOnUser -ComputerName server01 -Id 5,3,7
      
      I have run the command you listed by for me it works as expected without changing line 73, can you check again if you are running into issues. If you do run into the same issue please specify as much information as possible so I can attempt to reproduce the error.
      
      As for listing session times, I currently do not retrieve that information but I could include the logged on time in the results. I do think that setting group policies to manage automatic session termination would be a better method of achieving the automatic disconnects though. Let me know what your thoughts are.
    • OK I will check later (Friday afternoon here now;)
      
      Listing State and IDLE time is included and will do fine. For different administrators on several hundreds of different systems a GPO policy is not so good idea ?. If possible send these administrators (individually) a warning by mail and later on throw them out or/and  $exclude specific user who is doing something important on a server?
      I handle only admins on servers no client users at all.
      I have mail setup(HTML) but to a Group of all admins, also convert username to mail I have fixed. Filter only servers in $servers etc..
      Left to do is just send mail to  idletime xxHours  user@mail, and if idletime yyHours a warning that next time etc You will be logged out ?????
      
      That is my thoughts correct me if i´m wrong here ;) ?
      
      When the scripting is final it will run scheduled and nearly no manual input except excluded users etc..
      
    • Sorry I just went to my scripts and just realized I never responded back to you.
      
      But to answer your question, yes that sounds like a nice application for this script. Let me know if you complete the code for this project I would like to see how you tied it all together and if you are open to it I could give feedback on your code.
  • Thanks
    1 Posts | Last post June 04, 2015
    • Thanks for this looking forward to testing it out