Guy's Scripting Ezine 25 - MapNetworkDrive
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.
Calculating IP Address
ranges is a black art, which many network managers solve by creating custom
Excel spreadsheets. IPAT cracks this problem of allocating IP addresses
in networks in two ways:
For Mr Organized there is a nifty subnet
calculator, you enter the network address and the subnet mask, then IPAT
works out the usable addresses and their ranges.
For Mr Lazy IPAT
discovers and then displays the IP addresses of existing computers.
Download the Free IP Address Tracker
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.
Guy Recommends: A Free Trial of the Network Performance Monitor
(NPM)
Solarwinds'
Orion performance monitor
will help you discover what's happening on your network. This
utility will also guide you through troubleshooting; the dashboard will
indicate whether the root cause is a broken link, faulty equipment or
resource overload.
Perhaps the NPM's best feature is the way it suggests solutions to network
problems. Its
second best feature is the ability to monitor the health of individual VMWare
virtual machines. If you are interested in troubleshooting, and creating
network maps, then I recommend that you take advantage of Solarwinds' offer.
Download a free trial of
the Network Performance Monitor.
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\.
See more about logon scripts
• Logon Arguments • Logon Map Network Drive • Logon Scripts • PowerShell
Com Shell
• Ezine 3 Map Network Drive • Ezine
4 Logon • Tool Kit • PowerShell
Logon Scripts
• Ezine 25 map • Ezine 30 Map Simple
• Ezine 31 Mapnetworkdrive • Ezines • Free
CSV Importer
|