Logon Scripts

Windows Logon Scripts - Five Arguments for MapNetworkDrive

Introduction to Map Network Drive - Five Arguments

Whilst keeping in mind our goal, which is to map a network drive, this page explains the optional arguments available to the MapNetworkDrive method.  In addition to the network path and local drive letter, you can also specify the Active Directory credentials that a user needs to access the network share.

Topics for MapNetworkDrive - Five Arguments

 ♦

VBScript Background

Microsoft designed VBScript on the classical object, method and value structure.  The idea is that you create an object and then apply methods such as MapNetworkDrive.  In turn, the method or verb, manipulates values. 

Take as an example, assigning a drive letter to a Windows network share.  The key command is MapNetworkDrive, however, it needs a network object before MapNetworkDrive can assign the drive letter to the share.  In my minds eye, WSH has a whole library of objects.  Our job is to persuade VBScript to release one of these objects any time the method needs to manipulate values.  In VBScript, creating an object is summed up in one neat line:

Set objNetwork = CreateObject("WScript.Network")

I chose to call the object 'objNetwork'.  You can call the object what you like, but scripters like to stick to a pattern with their variables.  A prefix of str indicates a string value, while an obj prefix denotes an object.  Now that WScript has created objNetwork, we can start manipulating it with the method MapNetworkDrive.

MapNetworkDrive Arguments

When ever you employ a VBScript method to manipulate an object, take the time to check its arguments and syntax.  When you create the network drive letter there may complications, for example permission problems.  To overcome such problems, methods have optional arguments, in this case strUser and strPassword. Here is the full list of arguments for MapNetworkDrive:

objNetwork.MapNetworkDrive:
1) strDriveLetter, 2) strRemotePath, 3) bUpdateProfile, 4) strUser, 5) strPassword.

For most scripts you will only need MapNetworkDrive's two compulsory arguments, the drive letter (1) and the network share (2).  In addition, there are three optional arguments.  There is a command to save the drive letter in the user's profile.  You can also add username and password arguments so that you could connect to the share with a more privileged user account.  For instance, if you wish to map to a share where the ordinary users only have read access the mapping will fail, so you to map with an account which has full control of the network share.  If you omitted this step then the script would fail because the user did not have sufficient permissions to complete the drive mapping.

Special note. The format for the username can be plain "Guythom" or "Administrator".  If this gives trouble, try adding the domain name e.g. "cpdom\Guythom" or "cpdom\Administrator".  Where cpdom is the domain name.  Actually, this tip is especially useful where you are trying to map to a member server, in which case the cpdom\ prefix would be the machine name.

A little more about the third argument, which is referred to as bUpdateProfile.  This optional component controls whether the new mapped network drive should be stored in the user's profile.  Incidentally it took me ages to discover what the 'b' in bUpdateProfile meant, the answer was Boolean, in other words this argument takes either true or false.  The default is false, so the only time you need to enter a value is if you positively want the user to receive this drive every time they logon, in which case you set the bUpdateProfile value to true.

In conclusion, while there are 5 arguments, most of the time you will only need two, strDriveLetter and strRemotePath.  I have to tell you that while you must use these to arguments correctly, you may name the corresponding variables what ever you like.  For instance, I declare strDriveLetter for the drive letter.  If you prefer, you could call this argument dLetter or LocalDrive, in your scripts.  My point is learn to recognise when a method name is fixed and when a variable name is flexible.  For example you cannot refer to the method MapNetworkDrive as MapEmGood, but you can call the my variable strRemotePath  strRmPth if you prefer.  Naturally, whether you call it strRemotePath or strRmPth, the server and sharename must exist and be correctly spelt.

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 1 - MapNetworkDrive with 5 Arguments

Our objective is to map the H: to a share called '\home' on a Windows 2003 server called '\\alan'.  However in this example I want to show you how to employ all 5 of the MapNetworkDrive arguments, strDriveLetter, strRemotePath, strProfile, strUser and strPassword.

Pre-requisites

  1. On Line 15 change the server name from '\\alan' to your server name.
  2. Make sure that the shared folder is called '\home'.  Alternatively, alter the word '\home' in the script if your share is called something else.

Instructions to MapNetworkDrive

  1. Copy and paste the script below into notepad or get a script editor such as OnScript (free download).
  2. Change the server name from "\\alan to the name of your server.
  3. Check the share name '\home'.
  4. Save the file with .vbs extension e.g. MNDArguments.vbs.
  5. Double click the resulting logon script and look in your Windows Explorer for a new drive letter: home on 'alan' (H:)

' MNDArguments.vbs
' VBScript to map a network drive with all 5 arguments.
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.3 - April 24th 2010
' ---------------------------------------------------------'
Option Explicit
Dim objNetwork
Dim strDriveLetter, strRemotePath, strUser, strPassword, strProfile

' Values of variables set
strDriveLetter = "H:"
strRemotePath = "\\alan\home"
strUser = "guytom"
strPassword = "P@ssw0rd1"
strProfile = "false"

' This section creates a network object. (objNetwork)
' Then apply MapNetworkDrive method. Result H: drive
' Note, this script features 5 arguments on lines 21/22.
Set objNetwork = WScript.CreateObject("WScript.Network")
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath, _
strProfile, strUser, strPassword

' Extra code just to add a message box
WScript.Echo " Launch Explorer, check: "& strDriveLetter
WScript.Quit

' End of Example script .

Learning Points

Note 1: If necessary, create a share on your own machine. I confess, that just to get the VBScript working, I often experiment on my own machine.

MapNetworkDriveNote 2: If you run the script a second time, then you get WSH error message code 80070055.  The solution is launch Windows Explorer, just right-click on: home on 'alan' (H:) and select: Disconnect.  See diagram opposite.  Now you can run the script again.

Note 3: If you run the script a second time you get WSH error message code 800704xx, check the name of your server.

Note 4: Where a line is too long and needs a break, use the _ (underscore) and then continue on the next line.  Microsoft's VBScript does not understand word-wrap so this is why you need underscore for statements that span two lines.

objNetwork.MapNetworkDrive strDriveLetter, strRemotePath, false, _
strUser, strPassword

Guy Recommends: SolarWinds Network Topology Mapper (NTM)SolarWinds Network Topology Mapper

NTM will produce a neat diagram of your network topology.  But that's just the start; Network Topology Mapper can create an inventory of the hardware and software of your machines and network devices.  Other neat features include dynamic update for when you add new devices to your network.  I also love the ability to export the diagrams to Microsoft Visio.

Finally, Guy bets that if you test drive the Network Topology Mapper then you will find a device on your network that you had forgotten about, or someone else installed without you realizing!

Download your 14 day free trial of SolarWinds Network Topology Mapper

Example 2 - MapNetworkDrive Variations

This script does exactly the same job as Example 1.  What I want to demonstrate here is that there are a multitude of ways of scripting.  By approaching the problem for a different angle you get perspective on what Microsoft's VBScript is all about.

Notes

  1. This example uses lower case for strDriveLetter.  X or x would work equally well.
  2. Instead of declaring the optional arguments in advance, I just typed them in where needed.


' MNDArguments2.vbs
' VBScript to map a network drive with all 5 arguments.
' Author Guy Thomas http://computerperformance.co.uk/
' Version 2.2 - April 24th 2010
' ----------------------------------------------------'
Option Explicit
Dim objNetwork
Dim strDriveLetter, strRemotePath, strUser, strPassword, strProfile

' Values of variables set
strDriveLetter = "X:"
strRemotePath = "\\alan\home"

' This section creates a network object. (objNetwork)
' Then to apply the MapNetworkDrive method. Result X: drive
Set objNetwork = CreateObject("WScript.Network")
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath, false, gt, pass

' Extra code just to add a message box
WScript.Echo " Launch Windows Explorer, check: "& strDriveLetter
WScript.Quit

' End of Example VBScript.

Learning Points

®

Note 1: Try and figure out what you can and must change, e.g. servername.  Also, what is a special word cannot be varied in any way, e.g. WScript.Network.

Note 2: I shortened Set objNetwork = WScript.CreateObject("WScript.Network") by removing WScript
Set objNetwork = CreateObject("WScript.Network").  Note: you cannot shorten WScript.Network to plain Network.

Employ PowerShell for Logon Scripts

You may have noticed that VBScript is being phased out in favour of PowerShell.  Although PowerShell is used primarily for interrogating the operating sytetem it's possible to employ its cmdlets to configure the users' environment.  The technique is to create a ComObject, which can act as a wrapper for familiar VBScript commands. Here is example of PowerShell's New-Object cmdlet to manipulate MapNetworkDrive:

# PowerShell Logon 3 Script Example
Clear-Host
$Net = $(New-Object -ComObject WScript.Network)
$Net.MapNetworkDrive("X:", "\\alan\home")
$Net.objNetwork.EnumNetworkDrives
$Net

You could save these instructions in a .ps1 file.  However, the hard part is executing  this .ps1 file as a logon script.  See more about PowerShell logon script and Group Policy.

MapNetworkDrive Arguments Summary

If you want to master Microsoft's VBScript, then seek out methods, they are the 'doers', the verbs.  To really get to know each method, research all the arguments.  One day an optional argument will solve a problem, for example permissions errors with MapNetworkDrive.

 

Download my Logon Script eBook for only $6.25

Logon ScriptThe extra features you get in your eBook include, more pages full of detailed examples.  Also, ten 'how to...' sections, with screen shots showing which menus to use.  Go for Guy's eBook - and get a printable version with copy enabled and no expiry date.

  Jumbo Script 7 Package

If you like this page then please share it with your friends

 


See more logon scripts examples

Logon Script Home   • Disconnect Network Drives   • Map Network Drive Group   • Free CSV Tool

Map Network Drive Script   • Vbscript Map Network Drive Username   • Add Error Correcting Code

ObjNetwork.MapNetworkDrive   • Multiple Mapped Network Drives   • Troubleshooting logon scripts

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor for PowershellSolarwinds WMI Monitor

Windows Management Instrumentation (WMI) is most useful for PowerShell scripting.

SolarWinds have produced this Free WMI Monitor to take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2017 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: