{"id":1042,"date":"2014-05-23T09:55:30","date_gmt":"2014-05-23T09:55:30","guid":{"rendered":"http:\/\/www.nooblet.org\/blog\/?p=1042"},"modified":"2014-05-23T11:15:46","modified_gmt":"2014-05-23T11:15:46","slug":"logoff-disconnected-users-via-batch-file","status":"publish","type":"post","link":"https:\/\/www.nooblet.org\/blog\/2014\/logoff-disconnected-users-via-batch-file\/","title":{"rendered":"Logoff Disconnected Users From Remote Desktop via batch-file"},"content":{"rendered":"<p>I would like to log-off <i>some<\/i> users after they have disconnected. Windows has this feature built-in, you can find it at the <a href=\"\/blog\/wp-content\/uploads\/2014\/05\/20140523-103112_000235.png\" target=_blank rel=\"lightbox[1042]\">user-properties &#8220;Sessions&#8221; tab<\/a>.<\/p>\n<p>However I have found this feature to be unreliable. Whether it is set to log-off disconnected users at 1 minute or 5 minutes, during isolated tests it works fine, but when in production it has failed multiple times in the last few months with disconnected sessions lingering for hours before noticed.<\/p>\n<p>I found <a href=\"http:\/\/www.sumoc.com\/blog\/index.cfm\/2008\/3\/10\/How-to-autologoff-remote-Desktop-Users#comment-8214405C-5056-03DC-A580913F241B259F\">this command-line code<\/a> to log-off all disconnected users. It seemed to fit the purpose. It uses &#8220;<strong>quser<\/strong>&#8221; to get the list of logged on users, writes them to a file, finds the ones that are disconnected and uses &#8220;<strong>rwinsta<\/strong>&#8221; to log them off. Pretty neat.<\/p>\n<p>I felt I could improve on it though. I saw it didn&#8217;t really need to write to a temporary file, and just use internal variables. I also wanted it to connect to multiple servers and be able to list specific users to be logged off.<\/p>\n<p>This can then be added to an elevated scheduled task that runs every X minutes to check for disconnected users.<\/p>\n<p>This is working for Windows Server 2008 R2 and Windows Server 2012.<\/p>\n<p><strong>Disconnect listed users:<\/strong><br \/>\n<em>(adjust the list of servers and users)<\/em><\/p>\n<pre class=\"lang:batch decode:true \" >@ECHO off\r\nrem Logs off disconnected users listed in \"%users%\"\r\nSETLOCAL\r\nSET \"users=joe jane geoff\"\r\nSET \"servers=rds1 rds2 rds3\"\r\nFOR %%s IN (%servers%) DO (\r\n\tFOR \/f \"tokens=1,2\" %%a IN ('\"Quser \/server:%%s | findstr Disc\"') DO (\r\n\t\tFOR %%n IN (%users%) DO (\r\n\t\t\tIF \/I %%a==%%n rwinsta \/server:%%s %%b\r\n\t\t)\r\n\t)\r\n)<\/pre>\n<p><strong>Disconnect all users:<\/strong><br \/>\n<em>(adjust the list of servers)<\/em><\/p>\n<pre class=\"lang:batch decode:true \" >@ECHO off\r\nrem Logs off all disconnected users\r\nSETLOCAL\r\nSET \"servers=rds1 rds2 rds3\"\r\nFOR %%s IN (%servers%) DO (\r\n\tFOR \/f \"tokens=2\" %%a IN ('\"Quser \/server:%%s | findstr Disc\"') DO (\r\n\t\trwinsta \/server:%%s %%a\r\n\t)\r\n)<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I would like to log-off some users after they have disconnected. Windows has this feature built-in, you can find it at the user-properties &#8220;Sessions&#8221; tab. However I have found this feature to be unreliable. Whether it is set to log-off disconnected users at 1 minute or 5 minutes, during isolated tests it works fine, but [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":641,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[216],"class_list":["post-1042","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-windows","tag-windows-disconnected-logoff"],"_links":{"self":[{"href":"https:\/\/www.nooblet.org\/blog\/wp-json\/wp\/v2\/posts\/1042","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nooblet.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nooblet.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nooblet.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nooblet.org\/blog\/wp-json\/wp\/v2\/comments?post=1042"}],"version-history":[{"count":14,"href":"https:\/\/www.nooblet.org\/blog\/wp-json\/wp\/v2\/posts\/1042\/revisions"}],"predecessor-version":[{"id":1057,"href":"https:\/\/www.nooblet.org\/blog\/wp-json\/wp\/v2\/posts\/1042\/revisions\/1057"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.nooblet.org\/blog\/wp-json\/wp\/v2\/media\/641"}],"wp:attachment":[{"href":"https:\/\/www.nooblet.org\/blog\/wp-json\/wp\/v2\/media?parent=1042"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nooblet.org\/blog\/wp-json\/wp\/v2\/categories?post=1042"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nooblet.org\/blog\/wp-json\/wp\/v2\/tags?post=1042"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}