Guy’s Scripting Ezine 87 – Re-visiting MapNetworkDrive

Contents for Ezine 87 – Re-visiting MapNetworkDrive

 ♣

This Week’s Secret

In my youth, my mother used to say that a woman’s work is never done.  If I apply her sentiment to VBScript, I could say that a scriptwriter’s code is never done.  When I first wrote the AlreadyConnected logon script back in Ezine 32, I thought that it was perfect.  However, this week when a reader asked for a variation, I suddenly became aware of at least three improvements that I could make to this faithful old script.

My message to those of a nervous disposition is that it’s OK to copy other peoples script and make amendments.  My message to the confident and the gung-ho, is that you can always improve upon your script.

The highlight of this week’s ezine is my ‘Learning points’ section where I explain how I have adapted and amended the Already Connected logon script.

Guy Recommends: Tools4ever’s UMRAUMRA The User Management Resource Administrator

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.

Scenario: You want to map a network drive.  However, you also want to remove any existing mapped drives.

I start with the assumption that you already familiar with the MapNetworkDrive method.  The situation is that while you want to map a drive letter to specific UNC path, you wish to take the opportunity to clean up any old drive mappings.  Yes, you want to map strDriveLetter to strRemotePath, but you also want to delete any other mapped drive letters.

From a learning point of view, I want to show you how to adapt and improve a basic script (Ezine 32) to a different scenario.

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

Script to Map Network Drive – Also deletes all other drives.

This script started out as a solution to the situation where the drive was already mapped.  The problem was when you ran the original script a second time you got a drive already in use error message.

Prerequisites

This script needs a shared drive.  If all else fails create a shared folder on the machine where you run the script.  If you are not familiar with MapNetworkDrive, then have a look at Ezine 32.

Instructions for Creating a VBScript to display lastLogon time.

  1. Edit this variable: strRemotePath = " \ \grand\home"
  2. Do you want to map to the X: drive? Check strDriveLetter = UCase("x:" ) ‘
  3. Copy and paste the example script below into notepad or use a VBScript editor.
  4. One advantage of a good script editor such as OnScript is that you can see the line numbers (See menu on the upper left for free copy).
  5. Save the file with a .vbs extension, for example: RemoveThenMap.vbs 
  6. Double click RemoveThenMap.vbs and check the message box.

Example – To Map Network Drive – Plus Remove any Existing Mapped Drives

 

‘ RemoveThenMap.vbs Windows Logon Script
‘ VBScript to remove all previous mapped drives before
‘ using MapNetworkDrive to create the new network drive.
‘ Author Guy Thomas https://computerperformance.co.uk/
‘ Version 2.2 – September 2005
‘ ——————————————————‘
Option Explicit
Dim objNetwork, objShell, CheckDrive
Dim strDriveLetter, strRemotePath, intDrive, bforce

‘ The section sets the variables.
strDriveLetter = UCase("x:" ) ‘ Force UPPER case
strRemotePath = "\\grand\home"
bforce = true

‘ This sections creates two objects:
‘ objShell and objNetwork and counts the drives
Set objShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")
Set CheckDrive = objNetwork.EnumNetworkDrives()

‘ This section deletes all mapped network drives
On Error Resume Next
For intDrive = 0 To CheckDrive.Count – 1 Step 2
If CheckDrive.Item(intDrive) <> strDriveLetter _
Then objNetwork.RemoveNetworkDrive _
CheckDrive.Item(intDrive), bforce
Next

objNetwork.MapNetworkDrive strDriveLetter, strRemotePath
objShell.PopUp "Drive " & strDriveLetter & _
" connected successfully."

WScript.Quit

‘ Guy’s Map Network Drive Script ends here

Learning Points

At the risk of repeating my previous overconfidence, I now believe that this script is the bee’s knees, the cat’s whiskers and ‘the business’.  Here are the three features in the script that give me particular satisfaction.

Note 1:  Trace the role of the CheckDrive variable.  See how it relates to the EnumNetworkDrives method.  What I particularly like is the way that the script exposes any mapped drives with .item and item(intDrive).  It is thanks to CheckDrive that I am able to delete any extra drive letters.

Note 2:  As its name suggests, bForce is a Boolean enforce command to disconnect existing drives whether or not they are open or in use.  Observe that there is just one comma in this command:
Then objNetwork.RemoveNetworkDrive CheckDrive.Item(intDrive), bforce

Note 3:  Must strDriveLetter be in uppercase?  I used to think so, but I confess that when I double-checked, this script worked fine with a lowercase drive letter.  Still, it costs little for that nice touch "UCase()", which protects against, a) forgetting to use uppercase and b) some clients are case sensitive (Win98).

Summary of Mapped Network Drive

Scripts are fluid and flexible.  The job of the initial script was to map a network drive.  After modification, the script can delete any existing drives and also overcome the ‘drive already in use’ error.

See more about logon scripts

MapNetworkDrive  • Ezines  • Logon Map Network Drive  • Logon Scripts  • LEM 

Ezine 32 Remove  • Ezine 60 Rename  • PowerShell Logon Scripts  • Ezine 74 Map

Ezine 75 Shares  • Ezine 87 Map drive  • Ezine 105 Map Arguments  

Ezine 115 Map Groups  • Ezine 132 Assign Logon  • Tool Kit  • Free CSV Importer