We've all encountered those stubborn terminal sessions that refuse to die - processes that won't terminate, sessions that won't log off, and users stuck in limbo. When this happens on a production Windows Server 2008 Terminal Server, the stakes get higher.
Before diving into advanced solutions, let's acknowledge what typically doesn't work when sessions are truly frozen:
- Task Manager process termination
- Terminal Services Manager session reset
- Resource Monitor kill operations
- Remote Desktop Services administrative tools
When GUI tools fail, PowerShell often succeeds. Here's a comprehensive approach:
# First, identify the stuck session
$sessions = qwinsta /server:localhost | Where-Object { $_ -match 'Disc' } | ForEach-Object {
$parts = $_ -split '\s+'
[PSCustomObject]@{
SessionName = $parts[0]
Username = $parts[1]
ID = $parts[2]
State = $parts[3]
}
}
# Forcibly reset the session
foreach ($session in $sessions) {
if ($session.State -eq 'Disc') {
rwinsta $session.ID /server:localhost
}
}
# Nuclear option - terminate all processes
$processes = Get-Process -IncludeUserName | Where-Object {
$_.UserName -match $problemUser
}
$processes | Stop-Process -Force
When even PowerShell cmdlets fail, dropping down to WMI can sometimes work:
$sessionID = 3 # Replace with actual stuck session ID
$terminateMethod = Invoke-WmiMethod -Path "Win32_TSLogonSession.SessionId='$sessionID'" -Name Terminate
For the most persistent cases, we need to go deeper. This requires the Windows Debugger Tools:
# First, find the session's winlogon.exe process
$winlogon = Get-Process winlogon | Where-Object {
$_.SessionId -eq $stuckSessionId
}
# Attach the debugger (requires admin)
ntsd -pn winlogon.exe -c "q"
Once you've resolved the immediate crisis, consider these preventive steps:
- Implement session time limits via GPO
- Set up automated monitoring for orphaned sessions
- Consider upgrading from Server 2008 (EOL risks)
- Implement regular maintenance reboots during low-usage periods
Sometimes, despite our best efforts, a reboot remains the only solution. When this happens:
- Schedule it during the least disruptive time
- Use cluster failover if available
- Document the incident for future reference
We've all faced that nightmare scenario where a user session gets completely locked up on a production Terminal Server. The usual tools - Task Manager, Terminal Services Manager, even Resource Monitor - fail to kill the session. The user can't log back in, and you can't afford to reboot the production server.
When a session becomes truly stuck, the normal RDP/Terminal Services infrastructure stops responding. The session exists in a zombie state where:
- Processes become unkillable (STATUS_DELETE_PENDING)
- Session handles get corrupted
- The winlogon.exe process hangs
Before resorting to a reboot, try these PowerShell commands with elevated privileges:
# First attempt - query sessions
query session /server:localhost
# Try resetting the session (replace 1 with actual session ID)
reset session 1 /server:localhost
# If that fails, go for process termination
$sessionId = 1 # Replace with target session ID
$processes = Get-Process -IncludeUserName | Where-Object {$_.SessionId -eq $sessionId}
$processes | ForEach-Object { Stop-Process -Id $_.Id -Force }
# Last resort - use tsdiscon
tsdiscon $sessionId /server:localhost /v
If PowerShell commands don't work, you can try directly manipulating the session through the registry:
# Navigate to the sessions key
Set-Location "HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server"
# Find the stuck session (look for matching usernames)
Get-ChildItem .\WinStations | Get-ItemProperty | Where-Object {$_.Username -match "problemuser"}
# Manually delete the session key (WARNING: be very careful)
Remove-Item -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\Session1" -Force
To minimize occurrences:
- Set session timeouts in Group Policy (Computer Configuration > Policies > Administrative Templates > Windows Components > Remote Desktop Services)
- Implement resource limits per user session
- Keep Terminal Services updated with latest patches
While we eventually had to reboot in our case, these techniques have worked in 90% of similar situations. The registry method should be considered a last resort due to potential system instability. Always create a system restore point before making registry changes.