'How to add a mapped network drive via VBS?
I'm having some issues with my vbs script. It will add only the F drive and not add the G driver after it. What am I doing wrong?
'## This is for network drives
Set objNetwork = CreateObject("WScript.Network")
objNetwork.RemoveNetworkDrive "F:", True, True
'## for adding
Set objNetwork = CreateObject("WScript.Network")
objNetwork.MapNetworkDrive "F:" , "\\myserver\share1"
objNetwork.MapNetworkDrive "G:" , "\\myserver\share2"
Solution 1:[1]
MapDrive.vbs
VBScript to Map a Drive letter to a network file share (non-persistent).
This script is designed for reliability above speed, so it will reconnect at every login. 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.
Windows XP 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.
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.
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)
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
Solution 2:[2]
I've done this before like this:
dim objNet, strLocal, strPath, fso
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
Set objNet = WScript.CreateObject("WScript.Network")
'Name the drives and their paths
strLocal = Array("H:","M:")
strPath = Array("\\Full\Path\Number1", _
\\Full\Path\Number2")
'Loop to check if they are mapped, map it if they are not
For i = LBound(strLocal) To UBound(strLocal)
If fso.FolderExists(strLocal(i)) = True Then
wscript.echo(strLocal(i) & " Mapped")
Else
objNet.MapNetworkDrive strLocal(i), strPath(i), False
wscript.echo(strLocal(i) & " Re-mapped")
End If
Next
'Wrap up the script
WScript.Echo("")
WScript.Echo("Mapping Completed")
WScript.Sleep(2000)
'Keep the command prompt open long enough to see that it is completed
Set fso=Nothing
Set objNet=Nothing
Essentially, it checks to see if the drive is mapped already, and if not, then it will map it. I added this to my startup folder because I keep getting my corp network drives to lose connection when I reboot.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Hackoo |
| Solution 2 | legendjr |
