Is your server running slowly? Check with SolarWinds ipMonitor
Get a free evaluation copy of ipMonitor
Guy's Scripting Ezine 31 - Map Network Drive
part 2
Until I researched MapNetworkDrive, I had forgotten just
how many 'arguments' and variations there are. In fact I have unearthed
so many examples that I have delayed the RemoveNetworkDrive and
EnumNetworkDrives methods until another day.
As usual, I will show you more than just one technique. As
ever, my goal is to widen your scripting horizons with worked examples.
The idea is to map two shares with one script. Naturally, each share
is assigned to a different drive letter.
Instructions
- Copy and paste the script into notepad. Alternatively, use a
script editor like VBsEdit.
- Find my server called ALAN and change to the name of YOUR server.
Also edit the
following share names, Drivers and also Sports Share.
- Save the file with .vbs extension e.g. TwoMap.vbs
- Double click and observe drive letters in explorer. What you are
looking for is the M:\ and a P:\.
- 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.
' TwoMap.vbs - Map Network Drive to M: and P:
' VBScript Mapping two drives in one script.
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.4 - May 30th 2004
' -----------------------------------------------------------------'
Option Explicit
Dim DriveLetter1, DriveLetter2, RemotePath1, RemotePath2
Dim objNetwork
Set objNetwork = CreateObject("WScript.Network")
DriveLetter1 = "M:"
DriveLetter2 = "P:"
RemotePath1 = "\\ALAN\Drivers"
RemotePath2 = "\\ALAN\Sports Share"
objNetwork.MapNetworkDrive DriveLetter1, RemotePath1
objNetwork.MapNetworkDrive DriveLetter2, RemotePath2
WScript.Echo "Mapped drives " & DriveLetter1 & " also " & DriveLetter2
Wscript.Quit
' End of example VBScript
Learning Points
Note 1: There are a surprising number of traps when mapping multiple
drives. The central point is that each new drive letter has its own
line.
objNetwork.MapNetworkDrive DriveLetter2, RemotePath2
You could repeat the above line for each share you want to map, provided you change the
DriveLetter2 and RemotePath2. Mistakes arise where people get too
clever and try to map 6 drives all on one line.
Note 2: From a scripting point of view, you make do with just one
objNetwork which you recycle. It is not necessary to Set objNetwork1,
objNetwork2 for each drive, just recycle the one variable - objNetwork.
Note 3: Although I disapprove, it is possible to use share names with
spaces. This script will run provided you have NT, XP or W2K Pro clients. (No Window 9x thank
you.)
Note 4: In passing, in this script I have not prefixed the string values
with str. To be consistent, I should have called it strDriveLetter1.
Could this be Guy's way of showing variety in the naming variables, or it
could be an excuse that I have been an 'idle toad'?
Sometimes you may need to map to a hidden share. Where you have the
classic UNC path, you can add the dollar sign directly to the share, just use
the $ symbol in the same way as any other character. However, if you wish
to map to a third level, then you need a different technique. For
example \\ alan\home\Daniel$ will be covered in Example 2b.
Example 2a
strHomeServer = "\\ alan\Daniel$"
My point is that for this example, there is no need to specifically join the dollar
to the rest of the name with an
ampersand.
strHomeServer = "\\ alan\Daniel" ' Then followed by
strHomeServer = strHomeServer & "$" ' Would be laborious.
Naturally, you need a share called Daniel$ for this to work.
' Dollar.vbs - Map Network Drive to S: to the alan server
' VBScript using the .MapNetworDrive Method
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.9 - May 23rd 2004
' -----------------------------------------------------------------'
Option Explicit
Dim objNetwork, strUserName, strDriveLetter, strHomeServer
Set objNetwork = CreateObject("WScript.Network")
strDriveLetter = "S:"
strHomeServer = "\\alan\Daniel$"
strUserName =objNetwork.UserName
objNetwork.MapNetworkDrive strDriveLetter, strHomeServer
Wscript.Echo "Mapped Drive " & strDriveLetter & " to " & strHomeServer
WScript.Quit
' End of example VBScript
Learning Points
Note 1: Where ever possible - Keep the script simple.
Example 2b The More complex three part strHomeServer
Remember the most important lesson from last week was that you can only map to
the UNC path. So your final script depends on whether you are dealing
with \\ alan\home\Daniel$ or the shorter "\\ alan\Daniel$". In
also depends whether the share is on the home folder or the Daniel folder.
Option Explicit
Dim objNetwork, strUserName, strDriveLetter, strHomeServer
Set objNetwork = CreateObject("WScript.Network")
strDriveLetter = "S:"
strHomeServer = "\\alan\home\"
strUserName =objNetwork.UserName
strUserName = strUserName & "$"
objNetwork.MapNetworkDrive strDriveLetter, strHomeServer & strUserName
Learning Points
Note 1: If you are getting into a pickle with the three levels and the
dollar sign, then go back to basics. Create a normal two level servername, sharename script and
then gradually add complexity.
Occasionally, I get letters from people wishing to map a drive, but their
script fails because the share permissions do not allow the current user to
create the drive mapping. Personally, I
would rather alter the share permissions so that the original script ran as
designed. My other fear is exposing the administrator's password -
even in a script that the users will not see.
Nevertheless, adding username and password make for an interesting project, because
this
introduces more arguments; and do like collecting switches, arguments and
methods.
Instructions
This time I am using the str prefix for the string variables, so it
should be easy for you to spot the places where you need to amend the script
to suit your network.
' TestConnect.vbs - Map Network Drive to N: to the alan server
' VBScript using all 5 MapNetworkDrive arguments.
' Author Guy Thomas http://computerperformance.co.uk/
' Version 2.1 - May 30th 2004
' -----------------------------------------------------------------'
' object.MapNetworkDrive 5 arguments
' (strLocalName, strRemoteName, [bUpdateProfile], [strUser], [strPassword])
Option Explicit
Dim strUser, strPassword, strDriveLetter, strHomeServer, strProfile
Dim objNetwork, objPopUp
Set objNetwork = CreateObject("WScript.Network")
Set objPopUp = CreateObject("WScript.Shell")
strDriveLetter = "N:"
strHomeServer = "\\alan\home"
strProfile = "False" ' Mapping (not) stored in user Profile
strUser = "Administrator"
strPassword = "B££r & L@ger"
objNetwork.MapNetworkDrive strDriveLetter, strHomeServer, strProfile, strUser,
strPassword
objPopUp.popup "Drive " & strDriveLetter & " connected successfully."
WScript.Quit
' End of example VBScript
Learning Points
Note 1: strProfile is needed otherwise there are not enough 'arguments'
and you get a 'Type Mismatch' error. In my minds eye, the script needs
the third argument if it will to go on and read the strUser and strPassword.
I know, it is curious that the script will work with just the first two
variables.
Note 2: This is the week of trying out new ideas. Just for fun I used a
different method to create my beloved message box. Did you spot the
construction? Another surprise was that .popup is a property of
Shell objects not network object. That is why I created a special
shell object = objPopUp rather than reused objNetwork.popup.
MapNetworkDrive has a surprising number of arguments, twists and
turns, perhaps that is why this logon script method is so versatile and ubiquitous.
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.
|