VBScript Mapping network drives was my killer reason for learning VBScript. Each week new readers join the ezine, for them I want to give examples
so they can get started with VBScript. A good place to start is a simple script to map a network share to local drive letter.
For the more experienced scripters, I want to remind them that methods such as MapNetworkDrive have five arguments. The UNC path and the local
drive letter are two of the obvious arguments. Several readers and asked how you can map a network drive using different a different user, the answer is to add extra arguments for the user account and its password.
PowerShell If you are new to PowerShell, then one learning tactic is to recreate tasks that you did in VBScript, but using PowerShell
commands instead. In the case of MapNetworkDrive, the
answer is straightforward, in PowerShell create a Com object, which you then manipulate much like VBScript WScript.CreateObject. See example 2.
Walk-through Tip It seems that I am one of the few script writers who recommends a physical walk-through of the task in hand. What a walk-through does is, a) Help in troubleshooting, b) remind
us that a script can do anything we can do manually, c) In the case of Mapping a Network drive, reveal a box called reconnect at logon. My point is that the 5th mapnetworkdrive argument is called bUpdateProfile, which is the
equivalent of ticking the dialog box 'Reconnect at Logon' if map a drive using Windows Explorer.
Incidentally, my old friend Mad Mick
is wrong when he says the 'b' in UpdateProfile means 'bloody'. In fact, the 'b' stands for Boolean and the allowable values are either ="true" or ="false".
My secondary
mission is to show you to how PowerShell can create com objects, which you can then manipulate to map a UNC path to a local drive letter.
On Line 15 change the server name from '\\grandserver' to your server name.
Make sure that the shared folder is called '\downloads'. Alternatively, alter the word '\downloads' in the script to reflect a share on your server.
Instructions to MapNetworkDrive
Copy and paste the script below into notepad or get a script editor such as
OnScript (free download).
Change the server name from "\\grandserver to the name of your server.
Check the share name '\files'.
Save the file with .vbs extension e.g. MapNetDrvArguments.vbs.
Double click the resulting logon script and look in your Windows Explorer for a new drive letter: sharename on 'grandserver' (J:)
' MapNetDrvArguments.vbs ' VBScript to map a network drive with all 5 arguments. ' Author Guy Thomas http://computerperformance.co.uk/ ' Version 1.1 - November 2006 '
---------------------------------------------------------------' Option Explicit Dim objNetwork Dim strDriveLetter, strRemotePath, strUser, strPassword, strProfile Dim objShell
' Set values
for the 5 variables strDriveLetter = "J:" strRemotePath = "\\grandserver\files" strUser = "administrator" strPassword = "P#ssw0rd" strProfile = "false"
' This section creates a network
object. (objNetwork) ' Then apply MapNetworkDrive method. Result J: 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
Call Explorer WScript.Quit
Sub Explorer() If err.number = vbEmpty then Set objShell = CreateObject("WScript.Shell") objShell.run ("Explorer" &" " & strDriveLetter & "\" ) Else Wscript.echo
"VBScript Error: " & err.number End If err.clear End Sub
Note 1: If you don't have access to a server, create a share on your own machine.
Note 2: If you run the script for a second time,
then you get
WSH error message code 80070055. The solution is launch Windows Explorer, just right click on: sharename on computer (J:) 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 of code is too long to fit on one line, 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.
Note 5: The Sub Explorer() is solely for testing, it would be inappropriate to launch the explorer in a production script. Talking of testing,
I often add a line to remove the network drive in my test script. For example add this BEFORE the mapnetworkdrive line: objNetwork.RemoveNetworkDrive, strDriveLetter
If you are looking for handy network utilities, try some of the free downloads at
Tools4Ever
As with Example 1, this script applies the MapNetworkDrive method to map a UNC path to a local drive letter.
Prerequisites
Install PowerShell RC2
Instructions
Save the following script with a .ps1 extension, for example PSMap.ps1
Launch PowerShell and navigate to the folder where you saved the .ps1 file
Call
for your script file; at the command line PS>, type
.\PSmap.ps1
# PSMap.ps1 # Maps a network drive using PowerShell # Author Guy Thomas http://computerperformance.co.uk/ # Version 2.5 - November 2006 cls $Drive = "K:" $UNC = "\\grand\downloads" # Create the Com object with New-Object -com $net = New-Object -com WScript.Network;
# $net.removenetworkdrive($Drive)
$net.mapnetworkdrive($Drive,$Unc)
# Launches the Explorer $shell = new-object -comObject Shell.Application $Shell.open($Drive)
Learning Points
Note 1: While PowerShell really does require fewer instructions than VBScript, this script exaggerates the effect by not using so many arguments.
Note 2:
Whereas VBScript uses the ' Apostrophe for remarks, PowerShell uses the hash symbol for such # comments.
Note 3: -Com and -comObject seem interchangeable. Also, as expected, powershell
commands are not case sensitive. I have deliberately drawn attention to this case insensitivity by using New-Object and then new-object.
Note 4: Cls is unnecessary, it is merely one of my
quirks to clear my screen and show that at least something is happening should a test script fail.
Note 5: After you run the script for the first time, you may wish to remove the # in front of #
$net.removenetworkdrive($Drive). This stops the script complaining that a drive has already been mapped. For a production script we would need better error correcting code.
It is reassuring that everything that you can create with VBScript, you can also achieve in PowerShell. For methods such as MapNetworkDrive the secret is
to create -com objects in powershell and then manipulate them with familiar VBScript commands.
MapNetworkDrive was also a useful vehicle to review what arguments a particular methods supports.
Their topics and material are ideal for getting you started with VBScript. The
videos are easy to follow and you can control the pace. Try their free demo material and then see if you want to buy the full package.
See more about VB Script Training CD.
*
Guy
Recommends: Orion's NPM - Network Performance Monitor
Orion's performance monitor is designed for detecting network outages.
A network-centric
view make it easy to see what's working, and what needs your attention.
This utility guides you through troubleshooting by indicating whether the
root cause is faulty equipment or resource overload.