Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I have a virtualised Windows XP SP3 machine, which I am connecting to over Remote Desktop. One of my scripts needs to use the %sessionname% variable. However this returns incorrect information.

C:\>%sessionname%

constantly returns RDP-Tcp#5, instead of the value for the currently connected session (RDP-Tcp#35 or similar), as shown in Task Manager

This causes my scripts to contain incorrect information. What can I do to resolve this?

Edit Further Information: A restart appears to solve the problem for the first connection, but then subsequent connections have the numbers fall out of sync again.

share|improve this question
    
are you trying to run this as a local user or a domain user (assuming you have a domain)? –  Split71 Jan 23 '12 at 14:47
    
local user, there is no domain configured on these machines. –  Samuel Walker Jan 23 '12 at 15:24
    
Adding Further information, a restart appears to correct the problem, but only for the first reconnection. The numbers then get out of sync again. –  Samuel Walker Jan 24 '12 at 10:11
    
I've put in a workaround for the moment, but I'm still concerned as to why this variable is incorrect most of the time. –  Samuel Walker Jan 25 '12 at 14:17

4 Answers 4

I'm not sure why it doesn't update properly, but perhaps you can use the ouput of query session instead? It's a bit more detailed as well.

share|improve this answer
    
Hmm... C:\>query session Fails, query is not recognized. This happens on all our of XP VM's. I suspect there may be a deeper issue here. –  Samuel Walker Jan 23 '12 at 13:48

I am having exactly the same problem with Windows Server 2008 R2 running as a Remote Desktop. Session ID seems to be always correct but you cannot get Session ID from either %sessionid% or from with VBA Environ("SessionID"). My workaround is to use Environ("UserName") with C:>query session to extract the SessionID then use that with C:>query process "processname" to determine if the process is running in the session. I am not sure what your script is doing but perhaps this approach will help. Please note that this will only work if a user is only allowed one session at a time as is the case in our terminal server setup. If a user can have more than one session then the sessionid will not be unique based on username.

share|improve this answer

Thanks Paul for your post. I have the same issue on Win2008R2 and I changed my script to this :

for /F "usebackq tokens=1" %%f in (`query session ^| findstr /C:^^^>`) do set session="%%f"
set session=%session:~10,-1%
tasklist /FI "SESSIONNAME eq RDP-Tcp#%session%" | findstr mytool.exe >NUL
if %errorlevel% EQU 0 echo Tool found!

I look for the current session (so it works if several sessions are opened with the same user). Then I look if my tool is running in the current session.
Note the " " around %%f so that I am not annoyed by the ">" at the beginning of the line.

share|improve this answer

Does qwinsta /server:servername work? If sessions are constantly changing this may assist in determining the new sessions.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.