Guy’s Scripting Ezine 129 Mapping Network Drives
- This Week’s Secret
- This Week’s Mission
- Example 1 – Mapping a network drive with VBScript
- Example 2 – Mapping a network drive with PowerShell
- Guy’s Challenges
- Summary of MapNetworkDrive with PowerShell
♣
This Week’s Secret
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.
This Week’s Mission
This week I want to review mapping network drives in general and adding arguments for username and password in particular.
Here are the five possible arguments for MapNetworkDrive:
objNetwork.MapNetworkDrive:
1) strDriveLetter, 2) strRemotePath, 3) bUpdateProfile, 4) strUser, 5) strPassword.
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.
Guy Recommends: The Free IP Address Tracker (IPAT)
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 – Mapping a network drive with VBScript
Pre-requisites
- 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 https://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
‘ End of Example script .
Learning Points
Note 0: To see the line numbers, get a script editor such as OnScript (free download).
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.
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath, false, _
strUser, strPassword.
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
Guy Recommends: Tools4ever’s UMRA
Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.
It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.
Example 2 – Mapping a network drive with PowerShell
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 https://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.
Guy’s Challenges
Challenge 1: At the PowerShell command line, review the $net arguments by typing:
$net | get-member
Challenge 2: Research other com objects that you can create in PowerShell by typing:
get-help new-object -full
Summary of MapNetworkDrive with PowerShell
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.
If you like this page then please share it with your friends
See more Windows PowerShell tutorials
• PShell Home • Introduction • Dreams • 3 Key Commands • PowerShell Help About • Get-Help
• PowerShell v 3.0 • Set-ExecutionPolicy • Get-Command • Cmdlet scripts • Import-Module
• PowerShell Version Check • Backtick • PowerShell examples • PowerShell ISE • Get-Member
Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.