Reset a hung RDP sessions
Everyone who is household have a terminal server on Windows have to deal with hung sessions.
Sessions often crash if the user just closed the terminal window.
Of course, we all configurable parameters reset sessions on time and disabling the user, but sometimes that session is still not complete and then we use the command: qwinsta — view sessions and rwinsta — reset sessions
I am absolutely sure that the script that detects and spasyot hung session, many have written but not posted because it's simple, I'll post for those who are seeking and who needs it right now:
The script gets the list of sessions qwinsta, chooses the session with the status of the Drive ("*AB*" — is also a "Disc", just in a curve encoding added just in case) and rwinsta resets the session.
In skriptit rwinsta is commented out, so that session was dropped uncomment.
In its current form the script will just show you a hung session.
Supplement for those who prefer to reset manually or selectively:
Article based on information from habrahabr.ru
Sessions often crash if the user just closed the terminal window.
Of course, we all configurable parameters reset sessions on time and disabling the user, but sometimes that session is still not complete and then we use the command: qwinsta — view sessions and rwinsta — reset sessions
I am absolutely sure that the script that detects and spasyot hung session, many have written but not posted because it's simple, I'll post for those who are seeking and who needs it right now:
The script gets the list of sessions qwinsta, chooses the session with the status of the Drive ("*AB*" — is also a "Disc", just in a curve encoding added just in case) and rwinsta resets the session.
In skriptit rwinsta is commented out, so that session was dropped uncomment.
In its current form the script will just show you a hung session.
# reset the stuck sessions, add to the schedule a script on your server
RDP_Resetfailure Function($server){
$ts = qwinsta /server:$server
$td = ($ts | where { ($_ -like "*Disc*" -or $_ -like "*Disk*" -or $_ -like "*AB*" ) -and $_ -notlike "*services*"})
$tdselect = $td # or debug entry in the log: Login Id State
$td = $td-ireplace ("[^0-9]","") # leave only id sessions
for($i=0; $i -lt $td.Count; $i++){
Write-Host Reset session RDP Failture ID: $td[$i] $tdselect[$i] #a debug view id sessions
#rwinsta $td[$i] /server:$server # reset the stuck sessions, uncomment this line
}
}
$server = "MyRDPServer"
RDP_Resetfailure($server)
Supplement for those who prefer to reset manually or selectively:
# For correct display of Russian encoding in the console
[Console]::outputEncoding = [System.Text.Encoding]::GetEncoding('cp866')
RDP_Resetfailure Function($server){
$ts = qwinsta /server:$server
$td = ($ts | where { ($_ -like "*Disc*" -or $_ -like "*Disk*" -or $_ -like "*AB*" ) -and $_ -notlike "*services*"})
$tdselect = $td # or debug entry in the log: Login Id State
$td = $td-ireplace ("[^0-9]","") # leave only id sessions
Clear-Host
Write-Host "
The list hung sessions:
"
for($i=0; $i -lt $td.Count; $i++){
Write-Host Reset session RDP Failture ID: $td[$i] $tdselect[$i] #a debug view id sessions
}
Write-Host
if($td[0]){
Write-Host "
- To reset all sessions, enter 0
- Or enter the ID sbrasyvanie session
====================================
"
$r = Read-Host -Prompt "Your choice"
Write-Host =============================
# ==== Reset all sessions! =====
if($r -eq 0 ){
Write-Host
Write-Host ==== Reset all sessions! =====
for($i=0; $i -lt $td.Count; $i++){
Write-Host Reset session RDP Failture ID: $td[$i] $tdselect[$i] #a debug view id sessions
#rwinsta $td[$i] /server:$server # reset the stuck sessions, uncomment this line
Write-Host
}
}
# Reset session ID
if($r -gt 0 ){
$tdu = $tdselect | where { $_ -match ' '+$r }
Write-Host Reset session RDP Failture ID: $r $tdu
#rwinsta $r /server:$server # resetting a hung session, uncomment this line
}
}
}
$server = "MyRDPServer"
RDP_Resetfailure($server)
Комментарии
Отправить комментарий