The whole theme of my site is getting you started. Therefore, here is a series of three VBScripts which map a network drive
to a network share. There are few VBScript
frills on this page, just VBScripts for you to learn how to map a network drive by copying and then amending my code.
Our objective is to map the H: to a share called '\home'. In my example the server is called '\\alan', therefore the full UNC path is \\alan\home. We will be using Microsoft's VBScript and the code executes on any WSH client.
The best environment would be Windows Server 2003 with an XP client. However, to practice MapNetworkDrive scripts, you could create a share on your own desktop computer.
Pre-requisites
You need a computer with a network share. Therefore go to Line 2 of my script and change the name of the server from '\\alan' to the name of your server.
Either make sure that your shared folder is called '\home', or alternatively, alter the word '\home' in the script to match the name of your UNC share.
Instructions to Create a Map Network Drive Script
Copy and paste the script below into notepad or get a script editor such as OnScript (free download).
Change the server name from "\\alan to the name of your server.
Check the share name '\home'.
Save the file with a .vbs extension, e.g. MND.vbs or MapNetworkDrive.vbs.
Double click the resulting script and look in your Windows Explorer for a new drive letter called : home on 'alan' (H:)
Note 1: VBScript is an object based scripting language. On line 1 you can see how the code creates objNetwork. Line 2 then applies the famous MapNetworkDrive method to achieve
the actual drive mapping.
Note 2: If you run the script a second time,
then you get
WSH error message code 80070055. The solution is launch Windows Explorer, and then just right-click on: home on 'alan' (H:) and select: Disconnect. (See diagram opposite). Now you can run the
script again. Alternatively, you could create a fancy script, which maps to the first free drive letter, however that is difficult task, but see here if you want to try that advanced script.
Note 3: You could try mapping to a different drive letter, there is nothing magical about the H:\. MapNetworkDrive works just as well with the J:\ or Z:\.
Note
4: I reduced this first example script to the
bare minimum. All I wanted was for you to gain success mapping a network drive. The next examples are more typical of production Microsoft VBScripts because they introduce variables, comments and a header.
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.
Our objective with this map network drive script remains to map a drive, but this time
it's the J: drive. My share name and server are the same as example 1, '\home' and '\\alan'.
Pre-requisites.
On Line 10 change the server name from '\\alan' to your server name.
Make sure that your server has a share called '\home'.
Instructions to MapNetworkDrive
Copy and paste the script below into notepad.
Check strPath, your server is unlikely to be called "\\alan, so amend to the name of your server.
Save the file with .vbs extension e.g. MapNetworkDrive.vbs.
Double click your script and check in your Windows Explorer for a new drive called : home on 'alan' (J:)
MapNetworkDrive.vbs
' VBScript to map a network drive to a UNC Path.
' Author Guy Thomas http://computerperformance.co.uk/
' Version 2.3 - September 2010
' ----------------------------------------------------'
Option Explicit
Dim objNetwork
Dim strDriveLetter, strRemotePath strDriveLetter = "J:"
strRemotePath = "\\alan\home"
' Purpose of script to create a network object. (objNetwork) ' Then to apply the MapNetworkDrive method. Result J: drive
Set objNetwork = CreateObject("WScript.Network")
Note 1: At the top of the script is a heading section. The idea of the header is to explain what this VBScript will achieve. Some script writers feel that the Dim statements, which
declare variables, are also part of the header section.
Note 2: Option Explicit is a VBScript command which forces me to declare variables. Not only is this 'best practice', but in my case, it alerts me to typos later in the script.
Note 3: See how this script declares the variables strDriveLetter and strRemotePath, then reuses them later in the script. If you stick with me, you will see that I love variables.
In this example, MapNetworkDrive employs just two arguments, drive letter and UNC path. See here for the full list of MapNetworkDrive arguments.
Note 4:
Once we declare strDriveLetter, then we can assign it a value, in this case "J:". One perennial problem I have with scripting is paying attention to detail, especially the syntax. Even with a
simple letter - J, we must be careful. For the script to succeed we need precisely "J:". Neither "J:\", nor "J\:" will work.
Note 5: All three
examples on this page employ "Network" objects, as defined by CreateObject("WScript.Network"). I mention the Network object because in the broader picture, VBScript can also create other objects,
for example, Shell or LDAP / Active Directory.
Guy
Recommends: Permissions Analyzer - Free Active Directory Tool
I like the
Permissions Monitor because it enables me to see quickly WHO has permissions
to do WHAT. When you launch this tool it analyzes a users effective NTFS
permissions for a specific file or folder, takes into account network share
access, then displays the results in a nifty desktop dashboard!
Think of all the frustration that this free utility saves when you are
troubleshooting authorization problems for users access to a resource.
Give this permissions monitor a try - it's free!
Our objective remains to map a drive, but this time I have chosen the K: drive. My share name and server are the same as example 1, '\home' and '\\alan'. The only difference is to add a message box so that you
know what's happening.
Pre-requisites.
On Line 10 change the server name from '\\alan' to your server name.
Make sure that your server has a share called '\home'.
Instructions to MapNetworkDrive
Copy and paste the script below into notepad.
Change the server name from "\\alan to the name of your server.
Save the file with .vbs extension e.g. MapNetworkDrive.vbs.
Double click and check in your Windows Explorer for a new drive called : home on 'alan' (K:)
' MapNetworkDrive.vbs ' VBScript to map a network drive. And provide a message box ' Author Guy Thomas http://computerperformance.co.uk/
' Version 3.2 - September 2010 '
----------------------------------------------------' Option Explicit Dim objNetwork Dim strDriveLetter, strRemotePath
' Purpose of the script to create a network object. (objNetwork)
' Then to apply the MapNetworkDrive method. Result K: drive Set objNetwork = CreateObject("WScript.Network") objNetwork.MapNetworkDrive
strDriveLetter, strRemotePath
' Extra code just to add a message box WScript.Echo " Launch Explorer, check: "& strDriveLetter
WScript.Quit
' End of
MapNetworkDrive Example Logon Script.
Learning Points
Note 1: Debugging is not one of Microsoft VBScript's strengths. This is why I include so many message boxes in my test scripts. Such messages are not necessary in production scripts,
mainly
because they confuse or annoy users.
Note 2: See how I cunningly reuse the variable strDriveLetter in the WScript.Echo. Perhaps you are beginning to realize why I am so keen on variables.
»
Use PowerShell for Logon Scripts
VBScripts are being superseded by PowerShell .ps1 files.
While PowerShell is used mainly for configuring the operating system, it's
possible to its cmdlets to MapNetworkDrive. 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 manipulating MapNetworkDrive:
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
assigning PowerShell logon scripts.
Group Policy Drive Maps
The modern group policy method of drive mapping does not require any
knowledge of either VBScript or PowerShell. In Windows Server 2008 you can launch the
GPMC and configure Drive Maps in the Preferences section.
See more on Group
Policy Drive Maps.
The mission of this page was to get you started creating logon scripts. Mapping a drive letter to a UNC is mainstream application of VBScript. I wanted to give you three variations of the MapNetworkDrive method, so that you could decide which features to include in your logon script.
The 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.
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.