How-to: Create a robust Drive Mapping [MapDrive.vbs]

VBScript to Map a Drive letter to a network file share (non-persistent).

This script was designed back in the time of Windows XP when drive maps had a number of reliability issues: Disconnected Drives would fail to reconnect [Red-X error].

It accounts for 'remembered' connections including those to a file share that no longer exists or which is off-line.
This is a good approach for machines that are not always connected to the domain e.g. Laptops.

For each drive letter there are several possible states, that may have to be dealt with by the script:
- Remembered (persistent connection) / Not Remembered
- Already Connected / Connected to the wrong network share / Not Connected.

Windows will not map a 'remembered' connection to a different server unless you first unmap & unremember the existing connection, this applies even if the old connection path is currently disconnected.

Performance: This script is not quite as fast as a simple NET USE command, but it is faster than either of the PowerShell cmdlets: New-PSDrive or New-SMBMapping.

This script will remove any existing Drive Map before connecting to the correct file share.

' Map a network drive 

' Usage
'    cscript MapDrive.vbs drive fileshare //NoLogo
'    cscript MapDrive.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 MapDrive.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 MapDrive.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)
    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

Example:

cscript MapDrive.vbs H: \\Server64\teams //NoLogo

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 4.0 International License.

For drive mapping to work, File and Printer sharing must be enabled on the remote (server) computer.

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.

In many cases Group Policy will be an easier and faster method than mapping drives with a script.

Mapping drives in Group Policy Management Console:

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

Related VBScript commands

MapDrivePersistent - Map a Drive letter to a network file share (persistent).
.MapNetworkDrive - Drive Map.
NoDrives - Hide mapped drives from Windows Explorer.
NET - Manage network resources.
WMIC LOGICALDISK - List mapped drives.
Equivalent PowerShell command: New-PSDrive / New-SmbMapping - Create a mapped network drive.


 
Copyright © 1999-2024 SS64.com
Some rights reserved