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.
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.
Instructions
- Preamble, create a mapped network drive = P: (See Below)
- Copy and paste the main script into notepad.
- Change the server name ALAN to the name of Your server. Check the share name = Drivers.
- Save the file with .vbs extension e.g. RemoveDrive.vbs
- Double click and observe drive letters in explorer. What you are
looking for is the P:\ drive.
- If you get errors, check that you have amended the object names alan or
home. If you still get Code 800xxxxx message boxes, pay close attention
to the commas and speech marks on the second line.
1. Preamble: A Script to create a mapped network drive, in preparation for the main Example.
Set objNetwork = CreateObject("WScript.Network")
Set objShell = CreateObject("WScript.Shell")
DriveLetter1 = "P:"
RemotePath1 = "\\alan\Drivers"
objNetwork.MapNetworkDrive DriveLetter1, RemotePath1
objShell.PopUp "Drive " & DriveLetter1 & " connected successfully."
Wscript.Quit
' End of example VBScript
Main Example 1 Remove network drive.
' RemoveDrive.vbs - Removes Mapped Network Drive
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.5 - June 6th 2004
' -------------------------------------------------------------'
Option Explicit
Dim objShell, objNetwork, DriveLetter1
DriveLetter1 = "P:"
Set objShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")
objNetwork.RemoveNetworkDrive DriveLetter1
objShell.PopUp "Drive " & DriveLetter1 & " disconnected."
Wscript.Quit
' End of example VBScript
Learning Points
Note 1: As usual, I have chosen to declare the DriveLetter1 as a variable
rather than say: objNetwork.RemoveNetworkDrive "P:"
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.
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
' Example VBScript to test whether drive is already connected
' Author Guy Thomas http://computerperformance.co.uk/
' Version 3.0 - June 6th 2004
' -------------------------------------------------------------'
Option Explicit
Dim objShell, objNetwork
Dim DriveLetter1, DriveLetter2, RemotePath1, RemotePath2
Dim AllDrives, AlreadyConnected, Network1, Network2, i
Set Network1 = CreateObject("WScript.Network")
DriveLetter1 = "P:" ' This letter must be in CAPITALS.
RemotePath1 = "\\alan\Drivers"
Set objShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")
Set AllDrives = objNetwork.EnumNetworkDrives()
AlreadyConnected = False
For i = 0 To AllDrives.Count - 1 Step 2
If AllDrives.Item(i) = DriveLetter1 Then AlreadyConnected = True
Next
If AlreadyConnected = True then
objNetwork.RemoveNetworkDrive DriveLetter1
objShell.PopUp "Drive " & DriveLetter1 & " disconnected."
Else
objNetwork.MapNetworkDrive DriveLetter1, RemotePath1
objShell.PopUp "Drive " & DriveLetter1 & " connected successfully."
End if
Wscript.Quit
' End of example VBScript
Learning Points
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'.
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.
|