Keep disconnected RDP session as active

remote desktopwindows-server-2012

Is there any possibility to keep an RDP session as "active" when it was actually disconnected? We are running windows-server-2012 on a virtual machine.

Background:
we are automatically executing GUI tests on that machine.

  • If someone is logged in via RDP (and does nothing), the tests succeed
  • If the RDP session is disconnected, the tests fail at a point where keyboard strokes are simulated

So I guess it has something to do with the session being active or disconnected.

Best Answer

:::::::::::::::::::::::::::::::::::::::::
:: Automatically check & get admin rights
:::::::::::::::::::::::::::::::::::::::::
@echo off
CLS 
ECHO.
ECHO =============================
ECHO Running Admin shell
ECHO =============================

:checkPrivileges 
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges ) 

:getPrivileges 
if '%1'=='ELEV' (shift & goto gotPrivileges)  
ECHO. 
ECHO **************************************
ECHO Invoking UAC for Privilege Escalation 
ECHO **************************************

setlocal DisableDelayedExpansion
set "batchPath=%~0"
setlocal EnableDelayedExpansion
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs" 
ECHO UAC.ShellExecute "!batchPath!", "ELEV", "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs" 
"%temp%\OEgetPrivileges.vbs" 
exit /B 

:gotPrivileges 
::::::::::::::::::::::::::::
:START
::::::::::::::::::::::::::::
setlocal & pushd .
::::::::::::::::::::::::::::
::Get sessionID for current user, switch context to console.
::::::::::::::::::::::::::::
FOR /F "skip=1 tokens=3 usebackq" %%X in (`query session %USERNAME%`) DO tscon %%X /dest:console


START CALC.EXE

GOTO :EOF

Just save this as a .bat on the remote system and replace the "START CALC.EXE" line with the executable you'd actually like to run. When you're ready to run your test, RDP into the system and run the batch file.

What this script does is first gets admin rights, then switches your current user session from RDP to console. You'll get disconnected from the RDP session, but as long as there isn't a screen saver or screen lock, your application should be able to run to completion.