Is your server running slowly? Check with SolarWinds ipMonitor
Get a free evaluation copy of ipMonitor
Contents for Guy's Scripting Ezine 30 - Map Network Drive.
80% of the requests to my scripting clinic are about map network drive and
printer logon scripts. Indeed 80% of those problems are caused by
mistakes or misunderstandings in the UNC path. Incidentally, the basis
of my scripting clinic is this:, Guy gives free if it's a quick query, and I
charge $20 if it takes me an hour to research your question. Should you send in a
problem, please include the script as text or zip.
This week is the first instalment of a two part series about mapping
network drives. Today I features the basics, next week covers the more advanced
techniques. As this week's theme is back to basics, let us double check
the scenario; the situation is that you want users to connect automatically to
a network server. Many users actually believe that their H: drive is on
the local disk, but you know that all the data in that H: drive is stored
safely on the server.
This first example will map
a network drive. It's is about as short as I can make a VBScript.
Instructions
- Copy and paste the script below into notepad. Alternatively, use a
script editor like VBsEdit.
- Change MY server name ALAN to the name of Your server. Check the
share name = home. Do you have a home share? If not either create
one, or amend the script.
- Save the file with .vbs extension e.g. MapK.vbs
- Double click and observe drive letters in explorer. What you are
looking for is the K:\
- If you get errors, check that you have amended the object names alan or
home. If you still get Code 800xxxxx message boxes, pay close attention
to the commas and speech marks on the second line.
Set objNetwork = CreateObject("WScript.Network")
objNetwork.MapNetworkDrive "K:", "\\ALAN\home"
Learning Points
Note 1: As with most modern scripting languages, VBScript uses the object, method, properties
construction. The name of our object variable is = objNetwork. You can use any name
you choose, in fact, many scripts writers call this object WSHNetwork.
The name of the variable name really does not matter, what counts is that you
are consistent within the script.
Note 2: There is no choice with the precise
command CreateObject("Wscript.Network"). We want to create not modify and we want a network object, not
a shell object. About the only flexibility is the CapitalizAtion.
VBScript is case insensitive.
Note 3: Talking of flexibility, do watch the punctuation, tell the truth unless you
copy someone else's example, you are almost certain to make a syntax error.
Look at those brackets, that dot, and those speech marks, plenty of
room for an error unless you are very careful or copy a previous example.
Note 4: What is the method here? .MapNetworkDrive is the answer. I love
methods, they are the 'doers' always be on the lookout for methods to add to
your tool kit. RemoveNetworkDrive and EnumNetworkDrives() would be alternative verbs or
methods that in other circumstances you could apply to the objNetwork.
Note 5: K: and \\alan\home are the properties. Absolutely vital for the script
but boring for the programmer. Woops, I slipped up and forgot the
punctuation. Is it
a) "K:", "\\alan\home"
b) "K:\", "\\alan\home"
c) "K:" "\\alan\home"
Answer: a)
As you get to know me, you will appreciate that I love variables. The
purpose of this script is the same as Example 1, namely to map the home drive.
The difference is that I have introduced two new variables. I have also changed the drive letter from K to S to reduce 'The Drive is already in
use' error messages.
Set objNetwork = CreateObject("WScript.Network")
strDriveLetter = "S:"
strHomeServer = "\\alan\home"
objNetwork.MapNetworkDrive strDriveLetter, strHomeServer
Learning Points
Note 1: The introduction of more variables makes the script longer but easier to adjust.
Using this technique also makes us think about the VBscript construction and to
appreciate the part played by each element.
Note 2: strDriveLetter is a variable to hold the letter to be mapped, in this
case "S:". Note the speech marks around the value, realize that you
need a colon, but not a slash. ("S:\" would be an error). As for the name of the variable, do
change it to your own name, for example strDrive.
Note 3: strHomeServer is a second variable which tells the method where the server
and share are located.
Note 4: As you may know I run a scripting clinic, free
for short advice and a $20 charge if your problem needs research. Universal
Naming Convention is a great way of describing a server and share. Two
backslashes, server, one backslash share. Where most people go astray
is that they want to map to folder beneath the share.
\\server\share\subfolder. Whilst this can be done you must
build in an extra step using & to join on the subfolder. Employing
an
ampersand to join two variables is called concatenation.
The crucial feature of this script is the strUserName which is joined to
the home drive to form the complete path. \\ server \ home \ username.
Set objNetwork = CreateObject("WScript.Network")
strDriveLetter = "S:"
strHomeServer = "\\alan\home\"
strUserName =objNetwork.UserName
objNetwork.MapNetworkDrive strDriveLetter, strHomeServer & strUserName
Learning Points
Note 1: Pay attention to detail and spot the extra \ in the strHomeServer,
for example "\\alan\home\"
Note 2: The structure of the last line, see where the ampersand (&) is used and
check where you need a comma. Another surprise to beginners is that
there are no speech marks in the last line, they are taken care of earlier when
we initialized the variables.
Note 3: If you are using VBsEdit then you will see how it colour codes the command to
make it easier to read and quicker to troubleshoot.
The additional parts of this script are optional. They help with
troubleshooting and make it make it more understandable to outsiders.
' MapHomeUsername.vbs - Map Network Drive to S: to the alan server
' VBScript using the .MapNetworDrive Method
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.6 - May 23rd 2004
' -----------------------------------------------------------------'
Option Explicit
Dim objNetwork, strUserName, strDriveLetter, strHomeServer
Set objNetwork = CreateObject("WScript.Network")
strDriveLetter = "S:"
strHomeServer = "\\alan\home\"
strUserName =objNetwork.UserName
objNetwork.MapNetworkDrive strDriveLetter, strHomeServer & strUserName
Wscript.Echo "Mapped Drive " & strDriveLetter & " to " & strHomeServer
WScript.Quit
' End of Script
Learning Points
Note 1: At last I have added the header information. An apostrophe tells VBscript
not to execute that line. Use apostrophes freely to add remarks. If
you explain the purpose of each section, then it will help
you and others, to modify the script later.
Note 2: Option Explicit, forces us to declare all variables and so reduced typo
errors later in the script.
Note 3: Dim literally means dimension. In practice Dim means declare the
following variables, in this case objNetwork, and the three string
variables.
Note 4: Talking of variables, by introducing variables, it makes it relatively
easy to echo meaningful error messages.
MapNetworkDrive is a classic method which uses a network object to map
user's home drive. When you employ this method in your scripting, take
the time to initialize variables so that troubleshooting will be easier.
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.
|