Guy's Scripting Ezine 31 - Map Network Drive - Part 2
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.
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
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'?
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.
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.
Guy Recommends: Solarwinds' Free Bulk Import Tool
Import users from a spreadsheet. Just provide a list of the
users with their fields in the
top row, and save as .csv file. Then launch this FREE utility and match
your fields with AD's
attributes, click and import the users. Optionally, you can
provide the name of the OU where the new accounts will be born.
There are also two bonus tools in the free download, and all 3 have been approved by Microsoft:
- Bulk-import new users into Active Directory.
- Seek and zap unwanted user accounts.
- Find inactive computers.
Download your FREE bulk import tool.
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.
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
|