MapNetworkDrive with extra VBScript code
Our objective remains to map a drive, but this time the J:. My share name and server are the same as example 1, ‘\printers’ and ‘\\server’.
Pre-requisites.
On Line 10 change the server name from ‘\\server’ to your server name.
Make sure that your server has a share called ‘\printers’.
Instructions to MapNetworkDrive:
- Copy and paste the script below into notepad.
- Check strPath, your server is unlikely to be called “\\server, 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 : printers on server (J:)
‘ MapNetworkDrive.vbs
‘ VBScript to map a network drive to a UNC Path.
‘ Version 3.1 – by: http://www.tips247.net/tag/mapped-network/
Option Explicit
Dim objNetwork
Dim strDriveLetter, strRemotePath
strDriveLetter = “J:”
strRemotePath = “\\server\printers”
Set objNetwork = CreateObject(“WScript.Network”)
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath
WScript.Quit
Note:
- 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.
- 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.
- 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.
- 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.
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.
loading...
loading...
