WS2008 subst in Logon script does not “stick”

terminal-servervbscriptwindows-server-2008

I have a terminal server environment exclusively with Windows Server 2008.
My problem is that I need to "map" a drive letter to each users Temp folder. This is due to a legacy app that requries a separate Temp folder for each user but which does not understand %temp%.

So, just add "subst t: %temp%" to the logon script, right?
The problem is that, even though the command runs, the subst doesn't "stick" and the user doesn't get a T: drive.
Here is what I have tried;

The simplest version:

'Mapping a temp drive  
Set WinShell = WScript.CreateObject("WScript.Shell")  
WinShell.Run "subst T: %temp%", 2, True  

That didn't work, so tried this for more debug information:

'Mapping a temp drive
Set WinShell = WScript.CreateObject("WScript.Shell")
Set procEnv = WinShell.Environment("Process")
wscript.echo(procEnv("TEMP"))
tempDir = procEnv("TEMP")
WinShell.Run "subst T: " & tempDir, 3, True

This shows me the correct temp path when the user logs in – but still no T: Drive. Decided to resort to brute force and put this in my login script:

'Mapping a temp drive
Set WinShell = WScript.CreateObject("WScript.Shell")
WinShell.Run "\\domain\sysvol\esl.hosted\scripts\tempdir.cmd", 3, True

where \domain\sysvol\esl.hosted\scripts\tempdir.cmd has this content:

echo on
subst t: %temp%
pause

When I log in with the above then the command window opens up and I can see the subst command being executed correctly, with the correct path. But still no T: drive.

I have tried running all of the above scripts outside of a login script and they always work perfectly – this problem only occurs when doing it from inside a login script.

I found a passing reference on an MSFN forum about a similar problem when the user is already logged on to another machine – but I have this problem even without being logged on to another machine.

Any suggestion on how to overcome this will be much appreciated.

Best Answer

If you create a share for the drive where the user profiles are located (typically C:\Users). Assuming the share is PROFILE$, you can then use a script such as:

@echo off
echo.                 TEMP Drive Mapping


:TEMPDRIVE
  echo.                   Mapping Temp Drive...
  if exist T:\  echo Y | subst T: /d 
  if exist T:\  echo Y | net use T: /D
  if exist T:\ goto :TEMPDRIVE_ERROR

  if not exist %temp% MD %temp%
  NET USE T: \\localhost\profiles$\%temp:~9% /P:N  > nul: 2>&1
  if not exist T:\ goto :TEMPDRIVE_ERROR

  goto :TEMPDRIVE_COMPLETE 
:TEMPDRIVE_ERROR
  echo.                  ERROR: Unable to MAP Temp Drive!
:TEMPDRIVE_COMPLETE

:END
:EXIT

GOTO :EOF

Usually %TEMP% is prefixed with C:\Users\ on Windows Server 2008, which is why %temp:~9% is used, to remove that prefix. If using Windows Server 2003, it would be %temp:~12%