# VBS Login Script Map Network Drive



## newhouse1390

I am trying to convert some of our scripts over to vbs

The network drives are currently being mapped via batch scripts. All of the network drives are already mapped on most workstations. When the following script runs an error message pops up saying "device name already in use". Is there any way to make the script override any existing map and just re-map everytime executed?

If a user has an old network drive using a drive letter we want mapped to another share, I want the script to override that. This is what I have so far, can some one direct me in the right direction? Thanks.



> Set objNetwork = CreateObject("WScript.Network")
> objNetwork.MapNetworkDrive "S:", "\\nw-w2k3-dc-01\Shared"
> 
> Set objNetwork = CreateObject("WScript.Network")
> objNetwork.MapNetworkDrive "J:", "\\nw-w2k3-dc-01\APPS"


----------



## levi.rogers

*This should do it.*



> On Error Resume Next
> Set objNetwork = CreateObject("WScript.Network")
> objNetwork.MapNetworkDrive "S:", "\\nw-w2k3-dc-01\Shared"
> 
> On Error Resume Next
> Set objNetwork = CreateObject("WScript.Network")
> objNetwork.MapNetworkDrive "J:", "\\nw-w2k3-dc-01\APPS"


I apologize I did not read through everything on your post....

This will not overwrite existing maps it will just allow the error that is generated if a drive letter is already taken to be skipped. 

I would like at the following place:

http://www.microsoft.com/technet/scriptcenter/resources/qanda/hsgarch.mspx


----------



## newhouse1390

> On Error Resume Next
> Set objNetwork = CreateObject("WScript.Network")
> objNetwork.MapNetworkDrive "S:", "\\nw-w2k3-dc-01\Shared"
> 
> On Error Resume Next
> Set objNetwork = CreateObject("WScript.Network")
> objNetwork.MapNetworkDrive "J:", "\\nw-w2k3-dc-01\APPS"
> 
> On Error Resume Next
> Set objNetwork = CreateObject("WScript.Network")
> objNetwork.MapNetworkDrive "S:", "\\nw-w2k3-dc-01\Pictures"
> 
> On Error Resume Next
> Set objNetwork = CreateObject("WScript.Network")
> objNetwork.MapNetworkDrive "J:", "\\nw-w2k3-dc-01\users\%username%"


In therory the above should work but for some reason it only maps the first two drives and not the last two. Any ideas?

Thanks!


----------



## newhouse1390

*Update:*



> Set objNetwork = CreateObject("WScript.Network")
> objNetwork.MapNetworkDrive "U:" , "\\nw-w2k3-dc-01\users\%username%"


Report is access denied. Anything wrong with the syntax? Other than that I have got the other three drives to map correctly!


----------



## newhouse1390

It has to be something with the %username%. I am guessing vbs is a little different. Any oneknow how to correctly fill out that line?

Thanks again.


----------



## levi.rogers

*??*

Is that a redirected folder?


----------



## levi.rogers

*This is how I think it works...*



Code:


' MNDUserName.vbs
' VBScript to map a network drive to the UserName.
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.3 - April 24th 2005
' -----------------------------------------------------------------'
Option Explicit
Dim objNetwork
Dim strDriveLetter, strRemotePath, strUserName
strDriveLetter = "J:"
strRemotePath = "\\grand\home"

' Purpose of script to create a network object. (objNetwork)
' Then to apply the MapNetworkDrive method. Result J: drive
Set objNetwork = WScript.CreateObject("WScript.Network")
' Here is where we extract the UserName
strUserName = objNetwork.UserName
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath _
& "\" & strUserName

' Extra code just to add a message box
WScript.Echo " Launch Explorer, check: "& strDriveLetter
WScript.Quit

' End of MapNetworkDrive Example logon script.


----------



## newhouse1390

The way it is setup is:

*This is the My Documents re-direction:

User John Smith:

Home Folder \\server\users\john.smith\my documents

*I want the mapped drive to map the home folder:

\\server\users\john.smith


----------



## newhouse1390

The above script works! I just need it to not display the error message when the drive is already mapped.


----------



## levi.rogers

*Add the same thing as before*

Add:

ON ERROR RESUME NEXT

Put this after variable's are initialized and before the script for mapping drives.


----------



## newhouse1390

Thanks, scripts are working and are now active!


----------



## levi.rogers

*Your Welcome.*

Glad I could help.


----------

