Is your server running slowly? Check with SolarWinds ipMonitor
Get a free evaluation copy of ipMonitor
Contents for Guy's Scripting Ezine 25 - MapNetworkDrive
Every now and then you have to go back to basics. This week I would
like to revisit mapping network drives. Another reason for featuring
home drives is that my mail bag is full of people having problems with
MapNetworkDrive. Their specific problem is trying to be too clever and
map to a sub folder.
Our objective is to map the N: to a server called 'alan' with a share
called 'home'.
Instructions
- Pre-requisites. On Line 12 change the server name from 'alan' to
your server name (It does not have to be a domain controller).
- Copy and paste the script below into notepad.
- Save the file with .vbs extension e.g. MapNetworkDrive.vbs
- Double click and observe the message box
- Check your explorer for a new N:
' MapNetworkDrive.vbs
' VBScript to map a network drive.
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.3 - April 4th 2004
' -----------------------------------------------------------------'
Option Explicit
Dim objNetwork
Dim strDriveLetter, strRemotePath
Set objNetwork = WScript.CreateObject("WScript.Network")
strDriveLetter = "N:"
strRemotePath = "\\alan\home"
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath
WScript.Echo "Mapped drive "& strDriveLetter & " to " & strRemotePath
WScript.Quit
' End of script.
Learning Points
Note 1: To help me see the UNC path on the server, I like to introduce
the variables strDriveLetter and strRemotePath. The other benefit is
that I can use these variables in the WScript.Echo message. Many of
the problems arise through faulty understanding of the UNC path.
Note 2: objNetwork is the object, and will be our vehicle to create the
drive mapping.
Note 3: MapNetworkDrive is the method to join the UNC path to the
objNetwork.
Server name = Alan. Folder you wish to map c:\home.
Preparation, share out c:\home with a name of 'home'. Result UNC path
\\alan\home.
So far, so good, but problems arise when you wish users to connect to a
subfolder. For example, you cannot connect directly to
\\alan\home\guyt, because UNC only permits two components, not three.
The heart of the problem is that only one server name and one share name is
allowed. Fortunately, there are two work-arounds. Either create
a share called guyt and then map to \\alan\guyt, or cleverly, concatenate
guyt to the original \\alan\home share, for example: \\alan\home\ & guyt.
This second technique is particularly useful with user's home directory
because you can derive the username as a property of who ever has just
logged on. As ever, pay attention to the syntax, here we need to join
with an ampersand &, not a comma.
Instructions
- Important: Make sure you have a share called 'home'.
Underneath the users share, create a sub-folder for the account you are
testing. Best would be to create accounts with %Username% in the
Profile, Home folder dialog box.
- Pre-requisites. Change the server name from 'alan' to your server
name (does not have to be a domain controller).
- Copy and paste the script below into notepad.
- Save the file with .vbs extension e.g. MapNetworkDrive2.vbs
- Double click and observe the message box
- Check your explorer for a new Q:
' MapNetworkDrive2.vbs
' VBScript to map a network drive.
' Author Guy Thomas http://computerperformance.co.uk/
' Version 2.8 - April 4th 2004
' -----------------------------------------------------------------'
Option Explicit
Dim objNetwork
Dim strDriveLetter, strRemotePath, strUsername
Set objNetwork = CreateObject("WScript.Network")
strUserName =objNetwork.UserName
strDriveLetter = "Q:"
strRemotePath = "\\alan\home\" & strUserName
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath
WScript.Echo "Mapped drive "& strDriveLetter & " to " & strRemotePath
WScript.Quit
' End of script.
Learning Points
Note 1: On line 9, the 'WScript' is not strictly necessary before
CreateObject, so I have removed it.
Note 2: strUserName is derived from the objNetwork.UserName
Note 3: A common mistake is to omit the \ after home\. \\alan\home
would be incorrect.
1) Problem: Code 800A01C2. Check Line number, and what the Error is
trying to tell you?
(Beware Also 'The local drive name is already in use) 80070055
See more 800 Code Error message here
' MapNetworkDrive.vbs
' VBScript to map a network drive.
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.1 - April 4th 2004
' -----------------------------------------------------------------'
Option Explicit
Dim objNetwork, objAllDrives
Dim strDriveLetter, strRemotePath
Set objNetwork = WScript.CreateObject("WScript.Network")
strDriveLetter = "N:" 'must be capitalized
strRemotePath = "\\alan\home"
objNetwork.MapNetworkDrive strDriveLetter & strRemotePath
WScript.Echo "Mapped drive "& strDriveLetter & " to " & strRemotePath
WScript.Quit
' End of script.
Answer:
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath
There should be a comma, not an ampersand &.
Out Take 2
2) Problem: Code 800704B3. No network provide accepted the given
network path. Classic error for MapNetWorkDrive.
' MapNetworkDrive2.vbs
' VBScript to map a network drive.
' Author Guy Thomas http://computerperformance.co.uk/
' Version 2.4 - April 4th 2004
' -----------------------------------------------------------------'
Option Explicit
Dim objNetwork, objAllDrives
Dim strDriveLetter, strRemotePath, strUsername
Set objNetwork = CreateObject("WScript.Network")
strUserName =objNetwork.UserName
strDriveLetter = "Q:"
strRemotePath = "\\alan\home" & strUserName
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath
WScript.Echo "Mapped drive "& strDriveLetter & " to " & strRemotePath
WScript.Quit
' End of script.
Answer:
strRemotePath = "\\alan\home\" & strUserName
Missing \ after home\.
Their topics and material are ideal for getting you started with VBScript. The
videos are easy to follow and you can control the pace. Try their free demo material and then see if you want to buy the full package.
See more about VB Script Training CD.
|