📜 ⬆️ ⬇️

Reset suspended RDP sessions

Everyone who has terminal servers on Windows has to deal with hung sessions.
Sessions often hang, if the user simply closed the terminal window.
Naturally, we all configure the settings for resetting sessions by time and user disconnection, but it happens that the sessions are still not completed and then we use the commands: qwinsta - view sessions and rwinsta - reset sessions
I am absolutely sure that the script engine, which itself tracks and resends hung sessions, was written by many, but wasn’t uploaded to the network because it’s easy, I’ll post it for those who are looking for and who need it right now:

The script receives a list of qwinsta sessions, selects sessions with the status Disk ("* EbЄ *" is also a "Disk", just in the encoding curve, added just in case) and rwinsta resets the session.
In the script, rwinsta is commented out, so that the sessions are reset, uncomment it.
In its current form, the script will just show you hung sessions.

 #   ,        Function RDP_Resetfailure($server){ $ts = qwinsta /server:$server $td = ($ts | where { ($_ -like "*Disc*" -or $_ -like "**" -or $_ -like "*Є*" ) -and $_ -notlike "*services*"}) $tdselect = $td #      : Login Id State $td = $td -ireplace ("[^0-9]","") #   id  for($i=0; $i -lt $td.Count; $i++){ Write-Host Reset RDP Failture session ID: $td[$i] $tdselect[$i] #  id  #rwinsta $td[$i] /server:$server #   ,    } } $server = "MyRDPServer" RDP_Resetfailure($server) 
# , Function RDP_Resetfailure($server){ $ts = qwinsta /server:$server $td = ($ts | where { ($_ -like "*Disc*" -or $_ -like "**" -or $_ -like "*Є*" ) -and $_ -notlike "*services*"}) $tdselect = $td # : Login Id State $td = $td -ireplace ("[^0-9]","") # id for($i=0; $i -lt $td.Count; $i++){ Write-Host Reset RDP Failture session ID: $td[$i] $tdselect[$i] # id #rwinsta $td[$i] /server:$server # , } } $server = "MyRDPServer" RDP_Resetfailure($server)

Additive for those who prefer to drop manually or selectively:
 #        [Console]::outputEncoding = [System.Text.Encoding]::GetEncoding('cp866') Function RDP_Resetfailure($server){ $ts = qwinsta /server:$server $td = ($ts | where { ($_ -like "*Disc*" -or $_ -like "**" -or $_ -like "*Є*" ) -and $_ -notlike "*services*"}) $tdselect = $td #      : Login Id State $td = $td -ireplace ("[^0-9]","") #   id  Clear-Host Write-Host "   : " for($i=0; $i -lt $td.Count; $i++){ Write-Host Reset RDP Failture session ID: $td[$i] $tdselect[$i] #  id  } Write-Host if($td[0]){ Write-Host " -    c  0 -   ID   ==================================== " $r = Read-Host -Prompt " " Write-Host ============================= # ====   ! ===== if($r -eq 0 ){ Write-Host Write-Host ====   ! ===== for($i=0; $i -lt $td.Count; $i++){ Write-Host Reset RDP Failture session ID: $td[$i] $tdselect[$i] #  id  #rwinsta $td[$i] /server:$server #   ,    Write-Host } } #   ID if($r -gt 0 ){ $tdu = $tdselect | where { $_ -match ' '+$r } Write-Host Reset RDP Failture session ID: $r $tdu #rwinsta $r /server:$server #   ,    } } } $server = "MyRDPServer" RDP_Resetfailure($server) 
# [Console]::outputEncoding = [System.Text.Encoding]::GetEncoding('cp866') Function RDP_Resetfailure($server){ $ts = qwinsta /server:$server $td = ($ts | where { ($_ -like "*Disc*" -or $_ -like "**" -or $_ -like "*Є*" ) -and $_ -notlike "*services*"}) $tdselect = $td # : Login Id State $td = $td -ireplace ("[^0-9]","") # id Clear-Host Write-Host " : " for($i=0; $i -lt $td.Count; $i++){ Write-Host Reset RDP Failture session ID: $td[$i] $tdselect[$i] # id } Write-Host if($td[0]){ Write-Host " - c 0 - ID ==================================== " $r = Read-Host -Prompt " " Write-Host ============================= # ==== ! ===== if($r -eq 0 ){ Write-Host Write-Host ==== ! ===== for($i=0; $i -lt $td.Count; $i++){ Write-Host Reset RDP Failture session ID: $td[$i] $tdselect[$i] # id #rwinsta $td[$i] /server:$server # , Write-Host } } # ID if($r -gt 0 ){ $tdu = $tdselect | where { $_ -match ' '+$r } Write-Host Reset RDP Failture session ID: $r $tdu #rwinsta $r /server:$server # , } } } $server = "MyRDPServer" RDP_Resetfailure($server)

')

Source: https://habr.com/ru/post/276935/


All Articles