Guy's Scripting Ezine 32 - Remove Network Drive
Contents for Guy's Scripting Ezine 32 - RemoveNetworkDrive
This week I have a script which is a work of art. Some scripts are functional, they just get the job done, other scripts are ugly, they rely on work-arounds, but this week I have a script, which is the 'Best of Both Worlds', fun to execute and a joy to dissect.
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
There comes a time when you no longer need a mapped network drive. Perhaps you are re-assigning drive letters, or in my case I just want to undo mapped network drives that you created in testing. Another application of this technique would be for a Logoff script to cleans up the computer.
The purpose of this Logon Script is to remove a mapped network drive called P: Here is the key method: objNetwork.MapNetworkDrive.
1. Preamble: A Script to create a mapped network drive, in preparation for the main Example.
Set objNetwork = CreateObject("WScript.Network")
Main Example 1 Remove network drive.
' RemoveDrive.vbs - Removes Mapped Network Drive
Note 1: As usual, I have chosen to declare the DriveLetter1 as a variable
rather than say:
My reasoning is a) So that I can make the echo statement more effective, b) It's a habit I get into so that longer scripts can be controlled from on place.
Note 2: There is no UNC path in this script. All that is needed is the drive letter, the script does not care where that letter is mapped.
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.
Here is a clever script which will map the network drive if its connected, or else disconnect the drive if it is already present. Very handy in testing scripts and experimenting with 'if.. then...logic.
Naturally, the script would need modification to be useful in a production network. For example, if the drive letter was already connected, then after disconnecting, you could connect to different UNC path.
' BothWorlds.vbs - Removes or adds a Mapped Network Drive
Note 1: Bizarrely, the drive letter must be in CAPITALS, "P:" is correct, but "p:" fails with a not too helpful error message.
Note 2: In addition to the MapNetworkDrive method, did you spot another method? EnumNetworkDrives is an essential method to expose, or cycle through the mapped drive letters. Also remember that is a plural word - EnumNetworkDriveS.
Note 3: The logic of the script is handled by the If Then... Else.... End construction. In this instance the script says: if AlreadyConnected = True, then disconnect. Else (If AlreadyConnected is false) connect the drive. As I mentioned earlier, in a production script you could amend this logic to achieve your goal. For instance, if the drive is already mapped do nothing, else map the UNC path to the "P:" drive.
Note 4: The line 23: For i = 0 To AllDrives.Count - 1 Step 2, is responsible for cycling through the drive letters. i is the counter. AllDrives.Count is the total of all your drives. Step 2 is a surprise. The situation is that each drive has 2 properties, so each cycle is more efficient with Step 2 rather than Step 1. However Step 1 or even Step 3 will work on this occasion.
Remove Mapped network drive is a useful addition to your logoff script collection. You may also consider using the RemoveNetworkDrive script when testing. This script is also useful for testing 'if..logic'.
See more about logon scripts