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.
Best Answer
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.