Guy’s Scripting Ezine 30 – Map Network Drive

Contents for Guy’s Scripting Ezine 30 – Map Network Drive.

This Week’s Secret

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.

Guy Recommends: SolarWinds Free Wake-On-LAN UtilitySolarwinds Wake-On-LAN

Encouraging computers to sleep when they’re not in use is a great idea – until you are away from your desk and need a file on that remote sleeping machine!

WOL also has business uses for example, rousing machines so that they can have update patches applied.  My real reason for recommending you download this free tool is because it’s so much fun sending those ‘Magic Packets’. Give WOL a try – it’s free.

Download your free copy of SolarWinds Wake-On-LAN

Example 1 – Simple Map Network Drive

This first example will map a network drive.  It’s is about as short as I can make a VBScript.

Instructions

  1. Copy and paste the script below into notepad.  Alternatively, use a script editor like VBsEdit.
  2. 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.
  3. Save the file with .vbs extension e.g. MapK.vbs
  4. Double click and observe drive letters in explorer.  What you are looking for is the K:\
  5. 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)

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

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.

What I like best is the way NPM suggests solutions to network problems.  Its also has 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 try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

Example 2 – Adding variables

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. 

Example 3 – Mapping to a sub folder

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.

Recommended: Solarwinds’ Permissions Analyzer – Free Active Directory ToolFree Permissions Analyzer for Active Directory

I like thePermissions Analyzer because it enables me to see WHO has permissions to do WHAT at a glance.  When you launch this tool it analyzes a users effective NTFS permissions for a specific file or folder, and takes into account network share access, then displays the results in a nifty desktop dashboard!

Think of all the frustration that this free SolarWinds utility saves when you are troubleshooting authorization problems for user’s access to a resource.  Give this permissions monitor a try – it’s free!

Download SolarWinds’ Free Permissions Analyser – Active Directory Tool

Example 4 – Adding the header information

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 https://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.

Summary

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.

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  • Import Users From AD – Free CSVDE Tool

Ezine 25 map  • Ezine 30 Map Simple  • Ezine 31 Mapnetworkdrive  • Ezines  • PowerShell Logon Scripts