We help IT Professionals succeed at work.

vb script to sustitute drive mapping

matt_B_2008
matt_B_2008 asked
on
Hi all,

I want to try and move away from a logon BAT file i have which maps the X: back to the TEMP directory.

subst x: %temp%

Does anyone know whether there is a vb script i can use to achieve this?
Comment
Watch Question

Mike ThomasConsultant
Top Expert 2010

Commented:

Commented:
I use this VBscript through save it in a file called   *.vbs

The code checks is a drive is mapped and then if not, maps something to it.
Works great for me, also can apply through GPO when saved in a network share with propper access.
Dim WshNetwork
Set WshNetwork = WScript.CreateObject("WScript.Network")


Dim fso
   Set fso = CreateObject("Scripting.FileSystemObject")


If fso.DriveExists("G:") Then
   Else
      WshNetwork.MapNetworkDrive "G:", "\\Server\data"
   End If

If fso.DriveExists("O:") Then
   Else
      WshNetwork.MapNetworkDrive "O:", "\\Server\user"
   End If

Open in new window

Commented:
in your case there is no replacment to subst that use to map local folder to drive

if you want to map a share on a remote computer you can use this example:

Set WshNetwork = WScript.CreateObject("WScript.Network")
WshNetwork.MapNetworkDrive "e:", "\\Server\share"

Author

Commented:
Yes im using that to map a drive at the moment. If have a vbscript that maps anumber of drives to various servers shares as per your example.

What i wanted specifically is the substitute method, or how do i map a drive to teh %temp% location

Commented:
try this in vbs

Set objShell = CreateObject("WScript.Shell")
strDrive = "X:"
strPath = objShell.ExpandEnvironmentStrings("%temp%")
objShell.Run "cmd /c SUBST " & strDrive & " " & strPath, 1, True

Author

Commented:
Unfortunately that didnt work.

Author

Commented:
This is what i have at the moment


wshNetwork.RemoveNetworkDrive "S:"
WshNetwork.MapNetworkDrive "S:" , "\\server1\Shared Data"
wshNetwork.RemoveNetworkDrive "t:"
WshNetwork.MapNetworkDrive "t:" , "\\server2\SData"
wshNetwork.RemoveNetworkDrive "v:"
WshNetwork.MapNetworkDrive "v:" , "\\server3\code"
wshNetwork.RemoveNetworkDrive "w:"
WshNetwork.MapNetworkDrive "w:" , "\\server4\spools"

Commented:
opps forgot to say

edit line 4 to subst path

like this for example

objShell.Run "cmd /c \\server\storage\SUBST " & strDrive & " " & strPath, 1, True

Set objShell = CreateObject("WScript.Shell")
strDrive = "X:"
strPath = objShell.ExpandEnvironmentStrings("%temp%")
objShell.Run "cmd /c SUBST " & strDrive & " " & strPath, 1, True

Open in new window

Author

Commented:
Thanks

but im not sure what i would put in   here

\\server\storage\SUBST

Im not mapping to any particular drive share im tryng to map to the TEMP directory of the server they are logging into....does that make sense?

Commented:
Try using %temp%

Author

Commented:
Sorry where abouts exactly?

Commented:
what wrong with me...

the first version just fine
i run it on my computer and worked great

you don't see drive x: apper after you run the code ?
Set objShell = CreateObject("WScript.Shell")
strDrive = "X:"
strPath = objShell.ExpandEnvironmentStrings("%temp%")
objShell.Run "cmd /c SUBST " & strDrive & " " & strPath, 1, True

Open in new window

Author

Commented:
Nope still didnt work.....
Hi Matt

Try this, I found on this site. This is a vbs script. and the file extension should be .vbs
Set objShell = CreateObject("WScript.Shell")
strDrive = "X:"
strPath = "C:\Temp"
objShell.Run "cmd /c SUBST " & strDrive & " " & strPath, 1, True

Open in new window

Author

Commented:
Got this working int he end.....


'Mapping a temp drive  
Set WinShell = WScript.CreateObject("WScript.Shell")  
WinShell.Run "c:\windows\system32\subst X: %temp%", 2, True

Commented:
hey matt what error do you see when you run my script ?

Author

Commented:
i didnt have the %systemroot% in the path varibles so it coudl find the command.

Working now.
Commented:
read my minds,

you can run this in cmd;

set path=%path%;c:\windows\system32

did my script did the job after you run it ?

Commented:
thanks for the points