PowerShell Ezine, Logon Scripts

Guy's Scripting Ezine 32 - Remove Network Drive

Contents for Guy's Scripting Ezine 32 - RemoveNetworkDrive

This Week's Secret

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) 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

Example 1 -  Remove Mapped Network Drive

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

  1. Preamble, create a mapped network drive = P: (See Below)
  2. Copy and paste the main script into notepad.
  3. Change the server name ALAN to the name of Your server.  Check the share name = Drivers.
  4. Save the file with .vbs extension e.g. RemoveDrive.vbs
  5. Double click and observe drive letters in explorer.  What you are looking for is the P:\ drive.
  6. 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.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v10

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

Example 2 - Best of both worlds

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.

Summary of Remove Network Drive

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

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


Custom Search

Site Home

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

Fortunately, SolarWinds have created the Free WMI Monitor so that you can actually see and understand these gems of operating system information.  Take the guess work out of which WMI counters to use for scripts.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2014 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: