Link to home
Start Free TrialLog in
Avatar of McCoyIT
McCoyIT

asked on

Removing mapped drive connections for non-existant shares

I've searched and found results similar to my issue, but no solution that worked for the situation that keeps coming up which is:

A drive was mapped for users by a logon script (vbscript), and the same drive letter has been changed in the logon script to point to a new server name, when the old server has been taken offline.

The issue we run into is that the mapped drive in the script does not replace the previous mapped drive, until the user removes the drive manually (actually going into Windows Explorer, right clicking the drive > Disconnect. After logging off and back on and the drive now maps to the new location.

What we currently have in our logon script is this:

'Disconnect ALL mapped drives except H:
Set oNetwork = CreateObject("WScript.Network")
Set oEnumerate = oNetwork.EnumNetworkDrives
For i = 0 to oEnumerate.Count - 1 Step 2
  Select Case Trim(oEnumerate.Item(i) & "")
  Case "H:" 'Skip Drive
  Case ""
    oNetwork.RemoveNetworkDrive oEnumerate.Item(i+1), True, True
  Case Else
    oNetwork.RemoveNetworkDrive oEnumerate.Item(i), True, True
  End Select
Next

Open in new window


But it does not seem to be able to remove a drive / share that is no longer accessible. Is there any vbscript code that we could use that would accomplish this?
Avatar of CraigFrost
CraigFrost
Flag of United States of America image

Avatar of McCoyIT
McCoyIT

ASKER

No luck unforunately, I get the following error:

Script: (Path to .vbs file)
Line: 12
Char: 1
Error: This network connection does not exist.

Code 800708CA
Source: WSHNetwork.RemoveNetworkDrive
Does it work correctly afterwards if you use the NET USE /DELETE command to disconnect it?  For example:
NET USE G: /DELETE

Open in new window

If so, you could change your login script to disconnect using the WScript.Shell object's Run Method instead, invisibly of course:
'Disconnect ALL mapped drives except H:
Set oNetwork = CreateObject("WScript.Network")
Set oShell = Wscript.CreateObject("WScript.Shell")
Set oEnumerate = oNetwork.EnumNetworkDrives
For i = 0 to oEnumerate.Count - 1 Step 2
  Select Case Trim(oEnumerate.Item(i) & "")
  Case "H:" 'Skip Drive
  Case ""
    oNetwork.RemoveNetworkDrive oEnumerate.Item(i+1), True, True
  Case Else
    oShell.Run "NET USE " & oEnumerate.Item(i) & " /DELETE", 0, True
  End Select
Next

Open in new window

Avatar of remmett70
You could get around this by creating a static DNS record for the old server, with the new servers IP address.  Create a share to match what was mapped before.  This should allow your script to remove the old share and create the new one.  After a few days, when you are confident all the old mappings have been taken care of.  You only need to remove the share and delete the DNS record.
Avatar of McCoyIT

ASKER

CitizenRon I think you may be on to something, if I run "NET USE G: /DELETE" from a command window it shows successful and does remove the disconnected drive, and the drive maps properly at next logon.

However when I tried adding the second part to the logon script, the old drive mapping remains and does not disconnect.

Any ideas?

I like the DNS idea, and it would work in some cases, but in others a server is repurposed without changing the name, so that wouldn't work.
I did a bit of research and this situation seems to be somewhat prevalent.  One suggestion was to use NET USE in a slightly more aggressive manner:
NET USE * /DELETE /Y

Open in new window

which should delete every mapping that NET USE has access to.  A couple people said it worked where specifically telling NET USE which drive to disconnect didn't work in the login script.  Not sure if that's acceptable to your environment though.
ASKER CERTIFIED SOLUTION
Avatar of McCoyIT
McCoyIT

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
Avatar of McCoyIT

ASKER

Tested and found solution.