DriveMap.vbs

Map a network drive, this script includes error traps for the situation where Windows XP has a 'remembered' connection to a file share that no longer exists (or is off-line.)

The default behaviour is that Windows XP won't map a 'remembered' connection to a different server unless you first unmap the existing connection, and it won't unmap a connection unless you first connect to the (nonexistent) remote file share. The only way out of this catch 22 is to delve into the registry.

For each drive letter there are 4 possible states:

- Remembered, but not connected
- Remembered, and connected (Persistent)
- Not Remembered, not connected
- Not Remembered, but connected (non-persistent)

' Map a network drive 

' Usage
'    cscript DriveMap.vbs drive fileshare //NoLogo
'    cscript DriveMap.vbs H: \\MyServer\MyShare //NoLogo
'
' This script will remove any existing drive map to the same drive letter
' including persistent or remembered connections (Q303209)

Option Explicit
Dim objNetwork, objDrives, objReg, i
Dim strLocalDrive, strRemoteShare, strShareConnected, strMessage
Dim bolFoundExisting, bolFoundRemembered
Const HKCU = &H80000001

' Check both parameters have been passed 
If WScript.Arguments.Count < 2 Then
 wscript.echo "Usage: cscript DriveMap.vbs drive fileshare //NoLogo"
  WScript.Quit(1)
End If

strLocalDrive = UCase(Left(WScript.Arguments.Item(0), 2))
strRemoteShare = WScript.Arguments.Item(1)
bolFoundExisting = False

' Check parameters passed make sense
If Right(strLocalDrive, 1) <> ":" OR Left(strRemoteShare, 2) <> "\\" Then
 wscript.echo "Usage: cscript DriveMap.vbs drive fileshare //NoLogo"
  WScript.Quit(1)
End If

wscript.echo " - Mapping: " + strLocalDrive + " to " + strRemoteShare

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

' Loop through the network drive connections and disconnect any that match strLocalDrive
Set objDrives = objNetwork.EnumNetworkDrives
If objDrives.Count > 0 Then
  For i = 0 To objDrives.Count-1 Step 2
    If objDrives.Item(i) = strLocalDrive Then
      strShareConnected = objDrives.Item(i+1)
      objNetwork.RemoveNetworkDrive strLocalDrive, True, True
      i=objDrives.Count-1
      bolFoundExisting = True
    End If
  Next
End If

' If there's a remembered location (persistent mapping) delete the associated HKCU registry key
If bolFoundExisting <> True Then
  Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
  objReg.GetStringValue HKCU, "Network\" & Left(strLocalDrive, 1), "RemotePath", strShareConnected
  If strShareConnected <> "" Then
    objReg.DeleteKey HKCU, "Network\" & Left(strLocalDrive, 1)
    Set objReg = Nothing
    bolFoundRemembered = True
  End If
End If

'Now actually do the drive map (not persistent)
Err.Clear
On Error Resume Next
objNetwork.MapNetworkDrive strLocalDrive, strRemoteShare, False

'Error traps
If Err <> 0 Then
  Select Case Err.Number
    Case -2147023694
      'Persistent connection so try a second time
      On Error Goto 0
      objNetwork.RemoveNetworkDrive strLocalDrive, True, True
      objNetwork.MapNetworkDrive strLocalDrive, strRemoteShare, False
      WScript.Echo "Second attempt to map drive " & strLocalDrive & " to " & strRemoteShare
    Case Else
      On Error GoTo 0
      WScript.Echo " - ERROR: Failed to map drive " & strLocalDrive & " to " & strRemoteShare
  End Select
  Err.Clear
End If

Set objNetwork = Nothing

Simon Sheppard, SS64.com, Feb 2005
Credit (for the difficult parts) to Kenneth MacDonald, Edinburgh University Computing Services.

You are free to use or modify this script: Creative Commons Attribution 2.5 License.

For mapping multiple drives at once, theres an alternative version of this script over in the forum, it avoids having to load CSCRIPT.exe more than once.

“Success is falling nine times and getting up ten” ~ Jon Bon Jovi

Related:

.MapNetworkDrive - Drive Map
NoDrives - Hide mapped drives from Windows Explorer


© Copyright SS64.com 1999-2013
Some rights reserved