Link to home
Start Free TrialLog in
Avatar of runright
runrightFlag for Canada

asked on

Removing a mapped network drive

I think I am loosing it...I going to narrow it down to a specific 3 line piece of code, using a vbscript I want to remove a previously mapped drive.

Dim WshNetwork
Set WshNetwork = Wscript.CreateObject("WScript.Network")
WShNetwork.RemoveNetworkDrive "M:"

After running this code and I look at "My Computer" the drive still shows with a red X.  Double click on the drive opens it and removes the red X.  Attempting to map a new location to the "M:" drive produces an error saying already mapped...am I missing something sim;e?
Avatar of theoaks
theoaks
Flag of Australia image

how about a simple:

net use m: /del

this is just batch
or try this

Option Explicit
Dim objShell, objNetwork, DriveLetter1

DriveLetter1 = "M:"

Set objShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")

objNetwork.RemoveNetworkDrive DriveLetter1
objShell.PopUp "Drive " & DriveLetter1 & " disconnected."

Wscript.Quit
Avatar of runright

ASKER

Cannot, do not, want to use a batch file.  The logon script (where this code is used) is larger than just mapping drives, it also controls the printers and the default printers.  The code you recommended is almost identical to mine with the exception of assigning the drive letter to a variable and the message at the end..  Still, I did copy your suggestion verbatim into a new test script and ran it.  It came back with "M: disconnected" and showed in My Computer with a red X.  Double clicking on the M Drive still opened the M drive and showed me the files.  I ran the script again, it came back again with "M: Disconnected".  I then ran the script again, it came back with "The network connection does not exist", although double clicking the M drive in My Computer still opens it.
Found a solution that worked....changed the below line to read

WShNetwork.RemoveNetworkDrive "M:", true, true

This does a more through removal of the drive mapping.
Avatar of Don
This will remove all mapped drives...

'****Remove all mapped drives****
For intDrive = 0 To CheckDrive.Count - 1 Step 2
    wshNetwork.RemoveNetworkDrive CheckDrive.Item(intDrive), bforce
Next
should be  >>>>

Set objNetwork = CreateObject("Wscript.Network")
Set colDrives = objNetwork.EnumNetworkDrives
For i = 0 to colDrives.Count-1 Step 2
    objNetwork.RemoveNetworkDrive colDrives.Item(i)
Next
I have been using VB Scripting for a long time now to map the drives and printers for new users and to remove and change printers as printers change for existing users.    We just finished setting up a DFS and needed to change the mapping of the drives.  This must be the first time that I have had to change/delete the mapping of drives and
WShNetwork.RemoveNetworkDrive "M:"
DOES NOT WORK, it only disconnects the drive, but leaves it mapped
WShNetwork.RemoveNetworkDrive "M:", true, true
DOES WORK
By the way, I do use a similar type looping script to first verify if a drive needs to be mapped before deleting it and re-adding it.  I found users got confused if they didnt wait for the logon script to finish running before they started opening up drives and found them not available.

DriveM = 0 
DriveS = 0
if AllDrives.Count > 0 then 
  a = 0
  do
    If AllDrives.Item(a) = "M:" then 
      a = a + 1
      If ucase(AllDrives.Item(a)) = "\\Domain\PUBLIC" then 
        DriveM = 1
      end if
    elseIf AllDrives.Item(a) = "S:" then 
      a = a + 1
      If UCase(AllDrives.Item(a)) = "\\Server\CLIENTAPPS" then 
        DriveS = 1
      end if
    else
      a = a + 1
    end if
  loop until a >= AllDrives.Count
End if
 
If DriveM = 0 then 
  On Error Resume Next
  WShNetwork.RemoveNetworkDrive "M:", true, true
  WshNetwork.MapNetworkDrive "M:", "\\Domain\Public", True
End if
 
If DriveS = 0 then 
  On Error Resume Next
  WShNetwork.RemoveNetworkDrive "S:", true, true
  WshNetwork.MapNetworkDrive "S:", "\\Server\ClientApps", True
End if

Open in new window

thanks for the post back, shouldve written the script up in something other than notepad and i would have seen the optional arguments
I would be interested in a better program than notepad for script editing....trying to find the error on line 74 is a pain in notepad...
ASKER CERTIFIED SOLUTION
Avatar of Don
Don
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial