Guy’s Scripting Ezine 3 – Map Network Drive

Contents of Guy’s Scripting Ezine 3 – Map Network Drive

N.B. See a better more up-to-date version of this page Here.

1) A secret
2) Script to connect a network drive
3) Logon Script Tip
4) Links to more Scripts
5) Guy’s Litmus Tests

1) A secret

This week I was operating outside my usual area of expertise; I needed a CGI (Common Gateway Interface) script for an autoresponder project.  Now Guy knows very little about CGI scripts and This Week’s Secret is the CGI code made me feel like a beginner. I wished that there were more remarks or comments to help me understand the purpose of the script I was adapting.

So, I have taken to heart this problem of beginners needing explanations in scripts, and this week I have put in lots of comments to explain what each section is about.

Guy Recommends: The Free IP Address Tracker (IPAT) IP Tracker

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

2) Script to connect a network drive.

There are many learning points in this script
a) Simple mapping network drive method – MapNetworkDrive
b) The logic which disconnects K:\ drive if it is already connected. People often ask me how to check or disconnect a network drive.
c) For, next loops
d) If, then, else statements

Copy the lines following ‘Script starts up to ‘Script ends.
Paste the script into notepad.
Make changes to the value \\alan\home
Remember to save with a .vbs extension.
Double click to test the script
If you like it, then adapt it and assign it to your users


‘Script starts here.
‘ VBScript by Guy Thomas
‘Purpose of this script is two fold:
‘Firstly, it maps the K:\ drive to \\server\share
‘Secondly, if the K:\ drive is connected it disconnects it!
‘ The next line creates the Shell or environment for the commands – do not alter

Set WshShell = WScript.CreateObject("WScript.Shell")

‘ The section sets the variables. Note the pattern variable = value
‘ You may change the variable names if you wish to check your understanding.

Set GuyNet = WScript.CreateObject("WScript.Network")
Set CheckDrive = GuyNet.EnumNetworkDrives()

‘ Be very careful with the DriveLetter, the value must be a CAPITAL letter

DriveLetter = "K:"

‘ You must change the VALUE \\alan\home, unless you have a server called alan

RemotePath = "\\alan\home"

‘ This section deals with a For … Next loop

AlreadyConnected = False
For i = 0 To CheckDrive.Count – 1 Step 2
If CheckDrive.Item(i) = DriveLetter Then AlreadyConnected = True

‘ This section uses the If = then, else logic
‘ This section tests to see if the Drive is already mapped,
‘ and if yes then to disconnect
‘ WshShell.PopUp is not strictly necessary

If AlreadyConnected = True then
GuyNet.RemoveNetworkDrive DriveLetter
GuyNet.MapNetworkDrive DriveLetter, RemotePath
WshShell.PopUp "Drive " & DriveLetter & "Disconnect, then connect successfully"

GuyNet.MapNetworkDrive DriveLetter, RemotePath
WshShell.PopUp "Drive " & DriveLetter & " connected successfully."

End if
‘Script ends here

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

3) Logon script tip

These week’s tip is to place comments in YOUR scripts. In particular, put remarks on the PURPOSE of the script, and the role of each section. The HOW the script is done should be apparent from the variables and the methods.

4) Links to scripts at Guy’s site.

1. Map home drive
2. Remove Network Drive

5) Litmus tests

Each Litmus test is fun, yet has a serious learning point. I am sure that there will be at least one test that will suit you. This week’s litmus test is:
Professionals put comments on the PURPOSE of the script
Amateurs do not use comments in their scripts.

See more about logon scripts

Logon Arguments  • Logon Map Network Drive  • Logon Scripts  • PowerShell Com Shell

• Ezine 3 Map Network Drive  • Ezine 4 Logon  • Tool Kit  • Import Users From AD – Free CSVDE Tool

Ezine 25 map  • Ezine 30 Map Simple  • Ezine 31 Mapnetworkdrive  • Ezines  • PowerShell Logon Scripts