Guy’s Scripting Ezine 25 – MapNetworkDrive

Contents for Guy’s Scripting Ezine 25 – MapNetworkDrive

This Week’s Secret – Problems mapping drive letters

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.

Guy Recommends: The Free IP Address Tracker (IPAT) IP Tracker

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

Example 1 – Straight forward MapNetworkDrive

Our objective is to map the N: to a server called ‘alan’ with a share called ‘home’.

Instructions

  1. Pre-requisites.  On Line 12 change the server name from ‘alan’ to your server name (It does not have to be a domain controller).
  2. Copy and paste the script below into notepad.
  3. Save the file with .vbs extension e.g. MapNetworkDrive.vbs
  4. Double click and observe the message box
  5. Check your explorer for a new N:
 

‘ MapNetworkDrive.vbs
‘ VBScript to map a network drive.
‘ Author Guy Thomas https://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.

Example 2 – Map to a users sub-folder – e.g. Home drive

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

  1. 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.
  2. Pre-requisites.  Change the server name from ‘alan’ to your server name (does not have to be a domain controller). 
  3. Copy and paste the script below into notepad.
  4. Save the file with .vbs extension e.g. MapNetworkDrive2.vbs
  5. Double click and observe the message box
  6. Check your explorer for a new Q:
 

‘ MapNetworkDrive2.vbs
‘ VBScript to map a network drive.
‘ Author Guy Thomas https://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)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

Out Take 1

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

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