Keeping an active desktop session


Credit: this post heavily uses/reuses the content from Arnon Axelrod’s post. For some reasons, I couldn’t leave him a comment because of reCAPTCHA issue. So I decided to create this post and cover part he didn’t. I strongly recommend you to visit his post for full story.

Some test automation tools require an active desktop session to operate normally. I have been struggling with other approaches: autologin, relogin, unlock locked screen, etc. All of them led to same dead end. I already have a post for “How To Unlock Desktop Screen After Remote Access Disconnected” however it requires running cmd command as administrator which is not so convenience.

What locks the desktop?
1. System locked (Windows Key + L) – So far, I don’t see any better solution than using VNC.
2. Remote Desktop session disconnected – See solution below

In my opinion, Arnon Axelrod’s approach or solution is so elegent for this very common test automation problem. What we need to do is keeping an active desktop session by getting cmd command triggered automatically upon RDC disconnected.

In brief, the solution is composed of few pieces
1. A Task Scheduler’s task that is triggered upon disconnecting form Remote Desktop
2. The query session command
3. The TSCon command
4. A windows script that combines the 2 commands

for /f "skip=1 tokens=2" %%s in ('query user %USERNAME%') do (tscon.exe %%s /dest:console)

About Author

Hien Nguyen is an experienced Software QA Engineer. A tester by day and a blogger by night, Hien has a great passion for software testing, especially complex test automation problems. When not doing all those, he enjoys reading, jogging and trying new things.